我在代码中编写了一个http_server
函数,如下所示:
@contextlib.contextmanager
def http_server(app=None, handler_class=WSGIRequestHandler,
server_class=WSGIServer, bind=('localhost', 8082)):
server = server_class(bind, handler_class)
thread = threading.Thread(
target=server.serve_forever, name='sa-media test server.', daemon=True
)
thread.start()
url = f'http://{server.server_address[0]}:{server.server_address[1]}'
yield server, url
server.shutdown()
thread.join()
这将很好地工作,但是当我想像localhost/apiv1
中的bind
那样服务我的服务器时,会出现错误。
这是痕迹:
File "/usr/local/lib/python3.6/socketserver.py", line 453, in __init__
self.server_bind()
File "/usr/local/lib/python3.6/wsgiref/simple_server.py", line 50, in server_bind
HTTPServer.server_bind(self)
File "/usr/local/lib/python3.6/http/server.py", line 136, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/local/lib/python3.6/socketserver.py", line 467, in server_bind
self.socket.bind(self.server_address)
socket.gaierror: [Errno -2] Name or service not known
如何在localhost:8082/smth
上为服务器提供服务?