Vineeth
Vineeth

Reputation: 23

use of __init__ during inheritance in Python

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()

Upvotes: 2

Views: 975

Answers (2)

Jules Gagnon-Marchand
Jules Gagnon-Marchand

Reputation: 3801

BaseHTTPServer.BaseHTTPRequestHandler.__init__(self)

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

Upvotes: 0

Severin Pappadeux
Severin Pappadeux

Reputation: 20130

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

Upvotes: 1

Related Questions