First try on a UTF-16 to UTF-8 conversion.
This commit is contained in:
parent
ca9fd8c2d7
commit
4ccd8e7f2e
26
ptpip.py
26
ptpip.py
@ -26,6 +26,22 @@ class PTPIP:
|
||||
payload = self.socket.recv(pklen_int - 8)
|
||||
return (pktype_int, payload)
|
||||
|
||||
# see http://www.ietf.org/rfc/rfc2781.txt
|
||||
def utf16to8(self, u16text):
|
||||
u8text = ""
|
||||
for i in range(0, len(u16text), 2):
|
||||
char16 = u16text[i:i+2]
|
||||
char16 = struct.unpack('<H', char16)[0]
|
||||
if char16 < 0xd800 or char16 > 0xdfff:
|
||||
u8text += chr(char16)
|
||||
else:
|
||||
print("Encoded UTF-16. Please improve this method.")
|
||||
return u8text
|
||||
|
||||
# see http://www.ietf.org/rfc/rfc2781.txt
|
||||
def utf8to16(self, u8text):
|
||||
pass
|
||||
|
||||
def getSocket(self):
|
||||
return self.socket
|
||||
|
||||
@ -33,4 +49,12 @@ class PTPIP:
|
||||
pkg = self.createPkg(1, str.encode(guid[:16]) + str.encode(identifier))
|
||||
self.socket.send(pkg)
|
||||
result = self.recvPkg()
|
||||
return result
|
||||
if result[0] == 5:
|
||||
print("INIT FAILED!")
|
||||
return False
|
||||
elif result[0] != 2:
|
||||
print("Unknown package type: %i" % result[0])
|
||||
return False
|
||||
remote_guid = result[1][0:16]
|
||||
remote_name = self.utf16to8(result[1][16:])
|
||||
return (remote_guid, remote_name)
|
||||
|
Loading…
x
Reference in New Issue
Block a user