import socket
def get_avaliable_port():
"""
get avaliable port in system
"""
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", 0))
port = s.getsockname()[1]
s.close()
return port
def is_port_free(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
result = s.connect_ex(('127.0.0.1', port))
return result != 0