comparison lemuriformes/port.py @ 17:4793f99b73e0

[lemuriformes] utility functions
author Jeff Hammel <k0scist@gmail.com>
date Sun, 10 Dec 2017 17:42:52 -0800
parents
children
comparison
equal deleted inserted replaced
16:9b1bb9eee962 17:4793f99b73e0
1 #!/usr/bin/env python
2
3 import argparse
4 import socket
5 import sys
6
7
8 def get_free_port():
9 """get a free port on localhost"""
10
11 s = socket.socket(socket.AF_INET, type=socket.SOCK_STREAM)
12 s.bind(('localhost', 0))
13 address, port = s.getsockname()
14 s.close()
15 return port
16
17
18 def main(args=sys.argv[1:]):
19 """CLI to get a free port on localhost"""
20
21 # parse command line
22 parser = argparse.ArgumentParser(description=get_free_port.__doc__)
23 options = parser.parse_args()
24
25 # print an open port
26 print (get_free_port())
27
28
29 if __name__ == '__main__':
30 main()