不允许python socket.error操作 [英] python socket.error operation not permitted

查看:93
本文介绍了不允许python socket.error操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在以root身份运行以下代码,并使用python2.6.1,平台为linux

I am running below code as root and using python2.6.1, platform is 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

如何解决这个问题

推荐答案

有几种可能性.

  • 您不是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) 

在绑定它之前.

这篇关于不允许python socket.error操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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