Python 3: Does http.server support ipv6?

Does http.server

( http

Python 3.x module) support ipv6? For example, using this command line code (which starts the web server):

python -m http.server [port]



source to share

4 answers

Yes. When defining your server, do it as shown here .

import socket
from http.server import HTTPServer

class HTTPServerV6(HTTPServer):
    address_family = socket.AF_INET6


and then listen like this:

server = HTTPServerV6(('::', 8080), MyHandler)




There is a patch that allows IPv6 to bind http.server

in Python 3. I tried it, finding that it works on my laptop. For more information visit . Or just follow these steps:

Add the lines after +

to the file /your/path/to/python/Lib/http/

. Please note that lines without +

are source code


    server_address = (bind, port)

+   if ':' in bind:
+       ServerClass.address_family = socket.AF_INET6
    HandlerClass.protocal_version = protocol    
    httpd = ServerClass(server_address, HandlerClass)


Then try:

python -m http.server -b *your-ipv6-addr* *your-port*




Since Python 3.8, python -m http.server

supports IPv6 (see documentation and bug report for implementation history ).

To listen to all available interfaces:

python -m http.server --bind ::


Python 3.8 released on 2019-10-14 .



Oliver Bock's version of Python 3 (up to 3.8) looks like this:

from http.server import HTTPServer
from http.server import SimpleHTTPRequestHandler
import socket

class HTTPServerV6(HTTPServer):
    address_family = socket.AF_INET6

server = HTTPServerV6(('::', 8080), SimpleHTTPRequestHandler)


Modifying your internal Python 3 files like Edward Zhang looks pretty extreme.



All Articles