Python Socket WebChat,我不了解Select.select()及其工作原理

时间:2015-04-05 10:04:00

标签: python sockets select web-chat

我不想让这篇文章变得很长,所以我直接找到了我不理解的部分

这是我从here获得的代码 我做了我自己的应用程序,但我做了最糟糕的方式!

# Tcp Chat server

import socket, select

#Function to broadcast chat messages to all connected clients
def broadcast_data (sock, message):
    #Do not send the message to master socket and the client who has send us the message
    for socket in CONNECTION_LIST:
        if socket != server_socket and socket != sock :
            try :
                socket.send(message)
            except :
                # broken socket connection may be, chat client pressed ctrl+c for example
                socket.close()
                CONNECTION_LIST.remove(socket)

if __name__ == "__main__":

    # List to keep track of socket descriptors
    CONNECTION_LIST = []
    RECV_BUFFER = 4096 # Advisable to keep it as an exponent of 2
    PORT = 5000

    server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # this has no effect, why ?
    server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    server_socket.bind(('', PORT))
    6.listen(10)

    # Add server socket to the list of readable connections
    CONNECTION_LIST.append(server_socket)

    print "Chat server started on port " + str(PORT)

    while 1:
        # Get the list sockets which are ready to be read through select
        read_sockets,write_sockets,error_sockets = select.select(CONNECTION_LIST,[],[])

        for sock in read_sockets:
            #New connection
            if sock == server_socket:
                # Handle the case in which there is a new connection recieved through server_socket
                sockfd, addr = server_socket.accept()
                CONNECTION_LIST.append(sockfd)
                print "Client (%s, %s) connected" % addr

                broadcast_data(sockfd, "[%s:%s] entered room\n" % addr)

            #Some incoming message from a client
            else:
                # Data recieved from client, process it
                try:
                    #In Windows, sometimes when a TCP program closes abruptly,
                    # a "Connection reset by peer" exception will be thrown
                    data = sock.recv(RECV_BUFFER)
                    if data:
                        broadcast_data(sock, "\r" + '<' + str(sock.getpeername()) + '> ' + data)                

                except:
                    broadcast_data(sock, "Client (%s, %s) is offline" % addr)
                    print "Client (%s, %s) is offline" % addr
                    sock.close()
                    CONNECTION_LIST.remove(sock)
                    continue

    server_socket.close()

我不明白使用select的部分并添加新的连接,每次应用尝试连接时,猜测(myport,myip)都会创建一个新的描述符,并且此方法会尝试得到连接()ed人?你能解释一下吗? 我不知道为什么服务器套接字附加到套接字以及为什么传递给选择!我已经阅读了this,但我仍然不明白

我也不明白为什么if sock == server_socket:意味着有一个新的联系,我认为它是因为我还没有理解选择

1 个答案:

答案 0 :(得分:1)

Select()(就像C中的select()函数)有些神奇。它使您可以等待事件,而无需占用计算机的CPU。

在您的情况下,事件是套接字的输入和输出操作。想象一下,你在关门前等待(&#39;你是CPU)。如果可以做一些有用的事情,那么(正确的)门会打开。每扇门都通向一个I / O操作,该操作被添加到CONNECTION_LIST。

等待时,CPU不会花时间在你的程序上。无论哪个门首先打开,都会唤醒CPU,并让您处理数据。

注意:在Linux中,select()适用于所有文件操作(例如,在本例中为套接字)。在Windows中,这仅适用于套接字。

注意2:虽然套接字工作正常,但我更喜欢回调机制,例如来自gio库的iowatch(glib的一部分)。这些将在需要操作时调用函数。