1

Working with Python sockets using some code that I found to make a telnet server. The server code works fine. I am in need of sending hex strings to/ from the client do to escape character issues. When I send data to the client like this:

conn.sendall('\x74\x65\x73\x74\x31\x32\x33\x0D\x0A')

or

test_var = '\x74\x65\x73\x74\x31\x32\x33\x0D\x0A'
conn.sendall(test_var)

It works perfect. When I try to create a string and store it in a variable (like the following kludge):

def recover_raw_data(data):

    data_list = []
    hex_list = []

    for items in data:
        data_list.append(ord(items))

    for items in data_list:
        hex_list.append("\\")
        value = '%02X' % int(items)
        hex_list.append("0x" + value)
    print hex_list   
    almost_final_data = "".join(hex_list)
    just_about_final_data = almost_final_data.replace('\\0x', '\\x')
    final_data = just_about_final_data
    print final_data
    conn.sendall(final_data)
    return()

You can print the output of this mess and it looks right, a Wireshark capture shows the packet going out literally and not as ascii...

e.g. \x31\x32\x33\x34\x35\x0D\x0A

I've tried .encode and a ton of other ideas I found on Google... Wondering why I can't create a string from a variable that works... Any help would be greatly appreciated.

1
  • 1
    Because it would require re-interpreting a literal string. You might want to look at unpack if you want to get the real characters back. Commented Mar 4, 2013 at 20:23

1 Answer 1

1

A (Python2) str is a sequence of bytes. For instance, '\x31\x32\x33\x34\x35\x0D\x0A' is a sequence of bytes. You can see which bytes more clearly by applying list to the str:

In [26]: list('\x31\x32\x33\x34\x35\x0D\x0A')
Out[26]: ['1', '2', '3', '4', '5', '\r', '\n']

So you see, you really have 8 bytes here. '\x31' is all one byte. So you can not reproduce this str by joining the backslash character "\\" with the x character and then numerical characters: (e.g. the raw string r'\x31\x32\x33\x34\x35\x0D\x0A'.)

In [28]: list(r'\x31\x32\x33\x34\x35\x0D\x0A')[:10]
Out[28]: ['\\', 'x', '3', '1', '\\', 'x', '3', '2', '\\', 'x']

I'm not sure I understand the purpose of recover_raw_data so perhaps the following is wrong, but it seems to me all you need is:

def recover_raw_data(data):
    conn.sendall(data)

(And if that is true, then you do not need recover_raw_data at all since conn.sendall suffices...)


Actually, it is possible to convert the literal string (with backslash and x characters) to the desired string, by decoding with the 'string_escape' codec:

In [30]: (r'\x31\x32\x33\x34\x35\x0D\x0A').decode('string_escape')
Out[30]: '12345\r\n'

But I really don't think you should need to do this. Your data appears to be already in the right form before you go through this rigmarole.


PS. If you show an example of what the input to recover_raw_data looks like, and what you want done to it, perhaps we can suggest how to modify recover_raw_data.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. Ended up find out that you are correct. The data is already in the right form. Found out that the input data string we were using to test was flawed and chased my tail from there... Thanks for helping me vent and learn! May have a use for the 'string_escape' anyway, so always learning... Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.