无法在 https 端口 (443) [Errno 10013] 上实例化 python BaseHTTPServer

Cannot instantiate python BaseHTTPServer on https port (443) [Errno 10013]

BaseHTTPServer 无法使用端口 443 实例化。我想这是为了避免与其他一些 https 服务发生冲突而被阻止的。

因此,我尝试使用以下代码为此服务器定义 SNI,但仍然失败...

这是服务器建立代码,我运行它来自Windowscmd,具有管理员权限:

httpd = BaseHTTPServer.HTTPServer(("192.168.22.23", 443), MyRequestHandler)

tls_context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) 
tls_context.load_cert_chain(certfile='./cert.csr') 
tls_context.set_servername_callback(verify_tls) 
httpd.socket = tls_context.wrap_socket(httpd.socket, do_handshake_on_connect=True, server_side=True)

httpd.socket.settimeout(30) 
httpd.serve_forever()  

这是失败的命令:

httpd = BaseHTTPServer.HTTPServer(("192.168.22.23", 443), MyRequestHandler)

和输出:

c:\python27\lib\SocketServer.pyc in __init__(self, server_address, RequestHandlerClass, bind_and_activate)
    415         if bind_and_activate:
    416             try:
--> 417                 self.server_bind()
    418                 self.server_activate()
    419             except:

c:\python27\lib\BaseHTTPServer.pyc in server_bind(self)
    106     def server_bind(self):
    107         """Override server_bind to store the server name."""
--> 108         SocketServer.TCPServer.server_bind(self)
    109         host, port = self.socket.getsockname()[:2]
    110         self.server_name = socket.getfqdn(host)

c:\python27\lib\SocketServer.pyc in server_bind(self)
    429         if self.allow_reuse_address:
    430             self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
--> 431         self.socket.bind(self.server_address)
    432         self.server_address = self.socket.getsockname()
    433

c:\python27\lib\socket.pyc in meth(name, self, *args)
    226
    227 def meth(name,self,*args):
--> 228     return getattr(self._sock,name)(*args)
    229
    230 for _m in _socketmethods:

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

知道如何在 443 上定义我的服务器而不出现此错误吗?

谢谢

尝试将端口更改为 8080。您没有说是哪个 OS,但大多数 UNIX 衍生产品将只允许 root 侦听低于 1,024 或 4,096 的端口,具体取决于 OS 及其配置。来自这个 答案。

或者运行命令使用sudo。

BaseHTTPServer cannot be instantiated with port 443. I guess this is prevented to avoid collision with some other https services.

是的,报错"error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions"说明有这样的问题

Therefore, I've tried to define SNI to this server using the following code, but it still fails...

问题根本不在 TLS 层,因此在这一层(即 SNI)做一些事情无济于事。问题出在 TCP 层:它无法绑定到端口 443,因为该端口已被其他进程使用。解决此问题的唯一方法是确保没有其他进程使用此端口。