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


Groups > comp.lang.python > #42802 > unrolled thread

HTTPserver: how to access variables of a higher class?

Started byTom P <werotizy@freent.dd>
First post2013-04-05 13:02 +0200
Last post2013-04-06 23:38 +0200
Articles 10 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  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

#42802 — HTTPserver: how to access variables of a higher class?

FromTom P <werotizy@freent.dd>
Date2013-04-05 13:02 +0200
SubjectHTTPserver: how to access variables of a higher class?
Message-ID<as7paeFs5e9U1@mid.individual.net>
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.

[toc] | [next] | [standalone]


#42806

FromDave Angel <davea@davea.name>
Date2013-04-05 07:54 -0400
Message-ID<mailman.132.1365162863.3114.python-list@python.org>
In reply to#42802
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

-- 
DaveA

[toc] | [prev] | [next] | [standalone]


#42817

FromTom P <werotizy@freent.dd>
Date2013-04-05 15:26 +0200
Message-ID<as81nsFu3flU1@mid.individual.net>
In reply to#42806
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
>
Yes, sorry for the omission.

> Is MyWebServer class intended to have exactly one instance?
Yes, but I was trying to keep it general.
  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

ok, let me test that.  Do I assume correctly from what you write that 
the super() is not needed?
  In reality there is just one instance of MyWebServer, but I was 
looking for a general solution.
>
> See http://docs.python.org/2/library/basehttpserver.html
>

[toc] | [prev] | [next] | [standalone]


#42851

FromTom P <werotizy@freent.dd>
Date2013-04-05 23:41 +0200
Message-ID<as8uncF65m3U1@mid.individual.net>
In reply to#42806
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'

[toc] | [prev] | [next] | [standalone]


#42880

FromDave Angel <davea@davea.name>
Date2013-04-05 23:19 -0400
Message-ID<mailman.181.1365218398.3114.python-list@python.org>
In reply to#42851
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

[toc] | [prev] | [next] | [standalone]


#42811

FromDylan Evans <dylan@dje.me>
Date2013-04-05 22:27 +1000
Message-ID<mailman.135.1365164884.3114.python-list@python.org>
In reply to#42802

[Multipart message — attachments visible in raw view] — view raw

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

[toc] | [prev] | [next] | [standalone]


#42815

FromTom P <werotizy@freent.dd>
Date2013-04-05 15:21 +0200
Message-ID<as81e9Fu028U1@mid.individual.net>
In reply to#42811
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.
>

That was the next thing I was going to try, thanks.

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

[toc] | [prev] | [next] | [standalone]


#42918

FromTom P <werotizy@freent.dd>
Date2013-04-06 17:05 +0200
Message-ID<asartjFisjrU1@mid.individual.net>
In reply to#42811
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
>

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)




[toc] | [prev] | [next] | [standalone]


#42964

FromDylan Evans <dylan@dje.me>
Date2013-04-07 11:51 +1000
Message-ID<mailman.221.1365299497.3114.python-list@python.org>
In reply to#42918

[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

[toc] | [prev] | [next] | [standalone]


#42953 — Re: The SOLUTION HTTPserver: how to access variables of a higher class

FromTom P <werotizy@freent.dd>
Date2013-04-06 23:38 +0200
SubjectRe: The SOLUTION HTTPserver: how to access variables of a higher class
Message-ID<asbiuiFo2c5U1@mid.individual.net>
In reply to#42802
On 04/05/2013 01:02 PM, Tom P wrote:

ok, after much experimenting it looks like the solution is as follows:

class MyWebServer(object):
     def __init__(self):
   #      self.foo = "foo"  delete these from self
    #     self.bar = "bar"
         myServer = HTTPServer
         myServer.foo = "foo"  #define foo,bar here
         myServer.bar = "bar"

         self.httpd = myServer(('127.0.0.1', 8000), MyRequestHandler)

Then, in the request handler:
class MyRequestHandler(BaseHTTPRequestHandler):
     def do_GET(self):
         ss=self.server
         print ss.foo

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web