http POST message body not received
<p>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.
Code snippet:</p>

<pre><code>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)
self.listener.bind((self.host, self.port))
self.listener.listen(1)

while True:

        client_connection, client_address = self.listener.accept()
        request = client_connection.recv(2048)
        print request
</code></pre>

<p>This code yields the http header after processing the post request from the webpage:</p>

<pre><code>POST /test.txt HTTP/1.1
Host: localhost:8080
Content-Type: application/x-www-form-urlencoded
Origin: http://localhost:8080
Content-Length: 21
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
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
Referer: http://localhost:8080/
Accept-Language: nb-no
Accept-Encoding: gzip, deflate
</code></pre>

<p>But there is no body, so the question is why am i not receiving the http body when i know it is sent?</p>

<p>Thanks!</p>

<pre><code>while True:

        client_connection, client_address = self.listener.accept()
        request = client_connection.recv(2048)
        print request
</code></pre>

<p><code>recv</code> does not read exactly 2048 bytes but it reads up to 2048 bytes. If some data arrive <code>recv</code> 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 <code>recv</code> will only get the header and that you would need another <code>recv</code> 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 <code>recv</code>.</p>

<p>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 <code>recv</code> if the header is large. Then you should parse the header, figure out the size of the body (<code>Content-length</code> header) and read the remaining bytes.</p>

python sockets http http-1.1