Aluniteシ
Aluniteシ

Reputation: 3

How to resolve "typeError: __init__() takes exactly 3 arguments (2 given)"

I'm working on a Minecraft server client bypasses but there's some errors that I don't know how to fix. The error says:

typeError: __init__() takes exactly 3 arguments (2 given)

I can't seems to find the issue. I already search everywhere for tutorials how to fix this, but can't find any.

Java balls lol ok new word ?? ok why

Here is my code:

import socket
import select
import time
import sys

buffer_size = 4096
delay = 0.0001
forward_to = ('domain.tld', 25565)

class Forward:
    def __init__(self):
        self.forward = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    def start(self, host, port):
        try:
            self.forward.connect((host, port))
            return self.forward
        except Exception, e:
            print e
            return False

class TheServer:
    input_list = []
    channel = {}

    def __init__(self, host, port):
        self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server.bind((host, port))
        self.server.listen(200)

    def main_loop(self):
        self.input_list.append(self.server)
        while 1:
            time.sleep(delay)
            ss = select.select
            inputready, outputready, exceptready = ss(self.input_list, [], [])
            for self.s in outputready:
                if self.s == self.server:
                    self.on_accept()
                    break

                self.data = self.s.recv(buffer_size)
                if len(self.data) == 0:
                    self.on_close()
                    break
                else:
                    self.on_recv()

    def on_accept(self):
        forward = Forward().start(forward_to)[0], forward_to[1]
        clientsock, clientaddr = self.server.accept()
        if forward:
            print clientaddr, "You have connected"
            self.input_list.append(clientsock)
            self.input_list.append(forward)
            self.channel[clientsock] = forward
            self.channel[forwar] = clientsock
        else:
            print "Cannot connect to remote server."
            print "Closing connection with the client side."
            clientsock.close()

    def on_close(self):
        print self.s.getpeername(), "It has disconnected"

        self.input_list.remove(self.s)
        self.input_list.remove(self.channel[self.s])
        out = self.channel[self.s]

        self.channel[out].close()

        self.channel[self.s].close()

        del self.channel[out]
        del self.channel[self.s]

    def on_recv(self):
        data = self.data

        print data
        self.channel[self.s].send(data)

if __name__ == '__main__':
        server = TheServer('localhost, 12345')
        try:
            server.main_loop()
        except KeyboardInterrupt:
            print "CTRL C - Close The Server"
            sys.exit(1)

I can't seems to find the issue. I already search everywhere for tutorials how to fix this, but can't find any. I can't seems to find the issue. I already search everywhere for tutorials how to fix this, but can't find any.

Upvotes: 0

Views: 1073

Answers (1)

Croves
Croves

Reputation: 400

When you execute server = TheServer('localhost, 12345'), you are calling the constructor (the __init__ function) of the class TheServer. Take a look at this part of the code:

def __init__(self, host, port):
    self.server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    self.server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSADOR, 1)
    self.server.bind((host, port))
    self.server.listen(200)

You see 3 parameters: self, host and port. The first argument of every class method is always a reference to the current instance of the class. The other two are mandatory parameters that you must provide to the constructor when instanciating the class.

The problem is, in your call server = TheServer('localhost, 12345') you passed just one argument: a string 'localhost, 12345' so your class understands that the host is 'localhost, 12345' and the port is missing. That's the error: the class was expecting 3 parameters, but you gave just two. Replace the line with

server = TheServer('localhost', '12345')

Upvotes: 1

Related Questions