3

I know in order to send data through a UDP socket using python has to be in the following format "\x41" = A .

I have a python script udp_send.py and I want to send in a hex value to represent a register value on the server side.

I run my script through linux terminal >>python udp_send.py "\x41" . I read the variable using argv[1] in my python script. I did a len check and it is 3 . It ignore \ and take x, 4 and 1 as 1 byte each whereas I want \x41 to represent 1 byte only.

I was then trying to concatenate data="\x"+"41" but it did not work.

"\x" is interpreted as escape by python. I am trying to find a way to pass a hex value into my script and send it via UDP socket?

I have achieved the following so far. Send a hex value defined in python script via UDP socket .

from socket import *
from sys import *

## Set the socket parameters
host = <ip-define-here>
port = <port-define-here>
buf = 1024
addr = (host,port)

## Create socket
UDPSock = socket(AF_INET,SOCK_DGRAM)

## Send messages
data ='\x41'
#data=argv[1] #commented out
if not data:
    print "No data"
else:
    if(UDPSock.sendto(data,addr)):
        print "Sending message ",data

## Close socket
UDPSock.close()

I used wireshark on server side and saw the hex value 41 appear.

Just to be clear "41"(made of 2 bytes) is not the same as "\x41"(one byte only) in python.

My simple question is, how can I take a character "41" and join it with "\x" to form "\x41" so I can assign it to data variable in my python script so I can send it as hex value 41.

2 Answers 2

3

In your code, you can convert your arg to proper format, then you can call it with python udp_send.py 0x41

arg = sys.argv[1]

# intarg = 65 == 0x41
intarg = int(arg, 16)

# now convert to byte string '\x41'
hexstring = struct.pack('B', intarg)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much. Exactly what I was looking for. I knew a pack method existed, just did not know how to use it. Thank you once again.
Instead of struct.pack, you can also use chr(intarg).
1

You are making your problem more complicated than you need to. Characters already contain their byte values. You should be able to just pass the string 'A', and everything will work fine.

First of all, there is no difference between '\x41' and 'A' in python. Both represent a one character string made up of the byte 0x41. (or binary 0100 0001. Any place (in python) that you can use one, you can use the other. The problem is coming because of how the shell parses your string before it passes it in to python. The shell sees the backslash, tries to escape the next character, fails (because \x is not a valid shell escape), passes x through unescaped, and then passes the characters 4 and 1. So python receives a three character string made up of the characters x 4 and 1. If you want to pass the backslash through, you need to escape it, but that won't acheive what you want. Then python will get a four byte string consisting of the characters \, x, 4, and 1. Its repr will be '\\x4a', and if you print it, it will look like \x4a.

In order to get the one-byte value 0x41, use the character A.

2 Comments

Thanks jcdyer for the answer above. However i would like to send different hex values for different register addresses on the server side. If I want to send "\xAA" or "x01" or "\x0F", which can not be defined by character exactly like "\x41" can with character 'A'. So I want to send hex value to python and send that out through udp port.
Ah, yes. Then do what the other answer says. :)

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.