<s> import socket <EOL> HOST = "<STR_LIT:127.0.0.1>" <EOL> HOSTv6 = "<STR_LIT>" <EOL> def find_unused_port ( family = socket . AF_INET , socktype = socket . SOCK_STREAM ) : <EOL> """<STR_LIT>""" <EOL> tempsock = socket . socket ( family , socktype ) <EOL> port = bind_port ( tempsock ) <EOL> tempsock . close ( ) <EOL> del tempsock <EOL> return port <EOL> def bind_port ( sock , host = HOST ) : <EOL> """<STR_LIT>""" <EOL> if sock . family == socket . AF_INET and sock . type == socket . SOCK_STREAM : <EOL> if hasattr ( socket , '<STR_LIT>' ) : <EOL> if sock . getsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR ) == <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" "<STR_LIT>" ) <EOL> if hasattr ( socket , '<STR_LIT>' ) : <EOL> if sock . getsockopt ( socket . SOL_SOCKET , socket . SO_REUSEPORT ) == <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" "<STR_LIT>" ) <EOL> if hasattr ( socket , '<STR_LIT>' ) : <EOL> sock . setsockopt ( socket . SOL_SOCKET , socket . SO_EXCLUSIVEADDRUSE , <NUM_LIT:1> ) <EOL> sock . bind ( ( host , <NUM_LIT:0> ) ) <EOL> port = sock . getsockname ( ) [ <NUM_LIT:1> ] <EOL> return port </s>
