修复以下错误名称“ self”未定义

时间:2019-05-25 04:18:21

标签: python

这是我遇到的错误。我怎样才能解决此问题而又不会收到该错误?它在Python中

def __init__(self, config):
    # Shutdown on Ctrl+C
    signal.signal(signal.SIGINT, self.shutdown)

    # Create a TCP socket
    self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Re-use the socket
    self.serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    # bind the socket to a public host, and a port
    self.serverSocket.bind((config['HOST_NAME'], config['BIND_PORT']))

    self.serverSocket.listen(10) # become a server socket
    self.__clients = {}

while True:

    # Establish the connection
    (clientSocket, client_address) = self.serverSocket.accept()

    d = threading.Thread(name=self._getClientName(client_address),
                         target = self.proxy_thread, args=(clientSocket, client_address))
    d.setDaemon(True)
    d.start()

1 个答案:

答案 0 :(得分:0)

这是一个简单的缩进错误。 self在本地__init__()范围内定义,但您的while True块不在此范围内。只需缩进即可:

def __init__(self, config):
    # Shutdown on Ctrl+C
    signal.signal(signal.SIGINT, self.shutdown)

    # Create a TCP socket
    self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    # Re-use the socket
    self.serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

    # bind the socket to a public host, and a port
    self.serverSocket.bind((config['HOST_NAME'], config['BIND_PORT']))

    self.serverSocket.listen(10) # become a server socket
    self.__clients = {}

    while True:

        # Establish the connection
        (clientSocket, client_address) = self.serverSocket.accept()

        d = threading.Thread(name=self._getClientName(client_address),
                             target = self.proxy_thread, args=(clientSocket, client_address))
        d.setDaemon(True)
        d.start()