Python服务器名称错误:全局名称'套接字错误'未定义

时间:2016-04-24 13:17:28

标签: python kivy

我想在服务器和客户端之间建立连接,因此服务器会向客户端发送一个字符串。

以下是服务器:

import socket

def Main():
    host = '190.176.141.23'#ip changed
    port = 12345

    while True:

        s = socket.socket()
        s.bind((host,port))

        s.listen(1)
        c, addr = s.accept()
        print "Connection from: " + str(addr)


        command = c.recv(1024)

        if command == 'GIVETEXT':
            c.send('test')

        try:
            c.close()
            sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        except SocketError as e:
            if e.errno != errno.ECONNRESET:
                raise
            pass

if __name__ == '__main__':
    Main()

以下是我制作的客户:

import socket

class Client(object):

    def __init__(self, *args):
        self.s = socket.socket()

    def sent(self, host, port):
        self.s.connect((host, port))
        self.s.send('GIVETEXT')
        self.Text = self.s.recv(1024)
        print self.Text
        self.s.close
        return self.Text

毋庸置疑,我在另一段代码中执行了该方法,并且它有效。但之后服务器崩溃了,错误信息是:

NameError: global name 'SocketError' is not defined

1 个答案:

答案 0 :(得分:3)

socket.error;不是SocketError。将您的except行更改为:

except socket.error as e: