Python 错误:io.UnsupportedOperation:fileno
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31080829/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Error: io.UnsupportedOperation: fileno
提问by mee
I am using a server and the client programs from this link: http://www.bogotobogo.com/python/python_network_programming_tcp_server_client_chat_server_chat_client_select.php
我正在使用此链接中的服务器和客户端程序:http: //www.bogotobogo.com/python/python_network_programming_tcp_server_client_chat_server_chat_client_select.php
When I run the client I encounter the following error:
当我运行客户端时,我遇到以下错误:
Traceback (most recent call last):
File "client.py", line 26, in client
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
io.UnsupportedOperation: fileno
I am using Python 3
, but I have changed all lines using print from Python 2 to 3
.
我正在使用Python 3
,但我已经使用 print from 更改了所有行Python 2 to 3
。
Here is the code:
这是代码:
while 1:
socket_list = [sys.stdin, s]
# Get the list sockets which are readable
read_sockets, write_sockets, error_sockets = select.select(socket_list , [], [])
采纳答案by Wolph
While the fileno()
method works on normal IO objects (sys.stdout
, sys.stderr
, sys.stdin
and socket.socket
), the IDLE Python IDE changes your IO objects which breaks this.
虽然该fileno()
方法适用于普通 IO 对象(sys.stdout
、sys.stderr
、sys.stdin
和socket.socket
),但 IDLE Python IDE 会更改您的 IO 对象,从而破坏了这一点。
So... if you get this error, run the command from straight up Python instead.
所以...如果你收到这个错误,直接从 Python 运行命令。
回答by Martin X
I recently aiso got this error ( Python 2: AttributeError: StringIO instance has no attribute 'fileno' ; Python 3: io.UnsupportedOperation: fileno ) in test cases on Travis CI, when the python code excuted a command and wanted to read sys.stdout
我最近在 Travis CI 的测试用例中遇到了这个错误(Python 2: AttributeError: StringIO instance has no attribute 'fileno' ; Python 3: io.UnsupportedOperation: fileno ),当 python 代码执行一个命令并想要读取 sys.path 时。标准输出
I guess on Travis CI wraps command output and return a StringIO instead of a file object as usual. As you can see in the log webpage of Travis CI, the wrapped output is white color, instead of colorful as usual.
我猜在 Travis CI 上包装命令输出并返回一个 StringIO 而不是像往常一样的文件对象。正如你在 Travis CI 的日志网页中看到的,包装的输出是白色的,而不是像往常一样彩色。
So my way is not to excuted a command, to run instance of your own class to be tested directly instead.
所以我的方法不是执行命令,而是直接运行您自己的类的实例进行测试。
I searched all over the internet but failed to get a sulution. I solved this by myself and I want to share with others.
我在互联网上搜索了所有内容,但未能获得解决方案。我自己解决了这个问题,我想与他人分享。
In case you still don't understand what I meant. you can see this commit:
以防你仍然不明白我的意思。你可以看到这个提交:
https://github.com/martin68/apt-smart/commit/bb8fd766f7d96999a3a3fb79d089cde73c71ce83
https://github.com/martin68/apt-smart/commit/bb8fd766f7d96999a3a3fb79d089cde73c71ce83