Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #42964
| 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 | <dylan@dje.me> |
| 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 | <asartjFisjrU1@mid.individual.net> |
| References | <as7paeFs5e9U1@mid.individual.net> <mailman.135.1365164884.3114.python-list@python.org> <asartjFisjrU1@mid.individual.net> |
| Date | Sun, 7 Apr 2013 11:51:27 +1000 |
| Subject | Re: HTTPserver: how to access variables of a higher class? |
| From | Dylan Evans <dylan@dje.me> |
| Cc | python-list <python-list@python.org> |
| 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 <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.221.1365299497.3114.python-list@python.org> (permalink) |
| 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 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On Sun, Apr 7, 2013 at 1:05 AM, Tom P <werotizy@freent.dd> wrote:
> On 04/05/2013 02:27 PM, Dylan Evans wrote:
>
>> On 05/04/2013 9:09 PM, "Tom P" <werotizy@freent.dd> wrote:
>>
>>>
>>> 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.
>>
>> Consider inheriting HTTPServer in MyWebServer which is passed to the
>> request handler.
>>
>> --
>>> http://mail.python.org/**mailman/listinfo/python-list<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<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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
HTTPserver: how to access variables of a higher class? Tom P <werotizy@freent.dd> - 2013-04-05 13:02 +0200
Re: HTTPserver: how to access variables of a higher class? Dave Angel <davea@davea.name> - 2013-04-05 07:54 -0400
Re: HTTPserver: how to access variables of a higher class? Tom P <werotizy@freent.dd> - 2013-04-05 15:26 +0200
Re: HTTPserver: how to access variables of a higher class? Tom P <werotizy@freent.dd> - 2013-04-05 23:41 +0200
Re: HTTPserver: how to access variables of a higher class? Dave Angel <davea@davea.name> - 2013-04-05 23:19 -0400
Re: HTTPserver: how to access variables of a higher class? Dylan Evans <dylan@dje.me> - 2013-04-05 22:27 +1000
Re: HTTPserver: how to access variables of a higher class? Tom P <werotizy@freent.dd> - 2013-04-05 15:21 +0200
Re: HTTPserver: how to access variables of a higher class? Tom P <werotizy@freent.dd> - 2013-04-06 17:05 +0200
Re: HTTPserver: how to access variables of a higher class? Dylan Evans <dylan@dje.me> - 2013-04-07 11:51 +1000
Re: The SOLUTION HTTPserver: how to access variables of a higher class Tom P <werotizy@freent.dd> - 2013-04-06 23:38 +0200
csiph-web