可以选择()与Windows下的Python文件一起使用? [英] Can select() be used with files in Python under Windows?

查看:121
本文介绍了可以选择()与Windows下的Python文件一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 
一个echo服务器,使用select一次处理多个客户端
在终端输入任意一行输入将退出服务器


import select
导入套接字
导入系统
$ b $主机=''
端口= 50000
积压= 5
大小= 1024
服务器= socket.socket(套接字.b_inset,socket.SOCK_STREAM)
server.bind((host,port))
server.listen(backlog)
input = [server,sys.stdin]
running = 1
运行时:
输入准备,输出准备,exceptready = select.select(输入,[],[])
$ b $输入准备中的s:

如果s ==服务器:
#处理服务器套接字
客户端,地址= server.accept()
input.append(客户端)

elif s = = sys.stdin:
#处理标准输入
junk = sys.stdin.readline()
ru n宁= 0

其他:
#处理所有其他套接字
数据= s.recv(大小)
如果数据:
s.send(data )
else:
s.close()
input.remove(s)
server.close()

我得到错误信息(10038,'一个操作尝试了不是套接字的东西')。这可能与python文档中的注释有关:Windows上的文件对象不可接受,但是套接字是在Windows上,底层的select()函数是由WinSock库提供的,并不处理源自WinSock的文件描述符。在互联网上有很多关于这个话题的帖子,但是他们对我来说太技术了,或者根本就不清楚。所以我的问题是:是否有任何方式可以在Windows下使用Python中的select()语句?请添加一个小例子或修改上面的代码。感谢!

解决方案

看起来不像sys.stdin

如果您将输入更改为

  input = [server] 

异常将消失。



这是从 doc

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


I am trying to run the following python server under windows:

"""
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'). 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.". 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? Please add a little example or modify my code above. Thanks!

解决方案

Look like it does not like sys.stdin

If you change input to this

input = [server] 

the exception will go away.

This is from the 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.

这篇关于可以选择()与Windows下的Python文件一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆