Python 如何使用非阻塞套接字

Python 如何使用非阻塞套接字,python,python-3.x,sockets,blocking,nonblocking,Python,Python 3.x,Sockets,Blocking,Nonblocking,我正在尝试编写非阻塞服务器/客户端脚本 首先,, 这是我的密码: Server.py-> import socket import select import threading class ChatServer(threading.Thread): """ SERVER THREAD """ MAX_WAITING_CONNECTION = 10 RECV_HEADER_LENGTH = 10 def __init__(self, host

我正在尝试编写非阻塞服务器/客户端脚本

首先,, 这是我的密码:

Server.py->

import socket
import select
import threading

class ChatServer(threading.Thread):
    """
    SERVER THREAD
    """

    MAX_WAITING_CONNECTION = 10
    RECV_HEADER_LENGTH = 10

    def __init__(self, host, port):
        """
        Initialize new ChatServer

        :param host: Binding Host IP
        :param port: Binding Port number
        """


        threading.Thread.__init__(self)
        self.host = host
        self.port = port
        self.connections = [] ## Will keep active client connections.
        self.clients = {}
        self.running = True

    def _bind_socket(self):
        """
        Creates the server socket and binds it to the given host and port
        """
        self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.server_socket.bind((self.host, self.port))
        self.server_socket.listen(self.MAX_WAITING_CONNECTION)
        self.connections.append(self.server_socket)

    def _send(self, sock, client_message):
        """
        Prefixes each message with a 4-byte length before sending.

        :param sock: the incoming sock
        :param msg: the massage to send
        """
        user = self.clients[sock]
        client_message = user['header'] + user['data'] + client_message['header'] + client_message['data']
        sock.send(client_message)

    def _receive(self, sock):
        try:
            ## Bytes type header
            message_header = sock.recv(self.RECV_HEADER_LENGTH)

            if not len(message_header):
                return False

            message_length = int(message_header.decode('utf-8').strip())
            ## Bytes type data
            return {"header": message_header, "data": sock.recv(message_length)}
        except Exception as e:
            print('exception occur')
            print(e)
            return False

    def _broadcast(self, sending_client_socket, client_message):
        """
        Breadcasts a message to all the clients different from both the server itself and
        the client sending the message.
        :param client_socket: the socket of the client sending the message
        :param client_message: the message to broadcast ({'header': <bytes header>, 'data': <bytes data message>})
        """

        for sock in self.clients:
            is_not_the_server = sock != self.server_socket
            is_not_the_client_sending = sock != sending_client_socket ## sending client socket

            if is_not_the_server and is_not_the_client_sending:
                try:
                    user = self.clients[sending_client_socket]
                    print(f"Type client_message: {type(client_message)}")
                    print(f"Type user: {type(user)}")
                    sending_message = user['header'] + user['data'] + client_message['header'] + client_message['data']
                    sock.send(sending_message)
                except socket.error:
                    ## handles a possible disconnection of client "sock" by ..
                    sock.close()
                    self.connections.remove(sock) ## removing sock form active connections.
                    del self.clients[sock]

    def _log(self, sock, message):
        user = self.clients[sock]
        print(f"Received message from {user['data'].decode('utf-8')}: {message['data'].decode('utf-8')}")

    def _run(self):
        """
        Actually runs the server.
        """
        while self.running:
            ## Get the list of sockets which are ready to be read through select non-blocking calls
            ## The select has a timeout of 60 seconds

            try:
                ready_to_read, ready_to_write, in_error = select.select(self.connections, [], self.connections)
            except socket.error as e:
                print(f"General Error: {e}")
                continue
            else:
                for sock in ready_to_read:
                    ## if socket is server socket.
                    if sock == self.server_socket:
                        try:
                            client_socket, client_address = self.server_socket.accept()
                        except socket.error as e:
                            print(f"General Error: {e}")
                            break
                        else:
                            user = self._receive(client_socket)
                            if user is False:
                                continue
                            self.connections.append(client_socket)
                            self.clients[client_socket] = user
                            print(f"Accepted new connection from {client_address[0]}:{client_address[1]}..")
                    else:
                        message = self._receive(sock) ## Get client message
                        if message is False:
                            print(f"Closed connection from {self.clients[sock]['data'].decode('utf-8')}")
                            self.connections.remove(sock)
                            del self.clients[sock]
                            continue

                        self._log(sock, message)
                        print(message)
                        self._broadcast(sock, message)

                for sock in in_error:
                    self.connections.remove(sock)
                    del self.clients[sock]
        self.stop()

    def run(self):
        """
        Given a host and a port, binds the socket and runs the server.
        """
        self._bind_socket()
        self._run()

    def stop():
        """
        Stops the server by setting the "running" flag before closing
        the socket connection.
        """
        self.running = False
        self.server_socket.close()


if __name__ == '__main__':

    _HOST = '127.0.0.1'
    _PORT = 6667

    chat_server = ChatServer(_HOST, _PORT)
    chat_server.start()
    chat_server.join()
还有我的客户。py->

import socket
import select
import errno
import threading
import sys

RECV_HEADER_LENGTH = 10

class ChatClient(threading.Thread):


    def __init__(self, host, port):
        """
        Initialize new ChatClient

        :param host: Connect Host IP
        :param port: Connect Port number
        """

        threading.Thread.__init__(self)
        self.host = host
        self.port = port
        self.username = input("Username: ")
        self.running = True

    def _send(self, sock, message):
        sock.send(message.encode('utf-8'))  

    def _connect(self):
        """
        Connecting to the ChatServer
        """
        self.client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.client_socket.connect((self.host, self.port))
        self.client_socket.setblocking(0)
        self.username_header = f"{len(self.username):<{RECV_HEADER_LENGTH}}"
        self._send(self.client_socket, self.username_header+self.username)

    def prompt(self) :
        sys.stdout.write(f"#{self.username}$ ")
        sys.stdout.flush()

    def _run(self):
        """
        Actually run client.
        """
        while self.running:
            reading_sockets, writing_sockets, exceptional_sockets = select.select([self.client_socket], [self.client_socket], [])
            for sock in reading_sockets:
                if sock == self.client_socket:
                    username_header = self.client_socket.recv(RECV_HEADER_LENGTH)
                    if not len(username_header):
                        print("Connection closed by the server.")
                        sys.exit()
                    username_length = int(username_header.decode("utf-8").strip())
                    username = self.client_socket.recv(username_length).decode("utf-8")
                    message_header = self.client_socket.recv(RECV_HEADER_LENGTH)
                    message_length = int(message_header.decode('utf-8').strip())
                    message = self.client_socket.recv(message_length).decode('utf-8')
                    print(f"#{username}$ {message}")
            for sock in writing_sockets:
                self.prompt()
                message = input()
                print(len(message))
                if not message:
                    continue
                message_header = f"{len(message):<{RECV_HEADER_LENGTH}}"
                self._send(sock, message_header+message)
        self.stop()

    def run(self):
        """
        Given a host and a port, binds the socket and runs the server.
        """
        self._connect()
        self._run()

    def stop():
        """
        Stops the server by setting the "running" flag before closing
        the socket connection.
        """
        self.running = False
        self.client_socket.close()


if __name__ == '__main__':

    _HOST = '127.0.0.1'
    _PORT = 6667

    chat_server = ChatClient(_HOST, _PORT)
    chat_server.start()
    chat_server.join()
现在我的问题出在client.py上了。在_run函数中,我对同一个套接字使用select reading_套接字和writing_套接字

当我运行这段代码时,为reading_套接字阻塞for循环。因为在for-loop中,我一直保持着我的外壳,即使是再次按摩也不会释放。 所以我想让wait用户输入,但同时读取其他消息并在shell上打印。 我正在使用python3.7。 我怎样才能做到这一点

所以我想让等待用户输入,但同时读取其他 消息和打印在shell上。我正在使用python3.7。我怎样才能达到目标 这个

当sys.stdin实际有用户输入可供您使用时,确保仅从sys.stdin读取;这样你的输入呼叫就不会被阻塞。通过将sys.stdin作为要选择的第一个参数中的套接字之一传递,可以实现这一点。注意:这在Windows下不起作用,因为微软认为他们的select实现不支持在stdin上进行选择。在Windows下,您将不得不使用一个单独的线程来执行从stdin的阻塞读取,以及某种类型的线程间消息传递,以将从stdin读取的数据返回到网络线程,这是一个巨大的难题

下面是我如何修改您的_runself方法以获得您想要在MacOS/X下测试的行为:

def _run(self):
    """
    Actually run client.
    """
    while self.running:
        reading_sockets, writing_sockets, exceptional_sockets = select.select([self.client_socket, sys.stdin], [], [])
        for sock in reading_sockets:
            if sock == self.client_socket:
                username_header = self.client_socket.recv(RECV_HEADER_LENGTH)
                if not len(username_header):
                    print("Connection closed by the server.")
                    sys.exit()
                username_length = int(username_header.decode("utf-8").strip())
                username = self.client_socket.recv(username_length).decode("utf-8")
                message_header = self.client_socket.recv(RECV_HEADER_LENGTH)
                message_length = int(message_header.decode('utf-8').strip())
                message = self.client_socket.recv(message_length).decode('utf-8')
                print(f"#{username}$ {message}")
            elif sock == sys.stdin:
               self.prompt()
               message = input()
               print(len(message))
               if not message:
                   continue
               message_header = f"{len(message):<{RECV_HEADER_LENGTH}}"
               self._send(self.client_socket, message_header+message)
    self.stop()
注意,我将sys.stdin添加到select调用的read sockets参数中,以便select在其数据准备好从stdin读取时返回,我还从write sockets参数中删除了self.client_socket,因为将它放在那里会导致select在self.client_socket有缓冲空间接受更多传出数据时立即返回,也就是说,它几乎一直都会立即返回,这将使您的事件循环旋转,并导致您的客户端程序使用接近100%的内核,而这不是您想要的

我还修改了从stdin读取的代码,使其仅在可读套接字为sys.stdin时调用,因为尝试从stdin读取是没有意义的,除非它提供了数据;最后,我还有你自己。\你在TCP套接字上发送呼叫发送,而不是尝试将字节发送回stdin,因为stdin是只读/输入的,所以向它发送字节没有任何意义