Jump to content

Python 3 bytes encoding


Zelly

Recommended Posts

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!

S4E8tmR.gif

  • Like 1
Link to comment
Share on other sites

  • Clan Friend

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           ....getstatus
so basically you have to create 'bytes' straight away, not str then convert. put a 'b' in front
  • Like 2
Link to comment
Share on other sites

  • 1 month later...

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.

  • Like 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.