Python聊天不工作os错误10038

时间:2018-02-26 08:55:49

标签: python sockets chat

我的服务器正常启动,但是当我在该端口上运行客户端时,它无法正常工作。这是我的客户端错误代码:

OSError:[WinError 10038]尝试对非套接字

的操作进行操作

这是客户端的脚本:

chat_client.py

import sys
 import socket
import select

def chat_client():
    if(len(sys.argv) < 3) :
        print ('Usage : python chat_client.py hostname port')
        sys.exit()

    host = sys.argv[1]
    port = int(sys.argv[2])

    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. You can start sending messages')
    sys.stdout.write('[Me] '); sys.stdout.flush()

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

        # Get the list sockets which are readable
        ready_to_read,ready_to_write,in_error = select.select(socket_list , 
[], [])

    for sock in ready_to_read:             
        if sock == s:
            # incoming message from remote server, s
            data = sock.recv(4096)
            if not data :
                print ('\nDisconnected from chat server')
                sys.exit()
            else :
                #print data
                sys.stdout.write(data)
                sys.stdout.write('[Me] '); sys.stdout.flush()     

        else :
            # user entered a message
            msg = sys.stdin.readline()
            s.send(msg)
            sys.stdout.write('[Me] '); sys.stdout.flush() 

if __name__ == "__main__":

sys.exit(chat_client())

0 个答案:

没有答案