importsocketdefis_open(port):local_ip='127.0.0.1's=socket.socket(socket.AF_INET,socket.SOCK_STREAM)try:s.connect((local_ip,int(port)))s.shutdown(2)returnTrueexcept:returnFalsedefget_hostname():returnsocket.gethostname()deffind_unused_port():""" get a unused port of this machine :return: available port """min_port=7000max_port=7999aval_port=Noneforportinrange(min_port,max_port):ifnotis_open(port):aval_port=portbreakreturnaval_portif__name__=='__main__':printfind_unused_port()