Zelly Posted June 23, 2015 Posted June 23, 2015 EDIT: Figured out a work around use b'\xFF\xFF\xFF\xFFgetstatus' for send and used buf=buf[4:] for receive END EDIT: Don't know enough about how this works to google it myself. Basically I am trying to send a \0xFF\0xFF\0xFF\0xFFgetstatus request to an ET Server. In python 2.7 this worked: mysocket.sendto('\0xFF\0xFF\0xFF\0xFFgetstatus',(IP,PORT)) Now in python 3 I need to convert my message to a 'bytes' object instead of str?(I think. This is where I lose understanding) something like: mysocket.sendto(bytes('\0xFF\0xFF\0xFF\0xFFgetstatus','UTF-8),(IP,PORT)) I think. My problem is I have no idea what encoding to send lol. I have tried utf-8 , utf-16 , utf-32 , ascii and a few other random ones from: https://docs.python.org/3/library/codecs.html#standard-encodings Anyone got any ideas? Thanks in advance! 1 Quote
Clan Friend SunLight Posted June 23, 2015 Clan Friend Posted June 23, 2015 note: I am not a python expert by any means, and I have never used python 3 that being said... (listening to udp port 1234, and getting hex dump) $ nc -ulx -p 1234 with utf-8 encoding 00000000 C3 BF C3 BF C3 BF C3 BF 67 65 74 73 74 61 74 75 ........getstatu 00000010 73 s wrong. actually no need to use netcat, since you can just: >>> msg = '\xff\xff\xff\xffgetstatus' >>> type(msg) <class 'str'> >>> msg = bytes(msg, 'utf-8') >>> type(msg) <class 'bytes'> >>> repr(msg) "b'\\xc3\\xbf\\xc3\\xbf\\xc3\\xbf\\xc3\\xbfgetstatus'" >>> and you see it's wrong... instead, if you just: >>> msg = b'\xff\xff\xff\xffgetstatus' >>> type(msg) <class 'bytes'> >>> repr(msg) "b'\\xff\\xff\\xff\\xffgetstatus'" >>> and it works with python3... netcat output from mysocket.sendto(msg,(IP,PORT)): $ nc -ulx -p 1234 ����getstatusReceived 13 bytes from the socket 00000000 FF FF FF FF 67 65 74 73 74 61 74 75 73 ....getstatusso basically you have to create 'bytes' straight away, not str then convert. put a 'b' in front 2 Quote
menatwork Posted July 29, 2015 Posted July 29, 2015 Sunlight you're quite correct! Additionally, I'd like to mention to Zelly that there's a 2to3 code translator: https://docs.python.org/2/library/2to3.html- It might be useful in the future. When using Sockets perhaps Twisted ( https://twistedmatrix.com/trac/) might be useful; it takes the pain out of working with sockets and the likes. 1 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.