Return a local server socket listening on the given port.
(host, port=0, backlog=socket.SOMAXCONN, timeout=None)
| 60 | return host, port |
| 61 | |
| 62 | def create_server(host, port=0, backlog=socket.SOMAXCONN, timeout=None): |
| 63 | """Return a local server socket listening on the given port.""" |
| 64 | |
| 65 | assert backlog > 0 |
| 66 | if host is None: |
| 67 | host = get_default_localhost() |
| 68 | if port is None: |
| 69 | port = 0 |
| 70 | ipv6 = host.count(":") > 1 |
| 71 | |
| 72 | try: |
| 73 | server = _new_sock(ipv6) |
| 74 | if port != 0: |
| 75 | # If binding to a specific port, make sure that the user doesn't have |
| 76 | # to wait until the OS times out the socket to be able to use that port |
| 77 | # again.if the server or the adapter crash or are force-killed. |
| 78 | if sys.platform == "win32": |
| 79 | server.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1) |
| 80 | else: |
| 81 | try: |
| 82 | server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) |
| 83 | except (AttributeError, OSError): # pragma: no cover |
| 84 | pass # Not available everywhere |
| 85 | server.bind((host, port)) |
| 86 | if timeout is not None: |
| 87 | server.settimeout(timeout) |
| 88 | server.listen(backlog) |
| 89 | except Exception: # pragma: no cover |
| 90 | server.close() |
| 91 | raise |
| 92 | return server |
| 93 | |
| 94 | |
| 95 | def create_client(ipv6=False): |