Latest recipes tagged "ipv6" Code RecipesServer supporting IPv4 and IPv6 (Python) 2017-03-05T11:00:27-08:00Giampaolo RodolĂ <p style="color: grey"> Python recipe 578504 by <a href="/recipes/users/4178764/">Giampaolo RodolĂ </a> (<a href="/recipes/tags/dualstack/">dualstack</a>, <a href="/recipes/tags/ipv4/">ipv4</a>, <a href="/recipes/tags/ipv6/">ipv6</a>, <a href="/recipes/tags/ipv6_v6only/">ipv6_v6only</a>, <a href="/recipes/tags/networking/">networking</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/socket/">socket</a>). Revision 13. </p> <p>Utility functions to create a single server socket which able to listen on both IPv4 and IPv6. Inspired by: <a href="" rel="nofollow"></a></p> <p>Expected usage:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; sock = create_server_sock(("", 8000)) &gt;&gt;&gt; if not has_dual_stack(sock): ... sock.close() ... sock = MultipleSocketsListener([("", 8000), ("::", 8000)]) &gt;&gt;&gt; </code></pre> <p>From here on you have a socket which listens on port 8000, all interfaces, serving both IPv4 and IPv6. You can start accepting new connections as usual:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; while True: ... conn, addr = sock.accept() ... # handle new connection </code></pre> <p>Supports UNIX, Windows, non-blocking sockets and socket timeouts. Works with Python &gt;= 2.6 and 3.X.</p>