Python 如何在服务器套接字等待连接时关闭它?

Python 如何在服务器套接字等待连接时关闭它?,python,sockets,Python,Sockets,我有一个服务器程序,可以维护与多个客户端的连接。我希望能够关闭主服务器套接字(适用于所有人),以响应close by close by client消息,或者出于其他原因。问题是,服务器卡在accept()方法上,不关心是否在其他位置关闭套接字 我可以在主服务器中使用一些标志,然后在这段时间后关闭套接字,但是这意味着我必须在客户端请求后自己连接到服务器,以便检查while条件,这听起来像是非常糟糕的编程 守则: import socket import sys from thread impor

我有一个服务器程序,可以维护与多个客户端的连接。我希望能够关闭主服务器套接字(适用于所有人),以响应close by close by client消息,或者出于其他原因。问题是,服务器卡在accept()方法上,不关心是否在其他位置关闭套接字

我可以在主服务器中使用一些标志,然后在这段时间后关闭套接字,但是这意味着我必须在客户端请求后自己连接到服务器,以便检查while条件,这听起来像是非常糟糕的编程

守则:

import socket
import sys
from thread import *

HOST = ''   # Symbolic name meaning all available interfaces
PORT = 9992 # Arbitrary non-privileged port


s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'

#Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error , msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()

print 'Socket bind complete'

#Start listening on socket
s.listen(10)
print 'Socket now listening'

#Function for handling connections. This will be used to create threads
def clientthread(conn):
    #Sending message to connected client
    conn.send('Welcome to the server. Type something and hit enter\r\n') #send only takes string
    data=''
    #infinite loop so that function do not terminate and thread do not end.
    while True:

        #Receiving from client
        data += conn.recv(1024)
        print data
        if data == 'CLOSE':
            global s
            conn.sendall('You have requested to destroy the connection...')
            conn.close()
            s.close()
            return
        if data.find('\n') != -1:
            conn.sendall('OK...' + data + '\r\n')
            data=''






#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call 
    try:
        conn, addr = s.accept()
        print 'Connected with ' + addr[0] + ':' + str(addr[1])

        #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
        start_new_thread(clientthread ,(conn,))
    except:
        print 'socket issue sorry'
        break

这就是Python标准库为您提供帮助的地方。检查模块,或

通常没有理由不使用Python异步,因为它非常简单。作为奖励,您的代码将更具可读性和可维护性

您的代码与
asyncore

import asyncore
import socket
import sys

HOST = ''   # Symbolic name meaning all available interfaces
PORT = 9992 # Arbitrary non-privileged port

class ExampleHandler(asyncore.dispatcher_with_send):
    data = ''

    def handle_read(self):
        self.data += self.recv(1024)
        lines = self.data.split('\n')
        if len(lines) > 1:
            self.data = lines[-1]
            for line in lines:
                if not line: continue
                if line == 'CLOSE':
                    global s
                    self.send('You have requested to destroy the connection...\r\n')
                    self.close()
                    # To exit asyncore.loop() immediately,
                    # closing all client connections
                    raise asyncore.ExitNow()
                    # If you want to finish processing the client connections, you
                    # could instead close the server, 
                    server.close()
                else:
                    self.send('OK...%s\r\n' % line)

    def handle_write(self):
        self.send('Welcome to the server. Type something and hit enter\r\n')

class ExampleServer(asyncore.dispatcher):

    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind((host, port))
        self.listen(5)

    def handle_accept(self):
        pair = self.accept()
        if pair is not None:
            sock, addr = pair
            print 'Connected with ' + addr[0] + ':' + str(addr[1])
            handler = ExampleHandler(sock)
        else:
            print 'socket issue sorry'

server = ExampleServer(HOST, PORT)
try:
    asyncore.loop()
except asyncore.ExitNow:
    pass

注意:我还通过添加行缓冲区修复了原始代码中的字符串/缓冲问题。

因此主线程在
s.accept()处阻塞。
。如果在套接字上调用
close()?我认为
accept()
应该会失败返回。@JonathonReinhart-它在我使用过的所有TCP堆栈上都会出现。你可以在这里尝试,关闭()后服务器仍在等待新的连接。一个小片段和/或示例的支持链接将使这个不错的答案成为一个很好的答案。