I'm using python Flask on Windows 10. It works fine for ipv4 or ipv6, depending on the ip I bind, but not both at the same time.
With this example:
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)
I get this
With this example:
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)
I get this
When I run a minecraft server and bind it to "::", I get this
I saw in this post that Flask does dual-stacking on Linux when binding to "::". I would like to know if it's possible to make de Flask app listen to both ipv4 and ipv6 address at the same time. Thanks.
Additional info: Python socket module does not dual stack on windows (i think it does on Linux). I found this and managed to create a dual stack socket with this example:
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 It does work, but I don't know if this socket can be used with Flask.
According to [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 knows how to handle this exact scenario.
In development mode, I don't know why is so important to listen on all addresses, as things can be tested listening on one at a time.
I didn't see any easy way of making this work. As a note, on Lnx things seem to be easier as the IPv4-mapped IPv6 addresses are controlled via the net.ipv6.bindv6onl
setting.
There are however a number of ways to work things around, here's one that executes the current file (itself) in a new process for each listening IP address (and does it in a thread, since the (child) process blocks the execution).
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.")
Output (it's a bit mixed):
[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)
As seen, the servers start listening on the given addresses (you can remove the print calls, in order to have less output). Also (on another 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
You could also operate at OS level, by playing with the /etc/hosts file, but I didn't test that.