Linux 不允许 python socket.error 操作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11145463/
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 socket.error operation not permitted
提问by big
I am running below code as root and using python2.6.1, platform is linux
我以 root 身份运行下面的代码并使用 python2.6.1,平台是 linux
>>> import socket
>>> serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>>> serversocket.bind((socket.gethostname(), 80))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1, in bind
socket.error: [Errno 1] Operation not permitted
How to solve this problem
如何解决这个问题呢
采纳答案by Jirka Hanika
There are several possibilities.
有几种可能性。
- You are not root.
- A previously run version of your application is still holding the port in the background. Kill it by name.
- A system daemon is still holding the port, for example Apache.
- 你不是root。
- 以前运行的应用程序版本仍在后台保留端口。用名字杀死它。
- 系统守护进程仍然持有该端口,例如 Apache。
Note that the port is not immediately available after the socket is closed (server having been killed). If you want to be sure that processes that don't exist anymore cannot be blocking the port from reuse, issue:
请注意,套接字关闭后端口不会立即可用(服务器已被杀死)。如果您想确保不再存在的进程无法阻止端口重用,请发出:
serversocket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
before binding it.
在绑定之前。