1

I am trying to to print some simple variables using the built in HTTP server in Python

class WebServer:
    def __init__(self):
        from BaseHTTPServer import HTTPServer
        import urlparse
        server = HTTPServer(('', 8080), self.do_GET)
        server.serve_forever()

    def do_GET(self):
        parsed_path = urlparse.urlparse(self.path)
        message_parts = [
                'CLIENT VALUES:',
                'client_address=%s (%s)' % (self.client_address, self.address_string()),
                'command=%s' % self.command,
                'path=%s' % self.path,
                'real path=%s' % parsed_path.path,
                'query=%s' % parsed_path.query,
                'request_version=%s' % self.request_version,
                '',
                'SERVER VALUES:',
                'server_version=%s' % self.server_version,
                'sys_version=%s' % self.sys_version,
                'protocol_version=%s' % self.protocol_version,
                '',
                'HEADERS RECEIVED:',
                ]
        for name, value in sorted(self.headers.items()):
            message_parts.append('%s=%s' % (name, value.rstrip()))
        message_parts.append('')
        message = '\r\n'.join(message_parts)
        self.send_response(200)
        self.end_headers()
        self.wfile.write(message)
        return

But i seem to get this error:

Exception happened during processing of request from ('10.0.1.3', 52251)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
TypeError: do_GET() takes exactly 1 argument (4 given)
----------------------------------------

def do_GET(self): seems to be receiving more then just self, what am i missing?

4
  • Don't you need a HandlerClass? Commented Feb 5, 2013 at 18:12
  • isn't that self.do_GET in this case? Commented Feb 5, 2013 at 18:13
  • 1
    Take a look at the documentation for the module you're using :) you're passing in a function when a fully fledged object is expected docs.python.org/2/library/… Commented Feb 5, 2013 at 18:13
  • you could sublcass it and implement do_GET like in this example wiki.python.org/moin/BaseHttpServer Commented Feb 5, 2013 at 18:13

2 Answers 2

2

Inherit WebServer from a Request Handler like this:

class WebServer(BaseHTTPRequestHandler):

You will probably have to change your imports for that.

Then pass it as an argument to HTTPServer like this:

server = HTTPServer(('', 8080), WebServer)
Sign up to request clarification or add additional context in comments.

1 Comment

It works for me if I move server = HTTPServer(('', 8080), WebServer) server.serve_forever() after the definition of WebServer.
2

You're passing in a function, when a BaseHTTPRequestHandler is expected. In other words, Python is trying to instantiate your object using a BaseHTTPRequestHandler __init__ method, but you've provided a function that takes a different amount of arguments.

Instead of passing a plain function, sublcass BaseHTTPRequestHandler. The example linked by @dm03514 in the comments will get you started.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.