Mercurial > repos > shellac > guppy_basecaller
diff env/lib/python3.7/site-packages/galaxy/util/sockets.py @ 5:9b1c78e6ba9c draft default tip
"planemo upload commit 6c0a8142489327ece472c84e558c47da711a9142"
author | shellac |
---|---|
date | Mon, 01 Jun 2020 08:59:25 -0400 |
parents | 79f47841a781 |
children |
line wrap: on
line diff
--- a/env/lib/python3.7/site-packages/galaxy/util/sockets.py Thu May 14 16:47:39 2020 -0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,43 +0,0 @@ -import random -import shlex -import socket -import subprocess - - -def unused_port(range=None): - if range: - return __unused_port_on_range(range) - else: - return __unused_port_rangeless() - - -def __unused_port_rangeless(): - # TODO: Allow ranges (though then need to guess and check)... - s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - s.bind(('localhost', 0)) - addr, port = s.getsockname() - s.close() - return port - - -def __unused_port_on_range(range): - assert range[0] and range[1] - - # Find all ports that are already occupied - cmd_netstat = shlex.split("netstat tuln") - p1 = subprocess.Popen(cmd_netstat, stdout=subprocess.PIPE) - - occupied_ports = set() - for line in p1.stdout.read().split('\n'): - if line.startswith('tcp') or line.startswith('tcp6'): - col = line.split() - local_address = col[3] - local_port = local_address.split(':')[1] - occupied_ports.add(int(local_port)) - - # Generate random free port number. - while True: - port = random.randrange(range[0], range[1]) - if port not in occupied_ports: - break - return port