Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 1 | # |
2 | # SPDX-License-Identifier: MIT | ||||
3 | # | ||||
4 | |||||
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 5 | import socket |
6 | |||||
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 7 | def get_free_port(udp = False): |
8 | s = socket.socket(socket.AF_INET, socket.SOCK_STREAM if not udp else socket.SOCK_DGRAM) | ||||
Patrick Williams | d8c66bc | 2016-06-20 12:57:21 -0500 | [diff] [blame] | 9 | s.bind(('', 0)) |
10 | addr = s.getsockname() | ||||
11 | s.close() | ||||
12 | return addr[1] |