4 ms·
From: https://github.com/aosabook/500lines/blob/master/ci/code/helpers.py https://github.com/aosabook/500lines/blob/master/ci/code/hel... s = socket.socket
by banthar 10y ago
From: https://github.com/aosabook/500lines/blob/master/ci/code/helpers.py https://github.com/aosabook/500lines/blob/master/ci/code/hel...
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send(request)
response = s.recv(1024)
s.close()
Is that correct use of TCP? It seems to rely on response not being fragmented.
- smegel 10y agoNo it is not. You must keep reading from the socket until you have read as many bytes as required - he isn't even checking how many bytes recv returned.
- DSMan195276 10y agoIt's definitely not right as far as the C interface is concerned - and Google indicates that the Python interface doesn't do anything extra. That said, it probably does generally work considering the small size of the messages, especially if the dispatcher is run locally. It's unlikely that a two-byte or ten-byte message would get fragmented, though it technically could - and that's all that is sent by the dispatcher. That said, this should be a loop that combines all the responses until you receive a response of length zero, indicating EOF. The actual code to correctly handle this is really simple if you don't do any extra error handling - but it's not obvious if you haven't done some socket programming before. This code will probably work until the dispatcher and clients are expanded upon, resulting in more complex messages that eventually lead to sporadic fragmentation.
- deleted 10y ago[deleted]