Im currently creating a python socket http server, and I'm working on my GET and POST requests. I got my GET implementation working fine, but the body element of the POST requests won't show up.
self.host = ''
self.port = 8080
self.listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
client_connection, client_address = self.listener.accept()
request = client_connection.recv(2048)
POST /test.txt HTTP/1.1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/601.6.17 (KHTML, like Gecko) Version/9.1.1 Safari/601.6.17
Accept-Encoding: gzip, deflate
while True: client_connection, client_address = self.listener.accept() request = client_connection.recv(2048) print request
recv does not read exactly 2048 bytes but it reads up to 2048 bytes. If some data arrive
recv will return with the data even if more data might follow. My guess is that in your case the client is first sending the HTTP header and then the body. If NAGLE algorithms is off at the client side (common) it is likely that your first
recv will only get the header and that you would need another
recv for the body. This would explain what happens in your case: you get the header but not the body since you don't do another
But even that would be a too simple implementation which will go wrong sooner or later. To make it correctly you should implement the HTTP protocol correctly: first read the HTTP header which might need multiple
recv if the header is large. Then you should parse the header, figure out the size of the body (
Content-length header) and read the remaining bytes.