2

I am trying to build a simple HTTP server and am using BaseHTTPServer in python.

When ever I try to run the code below I get a error saying that init() takes exactly 4 arguments (1 given).

I guess the problem is that the constructor in handler is overriding the BaseHTTPServer.BaseHTTPRequest

Kindly let me know where I'm going wrong

class handler(BaseHTTPServer.BaseHTTPRequestHandler):
  def __init__(self,server):
    BaseHTTPServer.BaseHTTPRequestHandler.__init__(self)
    self.server = server
    self.port = 8080

def do_GET(self):
    #perform some operation
class server():

def __init__(self):
    self.port = 65531
    self.host = 'localhost'

def run(self):
    serverClass = BaseHTTPServer.HTTPServer
    server = "xyz.c1589.com"               # Some random server
    h = handler(server)
    server = serverClass((self.host,self.port),h)
    print "Starting server!!"
    try:
        server.serve_forever()
    except:
        print "Error Creating Server"
        server.server_close()


if __name__ == '__main__':
server().run()
2
  • BaseHTTPServer.BaseHTTPRequestHandler.__init__ takes three arguments and the subclass requires one, so when you instantiate a handler you need to pass four arguments. Commented Apr 18, 2015 at 2:04
  • When you are getting an error, you should post the complete traceback in your question. Commented Apr 18, 2015 at 2:06

2 Answers 2

1

The signature is BaseHTTPRequestHandler(request, client_address, server). Together with self it makes four arguments. You in __init__() provided only one

Sign up to request clarification or add additional context in comments.

Comments

0

BaseHTTPServer.BaseHTTPRequestHandler.__init__(self)

you need to put the args for the call to the base __init__ in there.

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.