| 35 | |
| 36 | |
| 37 | def get_socket_names(n_sockets, close=False): |
| 38 | socket_names = [] |
| 39 | sockets = [] |
| 40 | for _ in range(n_sockets): |
| 41 | if IS_JYTHON: |
| 42 | # Although the option which would be pure java *should* work for Jython, the socket being returned is still 0 |
| 43 | # (i.e.: it doesn't give the local port bound, only the original port, which was 0). |
| 44 | from java.net import ServerSocket |
| 45 | |
| 46 | sock = ServerSocket(0) |
| 47 | socket_name = get_localhost(), sock.getLocalPort() |
| 48 | else: |
| 49 | sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 50 | sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 51 | sock.bind((get_localhost(), 0)) |
| 52 | socket_name = sock.getsockname() |
| 53 | |
| 54 | sockets.append(sock) |
| 55 | socket_names.append(socket_name) |
| 56 | |
| 57 | if close: |
| 58 | for s in sockets: |
| 59 | s.close() |
| 60 | return socket_names |
| 61 | |
| 62 | |
| 63 | def get_socket_name(close=False): |