python-3.x - OSError: [WinError 10038] 尝试对非套接字的操作进行操作。为什么以及我哪里出错了?
问题描述
我正在使用python3中的套接字开发一个聊天应用程序,我有两个文件server.py和client.py,当我运行server.py时它运行得很好但是当我尝试使用client.py连接到server.py时它返回以下错误:
Traceback (most recent call last):
File "client.py", line 24, in <module>
read_sockets,write_socket, error_socket = select.select(sockets_list,[],[])
OSError: [WinError 10038] An operation was attempted on something that is not a socket
我做了一些研究并尝试了一些事情,比如改变一些东西,但也失败了。
两个文件的代码: server.py :
# Imports and initialization
import socket
from colorama import init, Fore, Style
import sys
import threading
init()
quit_msg = '!quit'
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Checking arguments and further code
if len(sys.argv) != 3:
print(Fore.RED + "[*] Correct usage: script, IP address(host), port number(room)")
sys.exit()
host = str(sys.argv[1])
port = int(sys.argv[2])
server.bind((host, port))
server.listen()
list_of_clients = []
def client_thread(conn , addr):
print(Fore.GREEN + "[+] Welcome to this room")
while True:
try:
message = conn.recv(2048)
if message:
print(Fore.YELLOW + f"[{addr[0]}" + Fore.GREEN + f"{message}")
message_to_send = Fore.YELLOW + f"[{addr[0]}" + Fore.GREEN + f"{message}"
brodcast(message_to_send, conn)
else:
remove(conn)
except:
continue
def brodcast(message, connection):
for clients in list_of_clients:
if clients != connection:
try:
clients.send(message)
except:
clients.close()
remove(clients)
def remove(connection):
if connection in list_of_clients:
list_of_clients.remove(connection)
while True:
conn, addr = server.accept()
list_of_clients.append(conn)
print(Fore.CYAN + f"[ {addr} ] connected")
thread = threading.Thread(target=client_thread, args=(conn, addr))
conn.close()
server.close()
客户端.py:
# Imports and intilization
import socket
import sys
import select
from colorama import init, Fore, Style
init()
quit_msg = '!quit'
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if len(sys.argv) != 3:
print(Fore.RED + "[*] Correct usage: script, IP address(host), port number(room)")
sys.exit()
host = str(sys.argv[1])
port = int(sys.argv[2])
client.connect((host, port))
while True:
sockets_list = [sys.stdin, client]
read_sockets,write_socket, error_socket = select.select(sockets_list,[],[])
for socks in read_sockets:
if socks == client:
message = socks.recv
print(message)
else:
message = sys.stdin.readline()
client.send(message)
sys.stdout.write("<You>")
sys.stdout.write(message)
sys.stdout.flush()
client.close()
命令: 在终端 1:
python3 server.py 127.0.0.1 8080
在终端 1 中运行命令后的输出:
nothing
在终端 2:
python3 client.py 127.0.0.1 8080
在终端 2 中运行命令后的输出:
Traceback (most recent call last):
File "client.py", line 24, in <module>
read_sockets,write_socket, error_socket = select.select(sockets_list,[],[])
OSError: [WinError 10038] An operation was attempted on something that is not a socket
而且我还注意到一件事,在终端 2 中运行命令后,它退出并出现错误,但在终端 1 中它返回:
[ ('127.0.0.1', 10703) ] connected
所以,这是与我的问题相关的所有信息,所以请解决我的问题并解释我做错了什么。提前致谢 :)
解决方案
如果您使用的是 Windows,文档会提到问题所在:
Windows 上的文件对象是不可接受的,但套接字是。在 Windows 上,底层的 select() 函数由 WinSock 库提供,并且不处理并非源自 WinSock 的文件描述符。
你不能stdin
在 Windows 上等待。
推荐阅读
- c# - C#:HashSet 的 volatile 读写
- amazon-web-services - 如何将 USERID 和 PHONENO 等 AWS Cognito 属性捕获到 MySQL 数据库中
- python - 一组元素的成对比较
- excel - 如何通过VBA控制excel图表源范围
- go - GCP => 监听 tcp :443: bind: 权限被拒绝
- javascript - 我应该如何在下拉列表中显示多个项目?
- castle-windsor - 如何在 AbstractFacility 中加载到容器 MassTransit EndPoint
- java - 试图在 Java Web 应用程序中实现 SQL 漏洞。Java + MySQL
- mapbox - Mapbox 是如何在加载地图之前加载此图像的?
- apache-spark - Spark-Java:如何将格式为“yyyy-MM-ddThh:mm:ss.SSS+0000”的数据集字符串列转换为格式的时间戳?