[英]Python Server NameError: global name 'SocketError' is not defined
I wanted to make a connection between a sever and a client, so the server sends a string to the client. 我想在服务器和客户端之间建立连接,因此服务器将字符串发送到客户端。
Here is the Server: 这是服务器:
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()
And here is the Client I made: 这是我的客户:
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
Needless to say, that I executed the method in another piece of code, and it worked. 不用说,我在另一段代码中执行了该方法,并且它起作用了。 But after that the server crashed, with the error message: 但是之后,服务器崩溃了,并显示错误消息:
NameError: global name 'SocketError' is not defined
It is socket.error
; 这是socket.error
; not SocketError
. 不是SocketError
。 Change your except
line to: 将您的except
行更改为:
except socket.error as e:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.