HTTPserver: how to access variables of a higher class?

Discussion in 'Python' started by Tom P, Apr 5, 2013.

  1. Tom P

    Tom P Guest

    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.
     
    Tom P, Apr 5, 2013
    #1
    1. Advertising

  2. Tom P

    Dave Angel Guest

    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
     
    Dave Angel, Apr 5, 2013
    #2
    1. Advertising

  3. Tom P

    Dylan Evans Guest

    On 05/04/2013 9:09 PM, "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.

    Consider inheriting HTTPServer in MyWebServer which is passed to the
    request handler.

    > --
    > http://mail.python.org/mailman/listinfo/python-list
     
    Dylan Evans, Apr 5, 2013
    #3
  4. Tom P

    Tom P Guest

    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:
    >> <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

    >
     
    Tom P, Apr 5, 2013
    #4
  5. Tom P

    Tom P Guest

    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
    >
     
    Tom P, Apr 5, 2013
    #5
  6. Tom P

    Tom P Guest

    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'
     
    Tom P, Apr 5, 2013
    #6
  7. Tom P

    Tom P Guest

    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:
    >> <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)
     
    Tom P, Apr 6, 2013
    #7
  8. Tom P

    Tom P Guest

    Re: The SOLUTION HTTPserver: how to access variables of a higherclass

    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
     
    Tom P, Apr 6, 2013
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Krusty
    Replies:
    0
    Views:
    1,089
    Krusty
    Sep 5, 2003
  2. WmGill
    Replies:
    0
    Views:
    310
    WmGill
    Nov 5, 2003
  3. Will Stuyvesant

    HTTP Location: supported by HTTPServer?

    Will Stuyvesant, Dec 4, 2003, in forum: Python
    Replies:
    0
    Views:
    337
    Will Stuyvesant
    Dec 4, 2003
  4. Erik Johnson
    Replies:
    1
    Views:
    415
    Donn Cave
    Aug 17, 2004
  5. Replies:
    0
    Views:
    603
Loading...

Share This Page