File size: 226 Bytes
e231bdc
 
 
 
 
 
1
2
3
4
5
6
7
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 0)) # bind to a random free port
port = s.getsockname()[1] # get the port number
s.close() # close the socket
print(port) # print the port number