TCP Socket recv()
Hi there,
I've been stuck on this for the past few days. I can't seem to wrap my head around this in python.
I'm trying to build a TCP socket listening program using Python to listen for a signal from my Arduino Uno. I'm attempting to encrypt the signal using the xxtea modules. However I'm stumped with the TCP portion. I have a couple of question.
The data sent by the arduino is in bytes. The python listening server has successfully picked up the data sent. However I'm not sure how to tell the server to stop listening. I've read up about it and I found this site here extremely helpful. But there is a portion of the code I don't understand.
End='something useable as an end marker'
def recv_end(the_socket):
total_data=[];data=''
while True:
data=the_socket.recv(8192)
if End in data:
total_data.append(data[:data.find(End)])
break
total_data.append(data)
if len(total_data)>1:
#check if end_of_data was split
last_pair=total_data[-2]+total_data[-1]
if End in last_pair:
total_data[-2]=last_pair[:last_pair.find(End)]
total_data.pop()
break
return ''.join(total_data)
In the last_pair=total_data[-2]+total_data[-1] , how is it possible to join to bytes together? Also is the end result a string?
Secondly in the portion total_data[-2]=last_pair[:last_pair.find(End)] ,I under stand that I can specify a range in a python list but how does the last_pair.find(End) work? I thought python lists do not have a find method?
Sorry if all this seems pretty basic. I'm really confused
2 comments · 100% upvoted · other discussions
bytesobjects are a lot like strings, which means you can concatenate them with the+operator. Try this in a repl:b"a" + b"c"and see what comes out.You are correct in that Python lists do not have a
findmethod, howeverbytesobjects do.last_pairis abytesobject.EndUsersarePITA [OP] 1 points · Apr 2020
Thank you! I guess I have a ways more to learn.