Get a free flask port.
()
| 104 | |
| 105 | |
| 106 | def _GetFreeFlaskPort(): |
| 107 | """Get a free flask port.""" |
| 108 | # We will prefer to use 5000. If not, we will then pick a random port. |
| 109 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 110 | result = sock.connect_ex(('127.0.0.1', 5000)) |
| 111 | if result == 0: |
| 112 | return 5000 |
| 113 | else: |
| 114 | s = socket.socket() |
| 115 | s.bind(('', 0)) |
| 116 | port = s.getsockname()[1] |
| 117 | s.close() |
| 118 | # Race condition: between the interval we close the socket and actually |
| 119 | # start a mint process, another process might have occupied the port. We |
| 120 | # don't do much here as this is mostly for convenience in research |
| 121 | # rather than 24x7 service. |
| 122 | return port |
| 123 | |
| 124 | def StartMint(root_folder=None, port=None): |
| 125 | """Start a mint instance. |