blob: 59d01723a1ebef9ede5c82a54b3f539408e15f76 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
2# SPDX-License-Identifier: MIT
3#
4
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05005import socket
6
Brad Bishop79641f22019-09-10 07:20:22 -04007def get_free_port(udp = False):
8 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM if not udp else socket.SOCK_DGRAM)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05009 s.bind(('', 0))
10 addr = s.getsockname()
11 s.close()
12 return addr[1]