blob: da4ffda9a99e2da6e903dc28d2ad134a37eb76d9 [file] [log] [blame]
Brad Bishopc342db32019-05-15 21:57:59 -04001#
Patrick Williams92b42cb2022-09-03 06:53:57 -05002# Copyright OpenEmbedded Contributors
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: MIT
5#
6
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05007import socket
8
Brad Bishop79641f22019-09-10 07:20:22 -04009def get_free_port(udp = False):
10 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM if not udp else socket.SOCK_DGRAM)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -050011 s.bind(('', 0))
12 addr = s.getsockname()
13 s.close()
14 return addr[1]