Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ipv6 #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions gadgetron/external/listen.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
from . import connection


def wait_for_client_connection(port):

sock = socket.socket(family=socket.AF_INET6)
def wait_for_client_connection(port, use_ipv4):
if use_ipv4:
sock = socket.socket(family=socket.AF_INET)
else:
sock = socket.socket(family=socket.AF_INET6)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', port))
sock.listen(0)
Expand All @@ -19,7 +21,7 @@ def wait_for_client_connection(port):
return conn


def listen(port, handler, *args, **kwargs):
def listen(port, handler, use_ipv4=False, *args, **kwargs):
"""
Listens on a given port and invokes the handler function with a connection and the provided args and kwargs
:param port: Port on which to listen
Expand All @@ -30,5 +32,5 @@ def listen(port, handler, *args, **kwargs):
logging.debug(f"Starting external Python module '{handler.__name__}' in state: [PASSIVE]")
logging.debug(f"Waiting for connection from client on port: {port}")

with connection.Connection(wait_for_client_connection(port)) as conn:
with connection.Connection(wait_for_client_connection(port, use_ipv4=use_ipv4)) as conn:
handler(conn, *args, **kwargs)