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]

      

+9


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)
server.serve_forever()

      

+6


source


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 https://bugs.python.org/issue24209 . Or just follow these steps:

Add the lines after +

to the file /your/path/to/python/Lib/http/server.py

. Please note that lines without +

are source code server.py

.

    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*

      

+6


source


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 .

+5


source


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

myserver.py:

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

class HTTPServerV6(HTTPServer):
    address_family = socket.AF_INET6

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

      

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

0


source







All Articles