socket.error:[Errno 10013]尝试以其访问权限禁止的方式访问套接字 [英] socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions

查看:409
本文介绍了socket.error:[Errno 10013]尝试以其访问权限禁止的方式访问套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Windows 7上使用Python 2.6.5创建自定义TCP堆栈,以在本地端口80上提供有效的http页面请求。但是,我似乎遇到了Windows 7加强安全性的困扰。这段代码在Vista上有效。

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.

这是我的示例代码:

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
追溯(最近一次调用):

File TestServer.py,第19行,在

服务器= SocketServer.TCPServer((HOST,PORT),
MyTCPHandler)文件
C:\Python26\lib\ \SocketServer.py,
第400行,位于 init 中,
self.server_bind()文件 C:\Python26\lib\SocketSocket.py,
第411行,在server_bind中
self.socket.bind(self.server_address)
文件,第1行,在绑定中

C:\python>python TestServer.py Traceback (most recent call last):
File "TestServer.py", line 19, in server = 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 bind

socket .error:[Errno 10013]尝试
以其访问权限禁止的方式
访问套接字

socket.error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions

我究竟如何得到这个t可以在Windows 7上工作吗?

How exactly do I get this to work on Windows 7?

[在2010年5月5日@ 2344 PDT编辑]这 answer 解释,该错误是由于访问低于1024的端口时需要提升的/超级用户特权引起的。我将尝试使用更高的端口号看看是否可行。但是,我仍然想知道为什么我的本地管理员帐户无法访问端口80。

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.

推荐答案

在Windows Vista / 7上,使用UAC,默认情况下,管理员帐户以非特权模式运行程序。

On Windows Vista/7, with UAC, administrator accounts run programs in unprivileged mode by default.

程序必须以熟悉的UAC对话框提示以管理员身份运行,然后才能以管理员身份运行。 。由于Python脚本不能直接执行,因此没有以管理员身份运行上下文菜单选项。

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.

可以使用 ctypes.windll.shell32 .IsUserAnAdmin()检测脚本是否具有管理员访问权限,并 ShellExecuteEx ,在python.exe上带有'runas'动词,并带有sys.argv [0]作为在需要时提示UAC对话框的参数。

It's possible to use ctypes.windll.shell32.IsUserAnAdmin() to detect whether the script has admin access, and ShellExecuteEx with the 'runas' verb on python.exe, with sys.argv[0] as a parameter to prompt the UAC dialog if needed.

这篇关于socket.error:[Errno 10013]尝试以其访问权限禁止的方式访问套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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