title: How do I send and receive HTTP POST requests in Python?
body: <p>I have these two Python scripts I'm using to attempt to work out how to send and receive POST requests in Python:</p>
  
  <p>The Client:</p>
  
  <pre><code>import httplib
  
  conn = httplib.HTTPConnection("localhost:8000")
  conn.request("POST", "/testurl")
  conn.send("clientdata")
  response = conn.getresponse()
  conn.close()
  
  print(response.read())
  </code></pre>
  
  <p>The Server:</p>
  
  <pre><code>from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
  
  ADDR = "localhost"
  PORT = 8000
  
  class RequestHandler(BaseHTTPRequestHandler):        
      def do_POST(self):
          print(self.path)
          print(self.rfile.read())
          self.send_response(200, "OK")
          self.end_headers()
          self.wfile.write("serverdata")
  
  httpd = HTTPServer((ADDR, PORT), RequestHandler)
  httpd.serve_forever()
  </code></pre>
  
  <p>The problem is that the server hangs on self.rfile.read() until conn.close() has been called on the client but if conn.close() is called on the client the client cannot receive a response from the server. This creates a situation where one can either get a response from the server or read the POST data but never both. I assume there is something I'm missing here that will fix this problem.</p>
  
  <p>Additional information:</p>
  
  <p>conn.getresponse() causes the client to hang until the response is received from the server. The response doesn't appear to be received until the function on the server has finished execution. </p>