Python 3:http.server是否支持ipv6?

问题描述 投票:7回答:4

http.serverhttp是Python 3.x模块)是否支持ipv6?例如,使用此命令行代码(启动Web服务器):

python -m http.server [port]
python-3.x webserver ipv6
4个回答
6
投票

是的,它确实。定义服务器时,请按照这样做,如here所示。

import socket
from BaseHTTPServer import HTTPServer

class HTTPServerV6(HTTPServer):
    address_family = socket.AF_INET6

然后像这样听:

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

6
投票

在Python 3中有一个允许在http.server中进行IPv6绑定的补丁。我试过它,发现它可以在我的笔记本电脑上运行。请访问https://bugs.python.org/issue24209了解更多信息。或者只需执行以下操作:

+之后的行添加到文件/your/path/to/python/Lib/http/server.py中。请注意,没有+的行是server.py的原始代码。

    server_address = (bind, port)

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

然后尝试:

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

3
投票

从Python 3.8开始,python -m http.server支持IPv6(参见documentationbug report with implementation history)。

要监听所有可用的接口:

python -m http.server --bind ::

Python 3.8目前仍处于开发阶段,3.8.0最终版本是planned for 2019-10-21


0
投票

Oliver Bock的Python 3版本(直到3.8)看起来像这样:

没有server.朋友:

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()

修改你的内部Python 3文件,如Edward Zhang似乎相当极端。

© www.soinside.com 2019 - 2024. All rights reserved.