HTTPserver: how to access variables of a higher class?

Tom P werotizy at freent.dd
Fri Apr 5 07:02:38 EDT 2013


First, here's a sample test program:
<code>
import sys
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler

class MyRequestHandler(BaseHTTPRequestHandler, object):
     def do_GET(self):
         top_self = super(MyRequestHandler, self) # try to access 
MyWebServer instance
         self.send_response(200)
         self.send_header('Content-type',	'text/html')
         self.end_headers()
         self.wfile.write("thanks for trying, but I'd like to get at 
self.foo and self.bar")
         return

class MyWebServer(object):
     def __init__(self):
         self.foo = "foo"  # these are what I want to access from inside 
do_GET
         self.bar = "bar"
         self.httpd = HTTPServer(('127.0.0.1', 8000), MyRequestHandler)
         sa = self.httpd.socket.getsockname()
         print "Serving HTTP on", sa[0], "port", sa[1], "..."

     def runIt(self):
         self.httpd.serve_forever()

server = MyWebServer()
server.runIt()

</code>

I want to access the foo and bar variables from do_GET, but I can't 
figure out how. I suppose this is something to do with new-style vs. 
old-style classes, but I lost for a solution.



More information about the Python-list mailing list