Skip to content

Commit

Permalink
add ipv6 support by setting address family (pasteorg#50)
Browse files Browse the repository at this point in the history
* add ipv6 support by setting address family
  • Loading branch information
Lekinho authored Feb 12, 2020
1 parent 177f22e commit c8de203
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
28 changes: 27 additions & 1 deletion paste/httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
61 changes: 60 additions & 1 deletion tests/test_httpserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import six

from paste.httpserver import LimitedLengthFile, WSGIHandler
from paste.httpserver import LimitedLengthFile, WSGIHandler, serve
from six.moves import StringIO


Expand Down Expand Up @@ -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

0 comments on commit c8de203

Please sign in to comment.