Why is my Flask app only accessible from other computers? - python

I have a default "Hello World" flask app running on my Windows 10 machine. Here is the code I'm using:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello_world():
return "Hello, World!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
When I access it from another computer on my network (My phone in this case) I get Hello, World! in the browser, and the flask app outputs <my phone ip> - - [25/Feb/2020 15:55:14] "GET / HTTP/1.1" 200 - to the console.
The weird part is that when I try to access localhost:5000 on the computer running the app, the browser hangs, and the flask app outputs 127.0.0.1 - - [25/Feb/2020 15:57:12] "GET / HTTP/1.1" 200 - to the console. It will hang forever.
Do you think this is a problem with my network configuration? I've had weird things happen in the past as a result of having Wireshark installed, Hyper-V, etc.? I've already disabled all virtual adapters other than my wifi.
Edit 1:
If I open another python interpreter I can use requests and it gives me b'Hello, World!' as the response content. Both browsers I've tried, Chrome and MS Edge, hang.
Edit 2:
For now I'm just going to resort to using Postman, since I'm using this to design an API anyway. I'm just confused why this is happening 🤔

Is windows firewall on? The code looks fine. This a network issue. Turn your firewall off to test.

Related

Localhost Flask app crashes after 413 error

Okay so I've realized that something weird is happening when I try to upload more than I specified in my config.py which is:
class Config:
#other configurations
MAX_CONTENT_LENGTH = 10 * 1024 * 1024
When I try to upload more than 10 MB, instead of giving me a 413 Error, application just refuses to connect. My error handler:
#errors.app_errorhandler(413)
def error_413(error):
return render_template('errors/413.html'), 413
My run.py:
from flaskblog import create_app
app = create_app()
if __name__ == "__main__":
app.run(debug=True)
I can see on terminal that I've gotten this error:
"POST /foo/bar HTTP/1.1" 413 -
Although my app seems to be running on the terminal, I can't access it whatsoever. It's just dead on the browser:
ERR_CONNECTION_REFUSED
I tried running it on uWSGI, Werkzeug, other browsers, no luck.
Any idea what's happening?
EDIT: I can access after I restart my computer. But I'm still curious why about this happens.
Also I use Cloud SQL with external IP for more information.
I had a similar error when using the MAX_CONTENT_LENGTH configuration.Did you register the blueprint errors if yes,Please try removing app_ from #errors.app_errorhandler.Like below:
#errors.errorhandler(413)
def error_413(error):
return render_template('errors/413.html'), 413
If this didn't work try removing the MAX_CONTENT_LENGTH line or if you are trying to connect from another device, run your app with the following command flask run --host=0.0.0.0 and then access it on the other device using your router's ip address which usually looks like 192.168.xxx.xxx and the port which your app is running on like 192.168.xxx.xxx:<port_of_your_app>.If this doesn't work it might be an issue with your firewall refusing incoming connections in the port that your app is running on in which case you can run the following command on your terminal sudo ufw allow <YOUR_PORT>.
Okay next time I should read documentations more carefully:
Connection Reset Issue When using the local development server, you
may get a connection reset error instead of a 413 response. You will
get the correct status response when running the app with a production
WSGI server.
from Flask - File uploads.

Python Flask app route is not working well

I tried to find a solution for my problem in other questions but I couldn't.
I downloaded the python flask and made my first flask app and it ran fine.
Here is the code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Hello, world!"
When I ran my second file where I had added an app.route ("/ david") and followed the same procedure again, refreshed it and nothing changed.
That is to say, I was going to / david and I get an URL error
Here is my second file
from flask import Flask
app = Flask(__name__)
#app.route("/")
def index():
return "Hello, world!"
#app.route("/david")
def david():
return "Hello, David!"
I tried the same with other files which have some added routes and the result is the same as the first file
Thanks for your answers, I hope to solve my problem.
You did not run the app. What you did is just create a structure for flask, but did not start the server.
Just add:
app.run()
To the bottom of the file and it will work. It will with start the flask server at http://localhost:5000.
By default, flask runs on port 5000.
It can be changed by:
app.run(host="0.0.0.0", port=xxxx)
0.0.0.0 means it accepts request from anywhere on the port specified.
Make sure you have all the permissions and nothing else is running if you want it to run on port 80.
Hope this helps. Good luck.
I had the same issue. Try first by restarting your IDE; this worked for me. If that doesn't work, try clearing your ports for Windows:
Open Task manager
Click on the “Processe” tab
Enable the "PID" column: View -> Select Columns -> Check the box for PID
Find the PID (in your case, 5000 - flask default port) and click “END PROCESS"

Docker - Can't get user webcam: getUserMedia() no longer works on insecure origins

WHAT WORKS
I created a simple Web Application in Flask that takes care of operating a simple return render_template("index.html") when the root node is accessed by a Web Browser.
# app.py
from flask import Flask, render_template
app = Flask(__name__)
#app.route("/")
def show_index():
return render_template("index.html")
if __name__ == "__main__":
app.run(port=80)
The index.html is a simple page that uses tracking.js in order to get the user webcam and track his/her face in the live video stream.
Opening cmd and typing python app.py results in Running on http://127.0.0.1:80/
Accessing the above mentioned URL results in the correct display of the page, that asks me for permission to use the camera, opens it and correctly tracks my face in the live video feed. So it's all working fine till here.
WHAT DOES NOT WORKS
The problem I'm experiencing arises when I dockerize my application using Docker. docker-machine ip is 192.168.99.100
Opening cmd and typing: docker run -p 4000:80 my_face_track_app results in: Running on http://0.0.0.0:80/
Accessing 192.168.99.100:4000 results in the correct display of index.html but I am not asked anymore for permission on the camera and inspecting the JS console I read the following exception:
getUserMedia() no longer works on insecure origins
Here the full error log:
I know the error is telling me I'm not serving the page in HTTPS.
Has anyone else encountered this problem?
What would be the proper solution to the issue or a possible walkaround?
Any help will be highly appreciated, thank you a lot in advance
WHAT I HAVE TRIED TO DO IN ORDER TO SOLVE THE PROBLEM
Since an HTTPS serving of the page is needed in order for JS to execute the function getUserMedia() I tought about serving my Flask application with an SSL certificate by modifying app.py like this:
# app.py
from flask import Flask, render_template
import OpenSSL
app = Flask(__name__)
#app.route("/")
def show_index():
return render_template("index.html")
if __name__ == "__main__":
app.run(port=80, ssl_context="adhoc")
I then dockerized the app building a new image. Typing:
docker run -p 443:80 facetrackapphttps
Results in
Running on https://127.0.0.1:80
So yeah, here HTTPS is ON: the problem is that the port 80 of the HTTPS Flask App is mapped to the port 443 of the docker-machine ip 192.168.99.100.
Trying to access 192.168.99.100:443 does not work and nothing is shown.
Does anybody have an idea about how to do this?
If your application is bound to 127.0.0.1 inside the container, you're not going to be able to access it from your host. According to the flask docs, flask will bind to 127.0.0.1 by default.
You'll need to modify your service so that it binds to 0.0.0.0 inside the container:
if __name__ == "__main__":
app.run(host='0.0.0.0', port=80, ssl_context="adhoc")

Flask Hello World Program Not working 404 Not Found Error

I am trying to get started with Flask, and I get 404 Not Found error for the hello world program.
The program I use:
from flask import Flask
app = Flask(__name__)
#app.route('/')
#app.route('/index.html')
def hello_world():
return 'Hello, World'
if __name__== '__main__':
app.run(host='localhost')
then I run
$export FLASK_APP = minimal.py
$flask run
Which returns:
* Serving Flask app "minimal"
* Forcing debug mode on
* Restarting with stat
* Debugger is active!
* Debugger PIN 127-804-808
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
When I visit http://localhost:5000 or http://127.0.0.1:5000, I get:
Not Found
The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.
And my host file looks like this:
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
Any idea whats wrong? Thank you
Yeah, you can remove the error 404 not found in following ways:
1: You should write command netstat -a -b to check which port is not free or busy doing something or
2: write in your code app.run(port=4996) instead of app.run() which by default choose port no. 50000.

flask 'hello world' not working

I copy pasted the flask's 'hello world' app from their website and am trying to run it. I get an error message in Chrome saying
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Here is the 'hello world' app straight from flasks website
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.debug = True
app.run()
What I have tried:
-temporarily disabling Avast!
-disabling windows firewall
-ensuring that the flask module is installed
This was working a couple days ago actually...
I don't know why but when I change
app.run()
to
app.run(port=4996)
it starts working. No idea why the default port is throwing an error. Oh well.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def index():
return 'Hello World'
if __name__ == '__name__':
app.run()
app.run(port=5000)
For Windows machines you can use the command in cmd:
set FLASK_APP=python_file.py
flask run
Some other process is running on port 5000. It may be you still have an old Flask process running, with broken code. Or a different web server altogether is running on that port. Shut down that process, or run on a different port.
You can switch to using a different port with the port argument to app.run():
app.run(port=8080)
If you can't figure out what process is still bound to port 5000, use the Windows Resource Monitor or run netstat -a -b from a command line. See How can you find out which process is listening on a port on Windows?
I think you are trying to copy the route generated through your flask program in cmd by pressing ctrl+c which quits your running flask program . i was also doing the same.just try to type the route generated by your flask program on your browser . it will definitely resolve your problem.
Where your python file store is, use cmd and then go on your file store directory, then
set FLASK_APP=filename.py
After this your flask run cmd will work.
from flask import Flask
app = Flask(__name__) # creating app
#app.route('/', methods['GET']) #routing it to the home page
def home(): #function
return "hello world"
app.run(port=5000, debug=true) #function call by the app
Add port and use methods whatever your need is USE GET in your case and try to remove your cache and run the this code it will definitely work.

Categories