Latest recipes tagged "dualstack"http://code.activestate.com/recipes/tags/dualstack/new/2017-03-05T11:00:27-08:00ActiveState Code RecipesServer supporting IPv4 and IPv6 (Python)
2017-03-05T11:00:27-08:00Giampaolo RodolĂ http://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/578504-server-supporting-ipv4-and-ipv6/
<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="http://bugs.python.org/issue17561" rel="nofollow">http://bugs.python.org/issue17561</a></p>
<p>Expected usage:</p>
<pre class="prettyprint"><code>>>> sock = create_server_sock(("", 8000))
>>> if not has_dual_stack(sock):
... sock.close()
... sock = MultipleSocketsListener([("0.0.0.0", 8000), ("::", 8000)])
>>>
</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>>>> while True:
... conn, addr = sock.accept()
... # handle new connection
</code></pre>
<p>Supports UNIX, Windows, non-blocking sockets and socket timeouts.
Works with Python >= 2.6 and 3.X.</p>