blob: d52340170b2eb155adb43cf07ea87d9807f05f4f (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#!/usr/bin/python3
#
# Copyright (C) 2017 Lars Wirzenius
#
# Pick a random port that is free to be listened on. For testing.
import errno
import random
import socket
import sys
MAX = 1000
for i in range(MAX):
port = random.randint(1025, 2**15-1)
s = socket.socket()
try:
s.bind(('0.0.0.0', port))
except OSError as e:
if e.errno == errno.EADDRINUSE:
continue
raise
break
else:
sys.stderr.write("Can't find a free port\n")
sys.exit(1)
sys.stdout.write('{}\n'.format(port))
|