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.
unpackif you want to get the real characters back.