socket.error: [Errno 10013] 尝试以访问权限禁止的方式访问套接字
我正在尝试在 Windows 7 上使用 Python 2.6.5 创建自定义 TCP 堆栈,以在本地端口 80 上提供有效的 http 页面请求。但是,我遇到了一个障碍,Windows 7 似乎加强了安全性。这段代码适用于 Vista。
这是我的示例代码:
import SocketServer
import struct
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
headerText = """HTTP/1.0 200 OK
Date: Fri, 31 Dec 1999 23:59:59 GMT
Content-Type: text/html
Content-Length: 1354"""
bodyText = "<html><body>some page</body></html>"
self.request.send(headerText + "\n" + bodyText)
if __name__ == "__main__":
HOST, PORT = "localhost", 80
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()
C:\python>python TestServer.py 回溯(最近一次调用最后一次):
文件“TestServer.py”,第 19 行,位于 服务器= SocketServer.TCPServer((主机,端口), MyTCPHandler) 文件 “C:\ Python26 \ lib \ SocketServer.py”, 第 400 行,在 init 中 self.server_bind() 文件“C:\Python26\lib\SocketServer.py”, server_bind 中的第 411 行 self.socket.bind(self.server_address) 文件“”,第 1 行,绑定中socket.error: [Errno 10013] 尝试 以某种方式访问套接字 被其访问权限禁止
我到底如何让它在 Windows 7 上工作?
[编辑于 5/5/2010 @ 2344 PDT] 这个答案解释了该错误是由于访问低于 1024 的端口时需要提升/超级用户权限引起的。我将尝试使用更高的端口号来查看是否有效。但是,我仍然想知道为什么我的本地管理员帐户无法访问端口 80。
I'm trying to create a custom TCP stack using Python 2.6.5 on Windows 7 to serve valid http page requests on port 80 locally. But, I've run into a snag with what seems like Windows 7 tightened up security. This code worked on Vista.
Here's my sample code:
import SocketServer
import struct
class MyTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
headerText = """HTTP/1.0 200 OK
Date: Fri, 31 Dec 1999 23:59:59 GMT
Content-Type: text/html
Content-Length: 1354"""
bodyText = "<html><body>some page</body></html>"
self.request.send(headerText + "\n" + bodyText)
if __name__ == "__main__":
HOST, PORT = "localhost", 80
server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)
server.serve_forever()
C:\python>python TestServer.py
Traceback (most recent call last):
File "TestServer.py", line 19, inserver = SocketServer.TCPServer((HOST, PORT),
MyTCPHandler) File
"C:\Python26\lib\SocketServer.py",
line 400, in init
self.server_bind() File "C:\Python26\lib\SocketServer.py",
line 411, in server_bind
self.socket.bind(self.server_address)
File "", line 1, in bindsocket.error: [Errno 10013] An attempt
was made to access a socket in a way
forbidden by its access permissions
How exactly do I get this to work on Windows 7?
[Edit on 5/5/2010 @ 2344 PDT] This answer explains that the error is caused by the need for elevated / superuser privileges when accessing ports lower than 1024. I'm going to try using a higher port number to see if that works. However, I still would like to know why my local admin account can't access port 80.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(24)
我刚刚遇到了同样的问题,我的系统是Win7。只需在终端上使用命令,例如:
netstat -na|findstr port
,您将看到该端口已被使用。因此,如果您想在不显示此消息的情况下启动服务器,您可以更改其他未使用的端口。I just encountered the same issue, my system is Win7. Just use the command on terminal like:
netstat -na|findstr port
, you will see the port has been used. So if you want to start the server without this message, you can change other port that is not in use.尝试在不同的端口运行服务器。为我工作:
解释:
如 Django 文档中所述:
如果您以具有普通权限的用户身份运行此脚本(推荐),您可能无权启动低端口号上的端口。低端口号是为超级用户 (root) 保留的。
该服务器使用 WSGI_APPLICATION 设置指定的 WSGI 应用程序对象。
请勿在生产环境中使用此服务器。它尚未经过安全审核或性能测试。 (这就是它的发展方向。我们的业务是制作 Web 框架,而不是 Web 服务器,因此改进此服务器以使其能够处理生产环境超出了 Django 的范围。)
Try to run the server at a different port. Worked for me:
Explanation:
as mentioned on Django documentation:
If you run this script as a user with normal privileges (recommended), you might not have access to start a port on a low port number. Low port numbers are reserved for the superuser (root).
This server uses the WSGI application object specified by the WSGI_APPLICATION setting.
DO NOT USE THIS SERVER IN A PRODUCTION SETTING. It has not gone through security audits or performance tests. (And that’s how it’s gonna stay. We’re in the business of making Web frameworks, not Web servers, so improving this server to be able to handle a production environment is outside the scope of Django.)
在 Windows Vista/7 上,使用 UAC,管理员帐户默认以非特权模式运行程序。
程序在以管理员身份运行之前必须通过我们熟悉的 UAC 对话框提示管理员访问权限。由于 Python 脚本不能直接执行,因此没有“以管理员身份运行”上下文菜单选项。
可以使用
ctypes.windll.shell32.IsUserAnAdmin()
来检测脚本是否具有管理员访问权限,并且ShellExecuteEx
在 python.exe 上带有“runas”动词,带有 sys.argv [0] 作为参数,在需要时提示 UAC 对话框。On Windows Vista/7, with UAC, administrator accounts run programs in unprivileged mode by default.
Programs must prompt for administrator access before they run as administrator, with the ever-so-familiar UAC dialog. Since Python scripts aren't directly executable, there's no "Run as Administrator" context menu option.
It's possible to use
ctypes.windll.shell32.IsUserAnAdmin()
to detect whether the script has admin access, andShellExecuteEx
with the 'runas' verb on python.exe, with sys.argv[0] as a parameter to prompt the UAC dialog if needed.对我来说,当我有另一个进程已经在侦听同一端口时,它会像 Windows 7 x64 上那样抱怨。
可以通过运行查看当前占用(绑定)的端口
For me it was complaining like that on Windows 7 x64 when I had another process already listening on that same port.
It is possible to see currently occupied (bound) ports by running
迈克菲为我阻止了它。我必须在访问保护规则中允许该程序
请参阅 http://www.symantec .com/connect/articles/we-are-unable-send-your-email-caused-mcafee
McAfee was blocking it for me. I had to allow the program in the access protection rules
See http://www.symantec.com/connect/articles/we-are-unable-send-your-email-caused-mcafee
您的本地端口正在被另一个应用程序使用。我遇到了同样的问题!
您可以尝试以下步骤:
转到命令行并以管理员身份运行!
类型:
类型:
<前><代码>TASKKILL /F /PID 4032
<块引用>
=>成功:PID 4032 的进程已终止。
注意:我的 5000 本地端口按 PID 4032 列出。您应该提供您的端口!
Your local port is using by another app. I faced the same problem!
You can try the following step:
Go to command line and run it as administrator!
Type:
Type:
Note: My 5000 local port was listing by PID 4032. You should give yours!
socket.error: [Errno 10013] 尝试以访问权限禁止的方式访问套接字
使用 flask 得到此信息:
意味着您正在使用的端口尝试绑定,已被其他服务或进程使用:
在我在 Eclipse / windows 上开发的代码中得到了关于这一点的提示:
socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
Got this with flask :
Means that the port you're trying to bind to, is already in used by another service or process :
got a hint on this in my code developed on Eclipse / windows :
我今天遇到了这个问题,但网络服务器的端口尚未使用。
在提升的终端中运行以下命令解决了问题。
我认为这与docker上运行的本地sql服务器有关,并且web服务器无法访问它。
I ran into this issue today, but the webserver's port was not already used.
Running the following commands in elevated terminal solved the problem.
I think it had something to do with local sql server running on docker, and the webserver not being able to access it.
这是解决您问题的一个简单解决方案。出现此错误的原因可能是您尝试在其上运行应用程序的端口上运行另一个进程或一堆进程。
以管理员身份打开cmd
如果您将服务器设置为服务并且服务中断,也可能会发生这种情况。
给出以下命令,
您将能够看到该端口上的活动任务列表。
您可以通过使用
完成后重新启动服务器来关闭所有不必要的任务。
Here is a simple solution to your problem..this error occurs either due to another process or bunch of processes running on the port which you are trying to run you app on.
Open cmd as an admin
This may also occure if you have setup your server as a SERVICE and the service breaks.
Give below command
you'll be able to see list of active task on that port.
you can close all unnecessary task by using
once done restart your server.
只需在高于 1024 的端口上运行,低于 1024 的任何端口都是特权的,这与 Linux 的处理相同,我在没有任何 UAC 特权升级的情况下在 win 上使用 5000 例如。
Just run on ports above 1024 , anything below is privileged, its the same deal with Linux, i use 5000 for example on wins without any UAC priv escalation.
看来80端口已经被使用了。尝试使用系统中任何其他应用程序未使用的其他端口。
It Seems the Port 80 is already in use. Try to Use some other Port which is not in use by any other application in your System.
我通过编辑出站防火墙规则在 Windows 10 上解决了这个问题。
右键单击规则“阻止 SQL Server 实例 MSSQLSERVER 中 R 本地用户帐户的网络访问”上的“允许”
Windows 10 防火墙的屏幕截图 - 出站规则 - 这就是阻止我的实例的原因
I solved this on Windows 10 by editing an outbound firewall rule.
Right click "allow" on rule "Block network access for R local user accounts in SQL Server instance MSSQLSERVER"
Screenshot from Windows 10 Firewall - Outbound rules- this is what was blocking my instance
我必须在 Windows 防火墙中允许 ..\python27\python.exe。我不需要在 WinXP 或 Win8 上执行此操作。
I had to allow ..\python27\python.exe in windows firewall. I don't need to do this on WinXP or Win8.
尝试使用 80 以外的端口
Try using a different port than 80
主要问题是端口号被其他应用程序使用。因此您可以将端口号更改为未使用的端口号,如下所示。
在Windows中,您可以在Windows任务管理器中查看不同应用程序使用的端口号。
The main problem is port number used by another application.So you can change the port number to unused one as shown below.
In windows you can view the used port numbers used by different apps in windows task manager.
在防病毒软件中禁用访问保护,
我遇到了同样的问题,最后发现了防病毒软件的以下日志。
被访问保护规则阻止 NT AUTHORITY\SYSTEM C:\WINDOWS\SYSTEM32\SVCHOST.EXE C:\PROGRAM FILES (X86)\MCAFEE\VIRUSSCAN ENTERPRISE\MCCONSOL.EXE 通用标准保护:防止终止 McAfee 进程 操作被阻止:终止
被端口阻止规则阻止 C:\USERS\用户名\APPDATA\LOCAL\PROGRAMS\PYTHON\PYTHON37-32\PYTHON.EXE 防病毒标准保护:防止群发邮件蠕虫发送邮件
Disable Access Protection in Antivirus,
I faced same issue at last found the below logs from antivirus.
Blocked by Access Protection rule NT AUTHORITY\SYSTEM C:\WINDOWS\SYSTEM32\SVCHOST.EXE C:\PROGRAM FILES (X86)\MCAFEE\VIRUSSCAN ENTERPRISE\MCCONSOL.EXE Common Standard Protection:Prevent termination of McAfee processes Action blocked : Terminate
Blocked by port blocking rule C:\USERS\username\APPDATA\LOCAL\PROGRAMS\PYTHON\PYTHON37-32\PYTHON.EXE Anti-virus Standard Protection:Prevent mass mailing worms from sending mail
我刚刚在我的案例中发现卡巴斯基安全软件 2019 防火墙阻止了 python 的网络访问。禁用防火墙可以顺利工作。或者为 python 应用程序添加例外规则,并且所有带有 *.py 的文件扩展名也将起作用。
I just found in my case Kaspersky Internet Security 2019 Firewall was blocking net access for python. Disabling firewall working smoothly. Or adding a exception rules for python app and all file extension with *.py will also work.
在 Windows 中,检查“万维网发布服务”是否正在运行,因为当您安装 IIS 时,它有一个自动服务。启动并监听80、443端口。
In Windows check "World Wide Web Publishing Service" if it is running, because when you install IIS it has a service that auto. starts and listens 80, 443 port.
然后
TASKKILL /F /PID 4032
正如 rafee noor 所说,但是当系统使用端口时,这不起作用
端口 80 正在被系统(PID 4)使用,那是什么?
或者
这适用于上述情况
then
TASKKILL /F /PID 4032
as rafee noor said, but this doesn't work when Port is being used by SYSTEM
Port 80 is being used by SYSTEM (PID 4), what is that?
or
this works in above case
对于在使用
vscode
进行远程主机
时遇到此错误的用户:Terminal
( View -> Terminal ) 或 ( CTRL+ ` )PORTS
选项卡上现在您可以使用该端口了。
For those who have encountered this error while using
vscode
forremote host
:Terminal
( View -> Terminal ) or ( CTRL+ ` )PORTS
Now you can use the port.
这有效。窗口 11。
在 app.run() 中手动指定端口=3000 或任何端口,
然后出现一个窗口,单击允许
我认为 Flask 服务器和 Web 应用程序都尝试在
上运行
在我的例子中,单个端口为 5000,这可能是原因
,所以更改端口有效。我不确定这是真的。
this worked. windows 11.
assign the port=3000 or any, manually in app.run()
then a window appeared ,clicked allowed
i think flask server and web app both try to run on
a single port in my case 5000 ,this may be the reason
,so changing port worked. i am not sure its true .
在花费数小时在 Docker 网络中进行调试并调整 Windows 端口和设置后,我发现从 VPN 中断开连接解决了该问题。
After spending hours debugging in Docker network and adjusting Windows ports and settings, I found that disconnecting from the VPN solved the issue.
它可能会帮助你:
检查您之前是否打开过wireguard VPN软件,例如tailscale。
看来,如果您在关闭程序中的套接字时关闭此类软件,我会见证某种竞争条件导致 errno 10013
我猜是 windows 的 bug。
it might help you:
check if you opened wireguard VPN software before, such as tailscale.
It seems that if u close such software while you close ur socket in your program, I witeness some kind of race condition result in errno 10013
I guess itz windows' bug.
我在Python中找到了解决这个问题的方法。
转到 c:\python27\ 目录并点击 python.exe 并按 Tab 键切换到兼容性,然后选择管理员权限选项并应用更改。现在您发出允许创建套接字连接的命令。
I found a solution to solve this problem in Python.
go to c:\python27\ directory and rigtlcick python.exe and tab to compaitbility and select the admin privilege option and apply the changes. Now you issue the command it allows to create the socket connection.