From ac2c0d1e8b492cb6905c72cdde3df052efa9d3f5 Mon Sep 17 00:00:00 2001 From: AN Long Date: Fri, 3 Dec 2021 17:56:51 +0800 Subject: [PATCH] Support IPv6 --- pypprof/net_http.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pypprof/net_http.py b/pypprof/net_http.py index da38f77..8aa24da 100644 --- a/pypprof/net_http.py +++ b/pypprof/net_http.py @@ -2,6 +2,7 @@ import gc import pkg_resources +import socket import sys import threading import time @@ -25,6 +26,12 @@ _wall_profiler = WallProfiler() +class ProfilerHTTPServer(HTTPServer): + '''We do not want modify the global HTTPServer.address_family for do not + break user's codes, so add a subclass for isolation. + ''' + address_family = socket.AF_INET + def start_pprof_server(host='localhost', port=8080): '''Start a pprof server at the given host:port in a background thread. @@ -40,7 +47,9 @@ def start_pprof_server(host='localhost', port=8080): # wall-clock profiler's SIGALRM handler, which may conflict with other uses. _wall_profiler.register_handler() - server = HTTPServer((host, port), PProfRequestHandler) + if ':' in host: + ProfilerHTTPServer.address_family = socket.AF_INET6 + server = ProfilerHTTPServer((host, port), PProfRequestHandler) bg_thread = threading.Thread(target=server.serve_forever) bg_thread.daemon = True bg_thread.start()