Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'classes,': 0.05; 'skip:\xa0 30': 0.05; '"the': 0.07; 'class,': 0.07; 'suppose': 0.07; 'sys': 0.07; 'tom': 0.07; 'variables': 0.07; '__init__': 0.09; 'classes.': 0.09; 'http': 0.09; 'style.': 0.09; 'type,': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; '2.7': 0.14; '"..."': 0.16; "%s'": 0.16; 'command,': 0.16; 'handler.': 0.16; 'it."': 0.16; 'it."': 0.16; 'new-style': 0.16; 'object):': 0.16; 'old-style': 0.16; 'subject:access': 0.16; 'subject:class': 0.16; '\xa0def': 0.16; '\xa0print': 0.16; 'wrote:': 0.18; 'users.': 0.18; 'solution.': 0.20; '8bit%:5': 0.22; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; 'print': 0.22; 'error': 0.23; 'skip:\xa0 20': 0.24; 'cc:2**0': 0.24; 'define': 0.26; 'first,': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'unix': 0.29; "doesn't": 0.30; '8bit%:3': 0.30; 'andrew': 0.30; 'message-id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'getting': 0.31; "skip:' 10": 0.31; '8bit%:2': 0.31; 'initiate': 0.31; 'workaround': 0.31; 'class': 0.32; 'figure': 0.32; 'url:python': 0.33; '"the': 0.34; 'skip:_ 10': 0.34; "i'd": 0.34; 'problem': 0.35; "can't": 0.35; 'classes': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'words,': 0.36; 'url:listinfo': 0.36; 'method': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'being': 0.38; 'server': 0.38; 'skip:& 10': 0.38; '8bit%:4': 0.38; 'skip:m 40': 0.38; 'pm,': 0.38; '8bit%:6': 0.40; 'url:mail': 0.40; 'lost': 0.61; 'sample': 0.67; 'nobody': 0.68; "'foo'": 0.84; 'how.': 0.84; 'received:mail-ob0-x22f.google.com': 0.84; 'to:none': 0.92; '2013': 0.98 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:cc:content-type:x-gm-message-state; bh=eN4Fjfc4SVkeq8Uv4kuTzj77hSYI9q6wMxXNvveN9i4=; b=EAppk6EKiu8ns+P+/RWuJCFTKuYEjR6mmU9xAdbUOTyCIf4QgRtFwBk/j99GoDINv2 pVaHTj59G8/hbVT6gIjP9JCFUElMKKix71AIx0oknhPBo3XhNsFiO5NFNGUMYO3FQ2+p 0UXZcxqLFvYMpCmbW9xVgB8/28J4swx0j5r51jVHmC+SAW6jquDYk/SmMxQ6Rt8VSek0 lb/eLsTIrJMfZp8gjW7taQnk7O/626gB7RheOn0wfwRzipaerdV15+reFXaklAdl4UvJ l2jtNDO4/F0yLqaG8f6xNLyckcy3lECPeqm1uUMi5GTWWWwY9NZREZ0svMqkZVaEcvuI fXRA== MIME-Version: 1.0 X-Received: by 10.182.160.106 with SMTP id xj10mr11666580obb.98.1365299488030; Sat, 06 Apr 2013 18:51:28 -0700 (PDT) X-Originating-IP: [220.245.216.139] In-Reply-To: References: Date: Sun, 7 Apr 2013 11:51:27 +1000 Subject: Re: HTTPserver: how to access variables of a higher class? From: Dylan Evans Cc: python-list Content-Type: multipart/alternative; boundary=f46d04478905ff1c6804d9bb8ed6 X-Gm-Message-State: ALoCoQl+c3KpIjUXm8U5tJmj+v9TjJpiBZbEwXSjuY+Jv3nXyXhzaLmbWt5X89JhsqmGD+gw0+BI 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: 254 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1365299497 news.xs4all.nl 6954 [2001:888:2000:d::a6]:36501 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:42964 --f46d04478905ff1c6804d9bb8ed6 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Apr 7, 2013 at 1:05 AM, Tom P wrote: > On 04/05/2013 02:27 PM, Dylan Evans wrote: > >> 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 >>> >> >> > I keep getting the same problem - if inherit from any of these classes in > BaseHTTPServer and try to use super(class, self) to initiate the higher > class, I get the error "TypeError: must be type, not classobj" - in other > words, these are old-style classes. > That means that in this call - > self.httpd = MyHTTPServer(('127.0.0.1', 8000), MyRequestHandler) > > there doesn't seem to be a way to define a > class MyHTTPServer(HTTPServer) > > > You can call the __init__ method on the class as a workaround for it being old style. This works on 2.7 from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler class MyRequestHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type', 'text/plain') self.end_headers() self.wfile.write('Got foo? %s' % self.server.foo) class MyWebServer(HTTPServer): def __init__(self): self.foo = 'foo' HTTPServer.__init__(self, ('127.0.0.1', 8000), MyRequestHandler) sa = self.socket.getsockname() print "Serving HTTP on", sa[0], "port", sa[1] def runit(self): self.serve_forever() server = MyWebServer() server.runit() > > > > -- > http://mail.python.org/**mailman/listinfo/python-list > -- "The UNIX system has a command, nice ... in order to be nice to the other users. Nobody ever uses it." - Andrew S. Tanenbaum --f46d04478905ff1c6804d9bb8ed6 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
On Sun, Apr 7, 2013 at 1:05 AM, Tom P <weroti= zy@freent.dd> wrote:
On 04/05/2013 02:27 PM, Dylan Evans wrot= e:
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 =A0def do_GET(self):
=A0 =A0 =A0 =A0 =A0top_self =3D super(MyRequestHandler, self) # try to acce= ss
MyWebServer instance
=A0 =A0 =A0 =A0 =A0self.send_response(200)
=A0 =A0 =A0 =A0 =A0self.send_header('Content-type', =A0 =A0 = =A0 =A0'text/html')
=A0 =A0 =A0 =A0 =A0self.end_headers()
=A0 =A0 =A0 =A0 =A0self.wfile.write("thanks for trying, but I'd li= ke to get at
self.foo and self.bar")
=A0 =A0 =A0 =A0 =A0return

class MyWebServer(object):
=A0 =A0 =A0def __init__(self):
=A0 =A0 =A0 =A0 =A0self.foo =3D "foo" =A0# these are what I want = to access from inside
do_GET
=A0 =A0 =A0 =A0 =A0self.bar =3D "bar"
=A0 =A0 =A0 =A0 =A0self.httpd =3D HTTPServer(('127.0.0.1', 8000), M= yRequestHandler)
=A0 =A0 =A0 =A0 =A0sa =3D self.httpd.socket.getsockname()
=A0 =A0 =A0 =A0 =A0print "Serving HTTP on", sa[0], "port&quo= t;, sa[1], "..."

=A0 =A0 =A0def runIt(self):
=A0 =A0 =A0 =A0 =A0self.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 passed to the
request handler.

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


I keep getting the same problem - if inherit from any of these classes in B= aseHTTPServer and try to use super(class, self) to initiate the higher clas= s, I get the error "TypeError: must be type, not classobj" - in o= ther words, these are old-style classes.
That means that in this call -
self.httpd =3D MyHTTPServer(('127.0.0.1', 8000), MyRequestHandler)<= br>
there doesn't seem to be a way to define a
=A0class MyHTTPServer(HTTPServer)<= br>


You can= call the __init__ method on the class as a workaround for it being old sty= le. This works on 2.7


from BaseH= TTPServer import HTTPServer, BaseHTTPRequestHandler

class MyRequestHandler(BaseHTTPRequestHandler):
=A0 =A0 def do_GET(self):
=A0 =A0 =A0 =A0 self.send_response(2= 00)
=A0 =A0 =A0 =A0 self.send_header('Content-type', '= ;text/plain')
=A0 =A0 =A0 =A0 self.end_headers()
=A0 =A0 =A0 =A0 self.wfil= e.write('Got foo? %s' % self.server.foo)

<= br>
class MyWebServer(HTTPServer):
=A0 =A0 def __init__= (self):
=A0 =A0 =A0 =A0 self.foo =3D 'foo'
=A0 =A0 =A0 =A0 HTTPSe= rver.__init__(self, ('127.0.0.1', 8000), MyRequestHandler)
=A0 =A0 =A0 =A0 sa =3D self.socket.getsockname()
=A0 =A0 =A0 = =A0 print "Serving HTTP on", sa[0], "port", sa[1]

=A0 =A0 def runit(self):
=A0 =A0 =A0 =A0 self= .serve_forever()

server =3D MyWebServer()
server.runit()
=A0



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



--
"The UNIX system has a command, nice ... in order to be nice to= the other users. Nobody ever uses it." - Andrew S. Tanenbaum
--f46d04478905ff1c6804d9bb8ed6--