Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42880

Re: HTTPserver: how to access variables of a higher class?

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 <davea@davea.name>
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; 'skip:[ 20': 0.04; 'classes,': 0.05; 'attribute': 0.07; 'class,': 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; "'__doc__',": 0.16; '(pdb)': 0.16; 'attribute,': 0.16; 'breakpoint': 0.16; 'missing?': 0.16; 'new-style': 0.16; 'object):': 0.16; 'old-style': 0.16; 'skip:/ 60': 0.16; 'subject:access': 0.16; 'subject:class': 0.16; 'trivially': 0.16; 'wrote:': 0.18; "skip:' 30": 0.19; 'solution.': 0.20; '>>>': 0.22; '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; 'testing': 0.29; "we'd": 0.29; 'am,': 0.29; "doesn't": 0.30; "i'm": 0.30; 'work.': 0.31; "skip:' 10": 0.31; 'class': 0.32; 'figure': 0.32; 'quite': 0.32; 'url:python': 0.33; 'skip:_ 10': 0.34; 'maybe': 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; 'there': 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; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; 'skip:u 10': 0.60; 'dave': 0.60; 'is.': 0.60; 'serving': 0.60; 'tell': 0.60; 'lost': 0.61; "you're": 0.61; 'save': 0.62; "you'll": 0.62; 'more': 0.64; 'sample': 0.67; 'received:74.208': 0.68; "'foo'": 0.84; '127.0.0.1': 0.84; 'glance': 0.84; 'how.': 0.84; "it'd": 0.84; 'received:74.208.4.194': 0.84; 'angel': 0.91; '***': 0.95
Date Fri, 05 Apr 2013 23:19:35 -0400
From Dave Angel <davea@davea.name>
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 <as7paeFs5e9U1@mid.individual.net> <mailman.132.1365162863.3114.python-list@python.org> <as8uncF65m3U1@mid.individual.net>
In-Reply-To <as8uncF65m3U1@mid.individual.net>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Provags-ID V02:K0:Z1ZQj00mxN99n8YM+SpfqDPPVp+akr1WkUzqTzBQy+1 YstjvID5XdlA1Kt+/25TcK7QSUWodW4pFyERHwDAgnTuLopFfb ZU08qOFFYV7iew+lkPr6fFOSKSnAfhsMFXjZL/kQfrXW77EtmE OSyzIrAVU1Bzbj+ATsc8lJ/GveICJKQur9PLrf9hhV36lcidsT fC7tLMli3w6PtEp/Q1heuw8wFOUBEXUSDAKxekofYjnxA0H35R YQR19OYSXWgbhhxMyz/DjGCHXSiKPG/LoaJQqE6tm3Cg/sWMnl ucDNokqGpfgXP5qPRogoxuC1hxUSp6Pi67fAFtXyxJ7xMZJDA= =
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.181.1365218398.3114.python-list@python.org> (permalink)
Lines 91
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1365218398 news.xs4all.nl 6925 [2001:888:2000:d::a6]:60144
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:42880

Show key headers only | View raw


On 04/05/2013 05:41 PM, Tom P wrote:
> 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:
>>> <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.
>>
>> 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
> <BaseHTTPServer.HTTPServer instance at 0x7ff20dde3830>
> (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'
>

I did a quick scan of the page whose link I showed you above.  It 
doesn't say there what 'server' attribute actually is.  Sounds like you 
need to combine my suggestion with Dylan's.  Once your server class 
inherits from the HTTPServer class, there should be an attriute 'foo'. 
But my understanding is still quite superficial, so you'll have to 
continue the good testing you're already doing.


-- 
DaveA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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