0

Yes I know UDP is bad but unfortunately I have no choice - my server only takes UDP...

What I have is a list that contains hex values, need to send that out UDP.

If I try and send the list I get - 'TypeError: must be string or buffer, not list'

If i convert to a string (called aList in my code) I get - 'TypeError: an integer is required' Print of aList = 09004000e3f00005f5

if I convert aList to an int with base 16 I get - 'TypeError: must be string or buffer, not long'

Suspect it's something basic but I'm missing it.

Simple code looks like::

import socket   #for sockets    

UDP_PORT    = 21105;
UDP_HOST    = '10.194.34.151';


z21 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

def _getLocoInfo (id):
    arr        = [ 0x09, 0x00, 0x40, 0x00, 0xE3, 0xF0, 0x00, id]
    arr.append(arr[5]^arr[6]^arr[7])
    return arr

msg = _getLocoInfo(0x05)
aList = "".join("%02x" % b for b in msg)

print (a)

try :
    z21.send(msg ,(UDP_HOST, UDP_PORT) )
     # receive data from client (data, addr)
    d = s.recvfrom(1024)

    print ('Server reply : ' + reply)

except socket.error as e:
        print ('Error Code : ' + str(e[0]) + ' Message ' + e[1])
        sys.exit()
1
  • 1
    Are you using Python 2 or 3? Commented Apr 12, 2016 at 0:15

1 Answer 1

1

I'm not very knowledgeable when it comes to python's socket module but looking at this page I feel you are missing two things:

  • try using z21.sendto instead of z21.send
  • try converting your message to a byte buffer with bytes()

So the result would look like this:

... other code

try:
  z21.sendto(bytes(msg) ,(UDP_HOST, UDP_PORT))

... following code

I hope this helps.

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

1 Comment

Well would you look at that. Don't I feel like a dumbass. That looks like it sends now but no response back so time to fire up wireshark. But at this stage looks good.

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.