I have a problem here with this code. I’m opening a socket, then listening to it with a while loop. I send data from a php script with
socket_write($sock, $test, $len);
It works very well, but when I send several writes in a row, the Python script handles some of the writes as just one write.
import socket
HOST = 'localhost' # the host
PORT = 12126 # the port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
while 1:
s.listen(0)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.close()
I’m looking for a way to listen to that port and get one write after another.
That’s not the way sockets work. Bytes go in and bytes come out, but there has to be some other mechanism to tell you how big a message is. If you send 50 bytes, then another 75 bytes, then 20 bytes on one end of a socket, and then call recv(100), you could get anywhere from 1 to 100 bytes from a blocking socket. You are responsible for buffering recv’s until you have a complete message, and you have to define what a complete message is. Some options:
Here’s an example of a class to buffer received data using a sentinel byte: