Flask应用程序中对IPv4和IPv6的双重支持 [英] Dual IPv4 and IPv6 support in Flask applications

查看:355
本文介绍了Flask应用程序中对IPv4和IPv6的双重支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以运行Flask来监听IPv4和IPv6(即双IP堆栈)?据我检查,可以使用以下任何一种在IPv4中运行:

Would it be possible to run Flask to listen in both IPv4 and IPv6 (i.e. dual IP stack)? As far as I checked it is possible to run either in IPv4 using:

app.run(host='0.0.0.0', port=port, debug=True)

或使用IPv6

app.run(host='::', port=port, debug=True)

,但是我没有找到一种同时运行的方法(可能有我的Flask应用程序实例监听IPv4,另一个实例监听IPv6,但是两者都不能监听相同的端口)

but I haven't find a way of running in both at the same time (it is possible to have an instance of my Flask application listening to IPv4 and another instance listening to IPv6, but both cannot listen to the same port).

谢谢!

更新(其他信息):

Sander Steffann发表评论(谢谢!),我已经开始在IPv6中监听我的应用程序:

Followingt Sander Steffann comments (thanks!), I have started my app listening in IPv6:

* Running on http://[::]:1028/
* Restarting with reloader

然后使用IPv6和IPv4卷曲进行测试:

Then test with IPv6 and IPv4 curls:

curl -g [::1]:1028/notify
curl 127.0.0.1:1028/notify

分别获取:

::1 - - [10/Feb/2014 12:04:51] "GET /notify HTTP/1.1" 200 -
::ffff:127.0.0.1 - - [10/Feb/2014 12:05:03] "GET /notify HTTP/1.1" 200 -

我对第二行的解释是某人"(操作系统?Flask所依赖的底层网络库?)正在将IPv4请求转换为IPv6请求.但是,我了解到这与经典双栈设置中本地"支持IPv4并不相同,即我希望有某种链接(这就是我运行应用程序(Running on http://0.0.0.0:1028/)时得到的结果

My interpretation of the second line is that "somebody" (the OS? the underlying networking libraries in which Flask relies upon?) is converting the IPv4 request to IPv6 request. However, I understand that is not the same that supporting IPv4 "natively" in a classical dual stack setup, i.e. I would have expected something linke this (that is what I get when I run my application (Running on http://0.0.0.0:1028/)

127.0.0.1 - - [10/Feb/2014 12:05:03] "GET /notify HTTP/1.1" 200 -

推荐答案

发生的事情是操作系统将传入的IPv4请求自动附加到侦听的IPv6套接字.通过使用::ffff:前缀将IPv4地址映射到IPv6地址.因此,来自127.0.0.1的传入IPv4连接看起来像是来自IPv6地址::ffff:127.0.0.1.

What happens is that the operating system automatically attaches incoming IPv4 requests to the listening IPv6 socket. The IPv4 address is mapped to an IPv6 address by prefixing it with ::ffff:. Therefore an incoming IPv4 connection from 127.0.0.1 looks like it is coming from IPv6 address ::ffff:127.0.0.1.

从客户端的角度来看,它正在与IPv4服务器通信.客户无法分辨出差异.从服务器的角度来看,每个人都在使用IPv6进行连接.操作系统执行IPv4数据包与IPv6软件之间的映射.

From the client's point of view it is talking to an IPv4 server. The client cannot tell the difference. From the server's point of view everybody is connecting using IPv6. The operating system performs the mapping between IPv4 packets and the IPv6 software.

这样做的结果是,您无需手动处理双栈编程即可开发软件.可以为IPv6编写所有软件,并将所有地址作为IPv6地址处理.这样可以简化代码(无需同时具有侦听IPv4和侦听IPv6套接字等),同时仍然为外部"提供完整的双栈体验.

The effect of this is that you can develop software without manually having to deal with dual stack programming. All software can be written for IPv6 and deal with all addresses as IPv6 addresses. This can simplify the code (no need to deal with having both a listening IPv4 and a listening IPv6 sockets etc.) while still providing a full dual-stack experience to the 'outside'.

因此,从系统外部看,您的服务是完全双堆栈的.在应用程序本身中,您将看到用IPv6地址表示的整个世界,就像在日志文件中显示的那样.通常不会造成任何问题.但这可能会影响您处理ACL,日志记录和其他类似方式的方式.

So your service is fully dual-stack as seen from outside your system. In the application itself you will see the whole world represented with IPv6 addresses though, like you show in your log file. That usually doesn't cause any problems. It might affect the way you handle ACLs, logging and other things like that though.

这篇关于Flask应用程序中对IPv4和IPv6的双重支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆