我有以下代码来运行一个简单的http服务器
from http.server import SimpleHTTPRequestHandler, HTTPServer
host = "localhost"
port = 8881
server_class = HTTPServer
httpd = server_class((host, port), SimpleHTTPRequestHandler)
print("http server is running {}:{}".format(host, port))
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()在我的代码中的某个时候,我想访问服务器的底层套接字s (我认为它必须以某种方式访问)来执行类似于s.getsockname()的事情。这有可能吗?
发布于 2018-06-12 20:09:04
您可以像这样以self.socket的形式访问它。
httpd.socket.getsockname()有关更多信息,请参见基类SocketServer的源代码。
然而,对于这个用例来说,正确的方法是httpd.server_address。通常,您不应该尝试使用原始套接字。另外,我将跳过server_class变量,只需转到HTTPServer((host, port), SimpleHTTPRequestHandler)就可以保持简单。
https://stackoverflow.com/questions/50821993
复制相似问题