There are a couple of issues with your original example. The first is that if you use the request
method, you should include the message body you want to send in that call, rather than calling send
separately. The documentation notes send() can be used as an alternative to request
:
As an alternative to using the request() method described above, you can also send your request step by step, by using the four functions below.
You just want conn.request("POST", "/testurl", "clientdata")
.
The second issue is the way you're trying to read what's sent to the server. self.rfile.read()
attempts to read the entire input stream coming from the client, which means it will block until the stream is closed. The stream won't be closed until connection is closed. What you want to do is read exactly how many bytes were sent from the client, and that's it. How do you know how many bytes that is? The headers, of course:
length = int(self.headers['Content-length'])
print(self.rfile.read(length))
I do highly recommend the python-requests library if you're going to do more than very basic tests. I also recommend using a better HTTP framework/server than BaseHTTPServer for more than very basic tests (flask, bottle, tornado, etc.).