Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder3.xlned.com!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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'classes,': 0.05; 'suppose': 0.07; 'sys': 0.07; 'variables': 0.07; 'http': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '"..."': 0.16; 'handler.': 0.16; 'new-style': 0.16; 'object):': 0.16; 'old- style': 0.16; 'subject:access': 0.16; 'subject:class': 0.16; 'wrote:': 0.18; 'solution.': 0.20; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'cc:2**0': 0.24; '>': 0.26; 'first,': 0.26; 'header:In-Reply-To:1': 0.27; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'class': 0.32; 'figure': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; "i'd": 0.34; "can't": 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'server': 0.38; 'skip:& 10': 0.38; 'skip:m 40': 0.38; 'pm,': 0.38; 'url:mail': 0.40; 'lost': 0.61; 'sample': 0.67; 'how.': 0.84 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=mime-version:x-received:x-originating-ip:in-reply-to:references :date:message-id:subject:from:to:cc:content-type:x-gm-message-state; bh=0Z8RIlEG5OvEnCSeiFlrrjKYV/dpeqovW1OExc8RaWY=; b=nRQI4fup8rCHMkkzgXM1ghkoJoifzA8UB1fDx5haJ3J8w7a7Kf4fFu/AF5UVVgT18q a3gE4hhMZ8J28mqaCudc07tZmANGCV2KLrriu4ifXDwOTq0AdmfmIYGVaESOk61xt5Yj 2L2UHbHSRNMELC6bXobNhW0jSXxCBd840cVQJilOphIJhMxMFuLRlTHoT2LUKBcIxKrf bGdFeL55IN/jPwJGQznTHWDohxW/Dl+ysDN18WR/Ref/HTobQVebe41+VgN79aQpX2o6 p+lFdR97G5KN0MKGKlCOjTmndDFDtMsftGlPLiqgstiHD9m8tvHpxgK6DDkijYibnGig 4nvw== MIME-Version: 1.0 X-Received: by 10.182.144.42 with SMTP id sj10mr7895804obb.66.1365164874187; Fri, 05 Apr 2013 05:27:54 -0700 (PDT) X-Originating-IP: [220.245.216.139] In-Reply-To: References: Date: Fri, 5 Apr 2013 22:27:54 +1000 Subject: Re: HTTPserver: how to access variables of a higher class? From: Dylan Evans To: Tom P Content-Type: multipart/alternative; boundary=14dae939916362e61b04d99c37b6 X-Gm-Message-State: ALoCoQlNRPb6Mp4jVuq8kWWQZKIHyAvjpH3EEV+QYNgT8zFphRFE/Iy+nQtP4HGtRuX5ofDMUyP1 Cc: python-list 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: 103 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365164884 news.xs4all.nl 6937 [2001:888:2000:d::a6]:34385 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42811 --14dae939916362e61b04d99c37b6 Content-Type: text/plain; charset=ISO-8859-1 On 05/04/2013 9:09 PM, "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. Consider inheriting HTTPServer in MyWebServer which is passed to the request handler. > -- > http://mail.python.org/mailman/listinfo/python-list --14dae939916362e61b04d99c37b6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable


On 05/04/2013 9:09 PM, "Tom P" <werotizy@freent.dd> wrote:<= br> >
> First, here's a sample test program:
> <code>
> import sys
> from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
>
> class MyRequestHandler(BaseHTTPRequestHandler, object):
> =A0 =A0 def do_GET(self):
> =A0 =A0 =A0 =A0 top_self =3D super(MyRequestHandler, self) # try to ac= cess MyWebServer instance
> =A0 =A0 =A0 =A0 self.send_response(200)
> =A0 =A0 =A0 =A0 self.send_header('Content-type', =A0 =A0 =A0 = =A0'text/html')
> =A0 =A0 =A0 =A0 self.end_headers()
> =A0 =A0 =A0 =A0 self.wfile.write("thanks for trying, but I'd = like to get at self.foo and self.bar")
> =A0 =A0 =A0 =A0 return
>
> class MyWebServer(object):
> =A0 =A0 def __init__(self):
> =A0 =A0 =A0 =A0 self.foo =3D "foo" =A0# these are what I wan= t to access from inside do_GET
> =A0 =A0 =A0 =A0 self.bar =3D "bar"
> =A0 =A0 =A0 =A0 self.httpd =3D HTTPServer(('127.0.0.1', 8000),= MyRequestHandler)
> =A0 =A0 =A0 =A0 sa =3D self.httpd.socket.getsockname()
> =A0 =A0 =A0 =A0 print "Serving HTTP on", sa[0], "port&q= uot;, sa[1], "..."
>
> =A0 =A0 def runIt(self):
> =A0 =A0 =A0 =A0 self.httpd.serve_forever()
>
> server =3D 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.

Consider inheriting HTTPServer in MyWebServer which is passe= d to the request handler.

> --
> http:/= /mail.python.org/mailman/listinfo/python-list

--14dae939916362e61b04d99c37b6--