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

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

问题描述

我正在尝试在 Windows 7 上使用 Python 2.6.5 创建自定义 TCP 堆栈,以便在本地端口 80 上提供有效的 http 页面请求.但是,我遇到了一个问题,似乎 Windows 7 加强了安全性.此代码适用于 Vista.

这是我的示例代码:

导入SocketServer导入结构类 MyTCPHandler(SocketServer.BaseRequestHandler):定义句柄(自我):headerText = """HTTP/1.0 200 OK日期:1999 年 12 月 31 日星期五 23:59:59 GMT内容类型:文本/html内容长度:1354"""bodyText = "<html><body>某个页面</body></html>"self.request.send(headerText + "
" + bodyText)如果 __name__ == "__main__":主机, 端口 = "本地主机", 80server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler)server.serve_forever()

<块引用>

C:python>python TestServer.py回溯(最近一次调用最后一次):
文件TestServer.py",第 19 行,在服务器 = SocketServer.TCPServer((HOST, PORT),MyTCPHandler) 文件"C:Python26libSocketServer.py",第 400 行,在 init 中self.server_bind() 文件 "C:Python26libSocketServer.py",第 411 行,在 server_bind 中self.socket.bind(self.server_address)文件",第 1 行,在绑定中

socket.error: [Errno 10013] 一次尝试以某种方式访问​​套接字被其访问权限禁止

我究竟如何让它在 Windows 7 上运行?

[Edit on 5/5/2010 @ 2344 PDT] 这个 answer 解释了该错误是由于访问低于 1024 的端口时需要提升/超级用户权限引起的.我将尝试使用更高的端口号,看看是否有效.但是,我还是想知道为什么我的本地管理员帐户无法访问端口 80.

解决方案

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

程序在以管理员身份运行之前必须提示管理员访问权限,以及熟悉的 UAC 对话框.由于 Python 脚本不能直接执行,因此没有以管理员身份运行"上下文菜单选项.

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

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 + "
" + 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, in server = SocketServer.TCPServer((HOST, PORT), MyTCPHandler) File "C:Python26libSocketServer.py", line 400, in init self.server_bind() File "C:Python26libSocketServer.py", line 411, in server_bind self.socket.bind(self.server_address) File "", line 1, in bind

socket.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.

解决方案

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, 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天全站免登陆