Run python http.server for IPv6 only

Question:

python3 -m http.server --bind :: launches a python web server, as shown in Python 3: Does http.server support ipv6?. However, it also supports IPv4, because I can visit the web server with localhost:8000 (edit: even 127.0.0.1:8000 works). I don’t want that. The python http.server is dual-stack even though I used --bind:

By default, server binds itself to all interfaces. The option -b/–bind specifies a specific address to which it should bind. docs

Is there a way to disable IPv4? I asked it to bind to ::.

Asked By: Ben Butterworth

||

Answers:

There is no way to do this because the support for IPv4 is not provided by Python, but by the OS. I’m not 100% sure though.

More details

The output of lsof -nP -i4TCP | grep LISTEN (open network files/sockets on IPv4) include (notice the IPv6)

Python    30838  ben    3u  IPv6 0xb364e79e7fc44213      0t0  TCP *:8000 (LISTEN)

This is alongside the IPv6 network file lsof -nP -6 (warning: lsof -nP -i6TCP doesn’t show it ):

Python    30838  ben    3u  IPv6 0xb364e79e7fc44213      0t0  TCP *:8000 (LISTEN)

I think the OS creates a network file/socket for IPv4 packets even though Python did not ask for it (for convenience). From my understanding of lsof and the docs:

When an open IPv4 network file’s address is mapped in an IPv6 address,
the open file’s type will be IPv6, not IPv4, and its display will be
selected by ‘6’, not ‘4’.

I’ve just realized this is a very similar "feature" to one I was reading yesterday…

This means it’s no longer a Stack Overflow answer but a Linux one: How do you turn off dual-stack networking for the server?

Answered By: Ben Butterworth
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.