I am trying to set up a server on a windows 10 machine, using Python and Flask, but it is not responding to external requests.
This is my server.py
file:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def index():
return "Hi there"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
When running, it says:
* Running on all addresses (0.0.0.0)
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://127.0.0.1:5000
* Running on http://195.XX.XXX.XXX:5000 (Press CTRL C to quit)
Indeed, if I try to access it from that machine, using 127.0.0.1:5000
or 195.XX.XXX.XXX:5000
, it works correctly.
However, when trying to access it from another machine, (using Chrome if that can be an issue), it just loads indefinitely, then says no data received, ERR_EMPTY_RESPONSE
.
What is wrong with this? I've followed steps on the documentation so I don't get what could be wrong.
I also disabled firewall entirely on the windows 10 machine.
CodePudding user response:
you might just need to open ports, also you may want to route HTTP requests using a webserver, you can use Apache, IIS or NGINX.
Follow this instructions to open firewall ports in Windows 10
You can manually permit a program to access the internet by opening a firewall port. You will need to know what port it uses and the protocol to make this work.
Navigate to Control Panel, System and Security and Windows Firewall.
Select Advanced settings and highlight Inbound Rules in the left pane.
Right click Inbound Rules and select New Rule.
Add the port you need to open and click Next.
Add the protocol (TCP or UDP) and the port number 5000 into the next window and click Next.
Select Allow the connection in the next window and hit Next.
Select the network type as you see fit and click Next.
Name the rule something meaningful and click Finish.
You have now opened a firewall port in Windows 10!
CodePudding user response:
How you're running matters. If it's via
python server.py
then the app.run()
will execute, and the server will bind to 0.0.0.0
But if you're running via some variant of
FLASK_APP=server.py flask run
then app.run()
won't execute, and the server will bind to 127.0.0.1
In that case, adding --host=0.0.0.0
should fix things (unless you're having a firewall issue).