BaseHTTPServer
不能用端口 443 实例化。我猜这是为了避免与其他一些 https
服务发生冲突。
因此,我尝试使用以下代码为该服务器定义 SNI,但它仍然失败...
这是服务器建立代码,我从具有管理员权限的 Windows cmd
运行它:
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 上定义我的服务器而不会出现此错误吗?
谢谢
最佳答案
BaseHTTPServer cannot be instantiated with port 443. I guess this is prevented to avoid collision with some other https services.
是的,错误“错误:[Errno 10013] 试图以其访问权限禁止的方式访问套接字” 表明存在这样的问题。
Therefore, I've tried to define SNI to this server using the following code, but it still fails...
问题根本不在 TLS 层,因此在这一层(即 SNI)做一些事情无济于事。问题出在 TCP 层:它无法绑定(bind)到端口 443,因为该端口已被其他进程使用。解决此问题的唯一方法是确保没有其他进程使用此端口。
关于python - 无法在 https 端口 (443) [Errno 10013] 上实例化 python BaseHTTPServer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57509871/