首页 > 解决方案 > 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

所以,这是与我的问题相关的所有信息,所以请解决我的问题并解释我做错了什么。提前致谢 :)

标签: python-3.xsockets

解决方案


如果您使用的是 Windows,文档会提到问题所在:

Windows 上的文件对象是不可接受的,但套接字是。在 Windows 上,底层的 select() 函数由 WinSock 库提供,并且不处理并非源自 WinSock 的文件描述符。

你不能stdin在 Windows 上等待。


推荐阅读