sockets - Python send/ recv binary data -


i trying send , receive binary data in python(v3). way attempting @ client end, manually create string of bytes , send through socket server. on server side, receive data , manually access individual bits resolve message.

this how client looks:

remoteip = "164.107.112.72" remoteport = 34562 remotestruct = remoteip.split('.', 4) remoteipbytes = ((int)(remotestruct[0])).to_bytes(1, byteorder='little') + ((int)(remotestruct[1])).to_bytes(1, byteorder='little') + ((int)(remotestruct[2])).to_bytes(1, byteorder='little') + ((int)(remotestruct[3])).to_bytes(1, byteorder='little')  headerstruct = remoteipbytes + remoteport.to_bytes(2, byteorder='little') sequencenum = 0 size = #filesize  data = headerstruct + sequencenum.to_bytes(1, byteorder='little') + (1).to_bytes(1,byteorder='little') + size.to_bytes(4, byteorder='little') #data sent server 

this how server handles data:

serversock = socket.socket(socket.af_inet, socket.sock_dgram)   # udp datagram host = '164.107.112.70' serversock.bind((host, localport))  seqnum = 0  #get size size = 0 var = serversock.recvfrom(12) seqnum = int.from_bytes((str(var))[6], byteorder='little') 

in last line of code, error saying (str(var)) being treated unicode string rather binary one. however, if don't cast string, access out of bounds error.

can explain me correct way send , receive binary data in python is? have tried using struct.pack , unpack before/after sending data, errors during unpacking, though format strings identical

(int)(remotestruct[0])

this looks lot c code. python doesn't have type casts c.

int refers object. way use calling - it's function.

instead write int(remotestruct[0]). wrote works because parenthesis totally redundant , don't change meaning. isn't syntactic difference, though. it's basic difference in type systems of 2 languages.

this has nothing question, though.

ipbytes = ((int)(remotestruct[0])).to_bytes(1, byteorder='little') + ((int)(remotestruct[1])).to_bytes(1, byteorder='little') + ((int)(remotestruct[2])).to_bytes(1, byteorder='little') + ((int)(remotestruct[3])).to_bytes(1, byteorder='little') 

this incredibly verbose way pack ipv4 address 4 bytes. try instead:

import socket print socket.inet_pton(socket.af_inet, "1.2.3.4") 

though not directly related question.

the source of indexerror problem (i'm guessing that's exception encounter, said "access out of bounds error" isn't exists in python's built-in types - don't paraphrase errors, quote them verbatim) somewhere in vicinity of:

var = serversock.recvfrom(12) 

notice return value of recvfrom: the return value pair (bytes, address).

if try index object @ 6 you'll indexerror raised, yes.

using str unicode string representation of pair won't though - although might prevent indexing operation failing because resulting unicode string long enough. :) you'll getting garbage, though.

instead, try indexing bytes read instead:

data, addr = serversock.recvfrom(12)  if len(data) > 6:     seqnum = ord(data[6]) 

notice need careful length checking here. because passed 12 recvfrom doesn't mean returns 12 bytes. means returns up to 12 bytes. don't want program explode unhandled exception first time sends short datagram.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -