title: How to call function from Python HTTP server?
body: <p>I want to provide one of my applications with a web interface. To achieve that, I subclass <code>BaseHTTPRequestHandler</code> from the <code>BaseHTTPServer</code> package (into a class I call <code>_RequestHandler</code>) and implement my own handlers for HTTP <code>HEAD / GET / POST / PUT</code> requests.</p>
  
  <p>When I get a request for a "normal file", I just open it, read it from the file system and serve it to the browser. That works fine. However, being a web interface, I also want to communicate certain requests to the application backend. To do this, I want my server to provide an interface, over which callback functions for certain URLs can be registered (by the backend), which will be called when there is a request incoming for the registered URL and pass the request head and body to the request handler (= callback function) registered for that URL.</p>
  
  <p>I. e. I want to be able to do something like this.</p>
  
  <pre><code>def callback_function(request):
     # Do some processing.
     return response
  
  srv = WebServer(port = 8080, webroot = "htdocs/")
  srv.register("/cgi-bin/test", callback_function)
  srv.run()
  </code></pre>
  
  <p>Now, when someone requests some arbitrary URL with the browser, that has not been registered, the web server will load the specified file (relative to a the "web root") and deliver it.</p>
  
  <p>I know how to implement this in principle. Place the URLs that should be handled differently in a dictionary. For each request, check if the requested URL is in the dictionary. If yes, put the request data (HTTP header and body) into a "request object", call the callback function registered for that URL and pass it that request object. Acquire the response object from the callback function, extract its contents (HTTP header and body) and serve it to the client. If the URL is not in the "special handled" dictionary, just load the file from the file system (relative to the "web root") and serve it.</p>
  
  <p>The problem is, that in order to run the web server, I have to do something like this.</p>
  
  <pre><code>server = HTTPServer(("localhost", port), _RequestHandler)
  server.serve_forever()
  </code></pre>
  
  <p>Now the problem is that I cannot pass an <strong>object</strong>, an <strong>instance</strong> of <code>_RequestHandler</code> to the constructor of <code>HTTPServer</code>. I have to pass <strong>the class itself</strong>. <code>HTTPServer</code> will create the instance for me, which will be inaccessible to me, so I cannot call methods on it and thus not register a callback function to it (or pass a dictionary of callback functions to the constructor or whatever).</p>
  
  <p>What can I do? There must be some way of getting a custom data object into the <code>_RequestHandler</code> instance, right? I mean if there isn't, then all that server stuff appears to be <strong>seriously</strong> restricted. I don't see how I can do that though, since I don't have access to the instance of <code>_RequestHandler</code>. It will be created by the <code>HTTPServer</code> class for me and not be passed to the outside.</p>
  
  <p>Any suggestions?</p>