Windows WinError 10038尝试对非套接字的对象执行操作
我试图用Python制作聊天服务器,但我无法解决它。我使用命令python client.py localhost 9009在CMD中运行代码 这是我正在使用的代码:Windows WinError 10038尝试对非套接字的对象执行操作,windows,python-3.x,sockets,Windows,Python 3.x,Sockets,我试图用Python制作聊天服务器,但我无法解决它。我使用命令python client.py localhost 9009在CMD中运行代码 这是我正在使用的代码: #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.
#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
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
for sock in read_sockets:
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())
这就是我得到的错误:
我不知道如何修理它。谢谢你的帮助
[Me] Traceback (most recent call last):
File "client.py", line 54, in <module>
sys.exit(chat_client())
File "client.py", line 32, in chat_client
read_sockets, write_sockets, error_sockets = select.select(socket_list , [],
[])
问题是,在Windows上,select仅支持套接字。你正试图在stdin上使用它。我对Python了解不够,无法就最佳替代方案提供建议,希望其他人能够解决这个问题。问题是,在Windows上,select仅支持套接字。你正试图在stdin上使用它。我对Python了解不多,无法就最佳替代方案提供建议,希望其他人能够解决这个问题。