小编典典

TypeError:__init __()缺少2个必需的位置参数:“ client_socket”和“ statusMessage”

python

import socket
import sys

class SimpleClient:
    def __init__(self, client_socket, statusMessage):
        self.client_socket = client_socket
        self.statusMessage = statusMessage

    def connectToServer(self):
        self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

        host  = 'cs5700sp15.ccs.neu.edu'
        port  = 27993

        remote_ip = socket.gethostbyname(host)

        try:
            self.client_socket.connect((remote_ip, port))
        except socket.error:
            print ('Connection failed')
            sys.exit()

        print ('Connection successful')

    def sendHelloMessage(self):
        """This funtion sends the initial HELLO message to the server"""
        nu_id = input('Enter your NUID: ')
        hello_message = 'cs5700spring2015 HELLO {}\n'.format(nu_id)
        self.client_socket.send(bytes(hello_message, 'ascii'))

    def receiveStatusMessage(self):
        """This function receives the STATUS message from the server"""
        self.statusMessage = str(self.client_socket.recv(1024))
        print (self.statusMessage)

        #handleStatusMessage()

def main():
  client = SimpleClient()
  client.connectToServer()
  client.sendHelloMessage()
  client.receiveStatusMessage()

if __name__ == "__main__":main()

我收到以下错误:

Traceback (most recent call last):
  File "/Users/sanketdeshpande/Documents/workspace/test/project01-simpleclient.py", line 49, in <module>
    if __name__ == "__main__":main()
  File "/Users/sanketdeshpande/Documents/workspace/test/project01-simpleclient.py", line 44, in main
    client = SimpleClient()
TypeError: __init__() missing 2 required positional arguments: 'client_socket' and 'statusMessage'

阅读 148

收藏
2020-12-20

共1个答案

小编典典

class SimpleClient:
    def __init__(self, client_socket, statusMessage):

您的班级有两个参数,但是在您调用时;

client = SimpleClient()

您没有写任何参数。因此,您必须提出两个可能的论点None

2020-12-20