HTTPserver: how to access variables of a higher class?

Dylan Evans dylan at dje.me
Sat Apr 6 21:51:27 EDT 2013


On Sun, Apr 7, 2013 at 1:05 AM, Tom P <werotizy at freent.dd> wrote:

> On 04/05/2013 02:27 PM, Dylan Evans wrote:
>
>> On 05/04/2013 9:09 PM, "Tom P" <werotizy at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20130407/ee059403/attachment.html>


More information about the Python-list mailing list