gpt4 book ai didi

WINDOWS 上的 Python flask 双栈(ipv4 和 ipv6)

转载 作者:可可西里 更新时间:2023-11-01 10:10:41 58 4
gpt4 key购买 nike

我在 Windows 10 上使用 python Flask。它适用于 ipv4 或 ipv6,具体取决于我绑定(bind)的 ip,但不能同时使用。

以这个例子:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
return "Hello World! <strong>I am learning Flask</strong>", 200

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

我得到 this

以这个例子:

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
return "Hello World! <strong>I am learning Flask</strong>", 200

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

我得到 this

当我运行 minecraft 服务器并将其绑定(bind)到“::”时,我得到 this

我在 this 中看到发布 Flask 在绑定(bind)到“::”时在 Linux 上执行双堆栈。我想知道是否可以让 de Flask 应用程序同时收听 ipv4 和 ipv6 地址。谢谢。

附加信息:Python 套接字模块在 Windows 上不支持双栈(我认为在 Linux 上支持)。我找到了 this并设法用这个例子创建一个双栈套接字:

import socket
from recipe1 import has_dual_stack, create_server_sock, MultipleSocketsListener
tcp = MultipleSocketsListener([("0.0.0.0", 5000), ("::", 5000)])
while True:
con, cliente = tcp.accept()
print ('Concetado por', cliente)
while True:
msg = con.recv(1024)
if not msg: break
print (cliente, msg)
print ('Finalizando conexao do cliente', cliente)
con.close()

result它确实有效,但我不知道这个套接字是否可以与 Flask 一起使用。

最佳答案

根据 [PalletProjects.Flask]: run(host=None, port=None, debug=None, load_dotenv=True, **options) :

Do not use run() in a production setting. It is not intended to meet security and performance requirements for a production server. Instead, see Deployment Options for WSGI server recommendations.

NGINX 知道如何处理这种情况。

在开发模式下,我不知道为什么监听所有地址如此重要,因为可以测试一次监听一个地址。

我没有看到任何简单的方法来完成这项工作。请注意,在 Lnx 上,事情似乎更容易,因为 IPv4 映射 IPv6 地址是通过 net.ipv6 控制的。 bindv6onl 设置。

但是有很多方法可以解决这个问题,这里有一个在新进程中为每个监听 IP 地址执行当前文件(本身)的方法(并在线程中执行,因为(子)进程阻止执行)。

code00.py:

#!/usr/bin/env python3

import sys
from flask import Flask
import threading
import subprocess


app = Flask(__name__)


def run_flask(host):
return subprocess.call([sys.executable, sys.argv[0], host])


@app.route("/")
def hello_world():
return "Hello World! <strong>I am learning Flask</strong>", 200


def main(argv):
port = 5000
debug = True

if argv:
app.run(host=argv[0], port=port, debug=debug)
else:
hosts = [
"127.0.0.1",
"::1",
]

threads = list()
for host in hosts:
threads.append(threading.Thread(target=run_flask, args=(host,)))

for idx, thread in enumerate(threads):
print("Starting on {0:s}:{1:d}".format(hosts[idx], port))
thread.start()


if __name__ == "__main__":
print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
main(sys.argv[1:])
print("\nDone.")

输出(有点混杂):

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q057881991]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code00.py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32

Starting on 127.0.0.1:5000
Starting on ::1:5000

Done.
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32

Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32

* Serving Flask app "code00" (lazy loading)
* Serving Flask app "code00" (lazy loading)
* Environment: production
* Environment: production
WARNING: Do not use the development server in a production environment. WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.

* Debug mode: on
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
* Restarting with stat
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] 64bit on win32


* Debugger is active!
* Debugger is active!
* Debugger PIN: 566-002-078
* Debugger PIN: 566-002-078
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
* Running on http://[::1]:5000/ (Press CTRL+C to quit)

如上所示,服务器开始监听给定的地址(您可以删除 print 调用,以减少输出)。另外(在另一个 cmd 上):

[cfati@CFATI-5510-0:C:\WINDOWS\system32]> netstat -an | findstr 5000
TCP 127.0.0.1:5000 0.0.0.0:0 LISTENING
TCP [::1]:5000 [::]:0 LISTENING

您也可以在 OS 级别操作,方法是使用 /etc/hosts 文件,但我没有对此进行测试。

关于WINDOWS 上的 Python flask 双栈(ipv4 和 ipv6),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57881991/

58 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com