title: Slow Python HTTP server on localhost
body: <p>I am experiencing some performance problems when creating a very simple Python HTTP server. The key issue is that performance is varying depending on which client I use to access it, where the server and all clients are being run on the local machine. For instance, a GET request issued from a Python script (urllib2.urlopen('<a href="http://localhost/" rel="noreferrer">http://localhost/</a>').read()) takes just over a second to complete, which seems slow considering that the server is under no load. Running the GET request from Excel using MSXML2.ServerXMLHTTP also feels slow. However, requesting the data Google Chrome or from RCurl, the curl add-in for R, yields an essentially instantaneous response, which is what I would expect.</p>
  
  <p>Adding further to my confusion is that I do not experience any performance problems for any client when I am on my computer at work (the performance problems are on my home computer). Both systems run Python 2.6, although the work computer runs Windows XP instead of 7.</p>
  
  <p>Below is my very simple server example, which simply returns 'Hello world' for any get request.</p>
  
  <pre><code>from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
  
  class MyHandler(BaseHTTPRequestHandler):
      def do_GET(self):
          print("Just received a GET request")
          self.send_response(200)
          self.send_header("Content-type", "text/html")
          self.end_headers()
  
          self.wfile.write('Hello world')
  
          return
  
      def log_request(self, code=None, size=None):
          print('Request')
  
      def log_message(self, format, *args):
          print('Message')
  
  if __name__ == "__main__":
      try:
          server = HTTPServer(('localhost', 80), MyHandler)
          print('Started http server')
          server.serve_forever()
      except KeyboardInterrupt:
          print('^C received, shutting down server')
          server.socket.close()
  </code></pre>
  
  <p>Note that in MyHandler I override the log_request() and log_message() functions. The reason is that I read that a fully-qualified domain name lookup performed by one of these functions might be a reason for a slow server. Unfortunately setting them to just print a static message did not solve my problem.</p>
  
  <p>Also, notice that I have put in a print() statement as the first line of the do_GET() routine in MyHandler. The slowness occurs prior to this message being printed, meaning that none of the stuff that comes after it is causing a delay.</p>