Skip to content

Commit 39d2360

Browse files
authored
wsgi server: allow binding to ipv6 address (#657)
Reproduce the fix in python3.8 http.server by doing a dns lookup and using the correct address family Fixes #567 Signed-off-by: Dominique Martinet <asmadeus@codewreck.org>
1 parent ecb5dd9 commit 39d2360

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

prometheus_client/exposition.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,24 @@ class ThreadingWSGIServer(ThreadingMixIn, WSGIServer):
138138
daemon_threads = True
139139

140140

141-
def start_wsgi_server(port, addr='', registry=REGISTRY):
141+
def _get_best_family(address, port):
142+
"""Automatically select address family depending on address"""
143+
# HTTPServer defaults to AF_INET, which will not start properly if
144+
# binding an ipv6 address is requested.
145+
# This function is based on what upstream python did for http.server
146+
# in https://github.com/python/cpython/pull/11767
147+
infos = socket.getaddrinfo(address, port)
148+
family, _, _, _, sockaddr = next(iter(infos))
149+
return family, sockaddr[0]
150+
151+
152+
def start_wsgi_server(port, addr='0.0.0.0', registry=REGISTRY):
142153
"""Starts a WSGI server for prometheus metrics as a daemon thread."""
154+
class TmpServer(ThreadingWSGIServer):
155+
"""Copy of ThreadingWSGIServer to update address_family locally"""
156+
TmpServer.address_family, addr = _get_best_family(addr, port)
143157
app = make_wsgi_app(registry)
144-
httpd = make_server(addr, port, app, ThreadingWSGIServer, handler_class=_SilentHandler)
158+
httpd = make_server(addr, port, app, TmpServer, handler_class=_SilentHandler)
145159
t = threading.Thread(target=httpd.serve_forever)
146160
t.daemon = True
147161
t.start()

0 commit comments

Comments
 (0)