TCP 聊天服务器 Python 3.4

TCP Chat server Python 3.4

我的客户端代码有错误。错误是,"The operation was attempted on something that is not a socket." 我该如何解决这个错误。我也知道我的输入还没有发送到服务器,如果你们有任何关于我如何完成的提示,我也会喜欢他们。谢谢!代码如下:

import socket, select, string, sys


def prompt() :
    sys.stdout.write('<You> ')
    sys.stdout.flush()

#main function
if __name__ == "__main__":

    host = "localhost"
    port = 5000

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.settimeout(2)

    # connect to remote host
    try :
        s.connect((host, port))
    except :
        print('Unable to connect')
        sys.exit()

    print('Connected to remote host. Start sending messages')
    prompt()
    input = input()

    while 1:
        socket_list = [sys.stdin, s]

        # Get the list sockets which are readable
        read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])

        for sock in read_sockets:
            #incoming message from remote server
            if sock == s:
                data = sock.recv(4096)
                if not data :
                    print('\nDisconnected from chat server')
                    sys.exit()
                else :
                    #print data
                    sys.stdout.write(data)
                    prompt()

            #user entered a message
            else :
                msg = sys.stdin.readline()
                s.send(msg)
                prompt()

服务器代码:

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(("0.0.0.0", PORT))
    server_socket.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()

我看不出有什么问题,所以我 运行 在我的 linux 框中输入了 python3.4 的代码,并且没有从服务器或客户端收到任何错误,并且它们已连接没有问题。我唯一能想到的就是你的 python 或 OS 版本,就像 tdelaney 建议的那样。我没有要测试的 windows 盒子,所以我无法确认它是否是 OS 特定的。

在 Microsoft Windows 上,python 的套接字库是使用 Winsock 实现的。 Winsock 没有像 unixy 系统上的套接字库那样集成到操作系统中。它对什么是套接字有自己的想法,并且不识别系统管道或控制台。

在 Windows 上你得到你看到的错误

>>> import platform
>>> print platform.system()
Windows
>>> import socket
>>> import select
>>> import sys
>>> select.select([sys.stdin],[],[])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
select.error: (10038, 'An operation was attempted on something that is not a socket')
>>> select.select([sys.stdin.fileno()],[],[])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
select.error: (10038, 'An operation was attempted on something that is not a socket')
>>>

在 linux 上有效

>>> import platform                                                                                                                         
>>> print platform.system()
Linux                                                                                                                                       
>>> import socket
>>> import select
>>> import sys
>>> select.select([sys.stdin],[],[],0)
([], [], [])
>>> 
Use input = [s]  #instead of socket_list = [sys.stdin, s]