简体   繁体   English

可以选择()与Windows下的Python文件一起使用吗?

[英]Can select() be used with files in Python under Windows?

I am trying to run the following python server under windows: 我试图在Windows下运行以下python服务器:

"""
An echo server that uses select to handle multiple clients at a time.
Entering any line of input at the terminal will exit the server.
"""

import select
import socket
import sys

host = ''
port = 50000
backlog = 5
size = 1024
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host,port))
server.listen(backlog)
input = [server,sys.stdin]
running = 1
while running:
    inputready,outputready,exceptready = select.select(input,[],[])

    for s in inputready:

        if s == server:
            # handle the server socket
            client, address = server.accept()
            input.append(client)

        elif s == sys.stdin:
            # handle standard input
            junk = sys.stdin.readline()
            running = 0

        else:
            # handle all other sockets
            data = s.recv(size)
            if data:
                s.send(data)
            else:
                s.close()
                input.remove(s)
server.close() 

I get the error message (10038, 'An operation was attempted on something that is not a socket'). 我收到错误消息(10038,'尝试对非套接字的操作')。 This probably relates back to the remark in the python documentation that "File objects on Windows are not acceptable, but sockets are. On Windows, the underlying select() function is provided by the WinSock library, and does not handle file descriptors that don't originate from WinSock.". 这可能与python文档中的说法有关 ,即“Windows上的文件对象是不可接受的,但是套接字是。在Windows上,底层的select()函数由WinSock库提供,并且不处理不能使用的文件描述符”。来自WinSock。“ On internet there are quite some posts on this topic, but they are either too technical for me or simply not clear. 在互联网上有很多关于这个主题的帖子,但它们对我来说太技术或者根本不清楚。 So my question is: is there any way the select() statement in python can be used under windows? 所以我的问题是:有没有什么方法可以在Windows下使用python中的select()语句? Please add a little example or modify my code above. 请添加一些示例或修改上面的代码。 Thanks! 谢谢!

Look like it does not like sys.stdin 看起来它不喜欢sys.stdin

If you change input to this 如果您将输入更改为此

input = [server] 

the exception will go away. 例外情况将会消失。

This is from the doc 这是来自doc

 Note:
    File objects on Windows are not acceptable, but sockets are. On Windows, the
 underlying select() function is provided by the WinSock library, and does not 
handle file descriptors that don’t originate from WinSock.

I don't know if your code has other problems, but the error you're getting is because of passing input to select.select() , the problem is that it contains sys.stdin which is not a socket. 我不知道你的代码是否有其他问题,但你得到的错误是因为将input传递给select.select() ,问题是它包含的sys.stdin不是套接字。 Under Windows, select only works with sockets. 在Windows下, select仅适用于套接字。

As a side note, input is a python function, it's not a good idea to use it as a variable. 作为旁注, input是一个python函数,将它用作变量并不是一个好主意。

Of course and the answers given are right... you just have to remove the sys.stdin from the input but still use it in the iteration: 当然,给出的答案是正确的...你只需要从输入中删除sys.stdin但仍然在迭代中使用它:

for s in inputready+[sys.stdin]: for inputready + [sys.stdin]中的s:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM