Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'classes,': 0.05; 'attribute': 0.07; 'suppose': 0.07; 'sys': 0.07; 'tom': 0.07; 'variables': 0.07; 'http': 0.09; 'method,': 0.09; 'python': 0.11; 'def': 0.12; '2.7': 0.14; '"..."': 0.16; 'attribute,': 0.16; 'new- style': 0.16; 'object):': 0.16; 'old-style': 0.16; 'received:74.208.4.195': 0.16; 'subject:access': 0.16; 'subject:class': 0.16; 'trivially': 0.16; 'wrote:': 0.18; 'solution.': 0.20; 'import': 0.22; 'print': 0.22; 'header:User- Agent:1': 0.23; 'instance,': 0.24; 'server.': 0.24; 'looks': 0.24; 'class.': 0.26; 'first,': 0.26; 'header:In-Reply-To:1': 0.27; "we'd": 0.29; 'am,': 0.29; 'class': 0.32; 'figure': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; "i'd": 0.34; 'could': 0.34; "can't": 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'subject:?': 0.36; 'url:org': 0.36; 'should': 0.36; 'so,': 0.37; 'server': 0.38; 'skip:m 40': 0.38; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'tell': 0.60; 'lost': 0.61; 'save': 0.62; 'more': 0.64; 'sample': 0.67; 'received:74.208': 0.68; 'glance': 0.84; 'how.': 0.84; "it'd": 0.84 Date: Fri, 05 Apr 2013 07:54:06 -0400 From: Dave Angel User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:17.0) Gecko/20130308 Thunderbird/17.0.4 MIME-Version: 1.0 To: python-list@python.org Subject: Re: HTTPserver: how to access variables of a higher class? References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Provags-ID: V02:K0:MBNa4h10JSSNmGXqIlXK+uYz1NpZWYKqMp9+KJMkr4L MqTBMkXL4FCo0kUdWBgli0hxiMZfiHZGLTNhlqzs/r/3MavFpY erY/WIFgL852EHJ0ioZ15wbtX/bAO2OCzOc5FJGX+rDwA53dLt Jk3rfPh8KZGkT8o51VAf/ZCCBXGdbm6F34Qw1G96amCY0iJMFi YSKmQhfciFIwmaLt0is4SETyxp4FZvd2NSSoGzw1PWus2NflnQ ERDKF1h3WMWI3ej6UP36Z/E7N+jJxzePEGaXcMCoLKBC3eAY7U Hw+tSbbgrfXXMg9X8TGGA/yjtr53X1FoCv+R6OFRIKzz0guzg= = X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 54 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365162863 news.xs4all.nl 6847 [2001:888:2000:d::a6]:45673 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42806 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 -- DaveA