Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news-1.dfn.de!news.dfn.de!news.informatik.hu-berlin.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Tom P Newsgroups: comp.lang.python Subject: Re: HTTPserver: how to access variables of a higher class? Date: Fri, 05 Apr 2013 23:41:00 +0200 Lines: 78 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net 5wuSthE18uAQ/XVQTHvshgLEnLt2GwhjKD0lockB8gjATvA7w= Cancel-Lock: sha1:kFYwIaU7lHQD+FX1CUd8jGaJiJU= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121011 Thunderbird/16.0.1 In-Reply-To: Xref: csiph.com comp.lang.python:42851 On 04/05/2013 01:54 PM, Dave Angel wrote: > On 04/05/2013 07:02 AM, Tom P wrote: >> First, here's a sample test program: >> >> 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() >> >> >> >> 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. > > It'd have been good to tell us that this was on Python 2.7 > > Is MyWebServer class intended to have exactly one instance? If so, you > could save the instance as a class attribute, and trivially access it > from outside the class. > > If it might have more than one instance, then we'd need to know more > about the class BaseHTTPServer.HTTPServer, From a quick glance at the > docs, it looks like you get an attribute called server. So inside the > do_GET() method, you should be able to access self.server.foo and > self.server.bar > > See http://docs.python.org/2/library/basehttpserver.html > That doesn't work. Maybe you mean something that I'm missing? Setting a breakpoint in do_GET: Pdb) b 7 Breakpoint 1 at /home/tom/Desktop/tidy/Python/webserver/simpleWebserver.py:7 (Pdb) c Serving HTTP on 127.0.0.1 port 8000 ... > /home/tom/Desktop/tidy/Python/webserver/simpleWebserver.py(7)do_GET() -> self.send_response(200) (Pdb) self <__main__.MyRequestHandler instance at 0x7ff20dde3bd8> (Pdb) self.server (Pdb) dir(self.server) ['RequestHandlerClass', '_BaseServer__is_shut_down', '_BaseServer__shutdown_request', '__doc__', '__init__', '__module__', '_handle_request_noblock', 'address_family', 'allow_reuse_address', 'close_request', 'fileno', 'finish_request', 'get_request', 'handle_error', 'handle_request', 'handle_timeout', 'process_request', 'request_queue_size', 'serve_forever', 'server_activate', 'server_address', 'server_bind', 'server_close', 'server_name', 'server_port', 'shutdown', 'shutdown_request', 'socket', 'socket_type', 'timeout', 'verify_request'] (Pdb) self.server.foo *** AttributeError: HTTPServer instance has no attribute 'foo'