diff options
author | Lekinho <mctreasure@gmail.com> | 2020-02-12 06:29:31 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-12 10:29:31 +0000 |
commit | c8de203b0dec88c65a32e2d1edb2480644dfbc84 (patch) | |
tree | c07900c15cfab46be6177092480c827f390afd61 | |
parent | 177f22e80b2ecde710a5423ab12db5280bdff3eb (diff) | |
download | paste-git-c8de203b0dec88c65a32e2d1edb2480644dfbc84.tar.gz |
add ipv6 support by setting address family (#50)
* add ipv6 support by setting address family
-rwxr-xr-x | paste/httpserver.py | 28 | ||||
-rw-r--r-- | tests/test_httpserver.py | 61 |
2 files changed, 87 insertions, 2 deletions
diff --git a/paste/httpserver.py b/paste/httpserver.py index 79468f3..0cfe6c2 100755 --- a/paste/httpserver.py +++ b/paste/httpserver.py @@ -1289,13 +1289,39 @@ def serve(application, host=None, port=None, handler=None, ssl_pem=None, ssl_context.use_certificate_chain_file(ssl_pem) host = host or '127.0.0.1' + is_ipv6 = False + if host.count(':') > 1 or '[' in host : + is_ipv6 = True + if port is None: - if ':' in host: + + if ':' in host and is_ipv6 is False: host, port = host.split(':', 1) + elif is_ipv6 and ']' in host: + + idx = host.find(']') + if (idx < (len(host) - 1)) and (host[(idx+1)] == ':'): + # check if the next position is ':', if so, split and grab the port + + host, port = host.rsplit(':', 1) + host = host.strip('[').strip(']') + + else : + + port = 8080 else: + port = 8080 + #strip '[' and ']' in host + host = host.strip('[').strip(']') server_address = (host, int(port)) + if is_ipv6: + # set address family + HTTPServer.address_family = socket.AF_INET6 + else: + HTTPServer.address_family = socket.AF_INET + if not handler: handler = WSGIHandler if server_version: diff --git a/tests/test_httpserver.py b/tests/test_httpserver.py index 512f06d..7b64a45 100644 --- a/tests/test_httpserver.py +++ b/tests/test_httpserver.py @@ -4,7 +4,7 @@ import socket import six -from paste.httpserver import LimitedLengthFile, WSGIHandler +from paste.httpserver import LimitedLengthFile, WSGIHandler, serve from six.moves import StringIO @@ -72,3 +72,62 @@ def test_limited_length_file_tell_on_socket(): assert f.read() == b'123456789' assert f.tell() == 10 backing_read.close() + + +def test_address_family_v4(): + #ipv4 + app = None + host = '127.0.0.1' + port = '9090' + + svr = serve(app, host=host, port=port, start_loop=False, use_threadpool=False) + + af = svr.address_family + addr = svr.server_address + p = svr.server_port + + svr.server_close() + + assert (af == socket.AF_INET) + assert (addr[0] == '127.0.0.1') + assert (str(p) == port) + + +def test_address_family_v4_host_and_port(): + #ipv4 + app = None + host = '127.0.0.1:9091' + + svr = serve(app, host=host, start_loop=False, use_threadpool=False) + + af = svr.address_family + addr = svr.server_address + p = svr.server_port + + svr.server_close() + + assert (af == socket.AF_INET) + assert (addr[0] == '127.0.0.1') + assert (str(p) == '9091') + +def test_address_family_v6(): + #ipv6 + app = None + host = '[::1]' + port = '9090' + + try: + svr = serve(app, host=host, port=port, start_loop=False, use_threadpool=False) + + af = svr.address_family + addr = svr.server_address + p = svr.server_port + + svr.server_close() + + assert (af == socket.AF_INET6) + assert (addr[0] == '::1') + assert (str(p) == port) + except (socket.error, OSError) as err: + # v6 support not available in this OS, pass the test + assert True
\ No newline at end of file |