Python套接字如何处理客户端意外关闭

2024-05-08 13:19:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我的python socket服务器监听,然后连接到一个客户端,该客户端将从用户发送不确定数量的字符串。然后,客户端可能会关闭或断开与服务器的连接。

这会导致错误。

[错误号10053]主机中的软件中止了已建立的连接。 或 [Errno 10054]远程主机强制关闭了现有连接

如何处理此事件,以便关闭连接并重新启动侦听重新连接的服务器?

Python服务器示例:

# Echo server program
import socket
import sys

HOST = None               # Symbolic name meaning all available interfaces
PORT = 7001              # Arbitrary non-privileged port
s = None

def startListening():
    print "starting to listen"

    for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,
                                  socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
        af, socktype, proto, canonname, sa = res
        try:
            s = socket.socket(af, socktype, proto)
        except socket.error as msg:
            s = None
            continue
        try:
            s.bind(sa)
            s.listen(1)
        except socket.error as msg:
            s.close()
            s = None
            continue
        break
    if s is None:
        print 'could not open socket'
        sys.exit(1)
    conn, addr = s.accept()
    print 'Connected by', addr
    while 1:
        data = conn.recv(1024)
        if not data: 
            break
        print data
        message = ""
        while not "quit" in message:
            message = raw_input('Say Something : ')
            conn.sendall(message)
        #conn.send("I got that, over!")

    conn.close()
    print "connection closed"

while 1:
    startListening()

python客户端示例:

# Echo client program
import socket
import sys

HOST = 'localhost'    # The remote host
PORT = 7001              # The same port as used by the server
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, socket.SOCK_STREAM):
    af, socktype, proto, canonname, sa = res
    try:
        s = socket.socket(af, socktype, proto)
    except socket.error as msg:
        s = None
        continue
    try:
        s.connect(sa)
    except socket.error as msg:
        s.close()
        s = None
        continue
    break
if s is None:
    print 'could not open socket'
    sys.exit(1)
s.sendall("Please send me some strings")
data = ""
while ("quit" not in data):
    data = s.recv(1024)
    print 'Received', repr(data)
s.close()

要重现此错误,请在一个命令窗口中运行服务器,在另一个命令窗口中运行客户端,然后关闭客户端窗口。


Tags: inimport服务器nonehost客户端dataport
3条回答

我认为当客户端意外关闭时,根据SIGPIPE,您应该处理该信号

只需关闭连接的一侧,然后准备再次接受新连接。

处理此问题的最佳方法是将服务器套接字的创建与accept/read/write循环分开:

create_server_socket()
while True:
    accept_new_connection();
    try:
        read_request();
        write_response()
    finally:
        close_connection()

添加try:except:around the data send and data recv in the server脚本似乎可以缓解此问题:

# Echo server program
import socket
import sys

HOST = None               # Symbolic name meaning all available interfaces
PORT = 7001              # Arbitrary non-privileged port
s = None

def startListening():
    print "starting to listen"

    for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,
                                  socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
        af, socktype, proto, canonname, sa = res
        try:
            s = socket.socket(af, socktype, proto)
        except socket.error as msg:
            s = None
            continue
        try:
            s.bind(sa)
            s.listen(1)
        except socket.error as msg:
            s.close()
            s = None
            continue
        break
    if s is None:
        print 'could not open socket'
        sys.exit(1)
    conn, addr = s.accept()
    print 'Connected by', addr
    while 1:
        try: 
            data = conn.recv(1024)
        except:
            print "cannot recieve data"
            break
        if not data: 
            break
        print data
        message = ""
        while not "quit" in message:
            message = raw_input('Say Something : ')
            try: 
                conn.sendall(message)
            except Exception as exc:
                #print exc # or something.
                print "message could not be sent"
                break
        #conn.send("I got that, over!")

    conn.close()
    print "connection closed"

while 1:
    startListening()

相关问题 更多 >