get open TCP port in Python -


I want to get a random open TCP port at the local host in Python. What is the easiest way?

My current solution:

  def Get_open_port (): import socket S = socket.socket (socket.AF_INET, socket.SOCK_STREAM) s.bind ((",, 0)) s.listen (1) ports = s.getsockname () [1] s.close () return port  

is not very good and this is not 100% accurate but it still works.


Comments