python flask...My computer couldn't display - python

This is code from flask homepages.
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')
app.run(debug=True)
when I run the code then enter http://0.0.0.0:5000/
My computer couldn't display...
what is the problem?

1) That isn't the code on the flask homepage.
2) Calling run() twice won't work. If the first run() will finish executing and get to the second run(), wouldn't the second run() finish executing and get to the end of the program and terminate?
app.run() is defined like this:
run(host=None, port=None, debug=None, **options)
Changed in version 0.10: The default port is now picked from the SERVER_NAME variable.
Parameters:
host – the hostname to listen on. Defaults to '127.0.0.1'.
Set this to '0.0.0.0' to have the server available externally as well.
port – the port of the webserver. Defaults to 5000 or the port defined in the SERVER_NAME config variable if present.
debug – if given, enable or disable debug mode. See debug.
options – the options to be forwarded to the underlying Werkzeug server. See werkzeug.serving.run_simple() for more information.
You can set all those parameters in one app.run() call.
3) From the flask docs:
If you run the server [with app.run()] you will notice that the
server is only accessible from your own computer, not from any other
in the network. This is the default because in debugging mode a user
of the application can execute arbitrary Python code on your computer.
If you have debug disabled or trust the users on your network, you can
make the server publicly available simply by changing the call of the
run() method to look like this:
app.run(host='0.0.0.0')
This tells your operating system to listen on all public IPs.
If you try setting the host to 0.0.0.0, then start your server, flask will display the message:
Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
However, that does not mean that 0.0.0.0 is a host name(= an IP address). 0.0.0.0 means that the server is listening for any public host name(= an IP address) on port 5000. The url http://localhost:5000 will also work.
The bottom line: Don't use the host 0.0.0.0 unless you know what you are doing.

I was facing the same problem and i found out that I had to save any code i had written in all my files then after i run then main.py file the web server showed up. So it might be the same situation, just hit cntr-s and save all code in the different .py files then run it

Related

Access Flask application from local network

I want to be able to run Flask on a LAN connection without connection to the internet. When running the flask application it starts and runs on host = 0.0.0.0:5000 but the site can't be reached from browsers going to 0.0.0.0:5000. Running with an internet connection to the network allows the site to show up. When communicating from another device on the same network sending a post to the Flask's ip and correct endpoint allows for a successful http post request with response when the network does not have internet.
The main function is shown here.
if __name__ == "__main__": port = 5000 app.run(port = port,host = HOST, debug = True)
Bottomline: How do I run flask with a wifi connection that does not have internet to communicate with other devices on the network and show the site at 0.0.0.0:5000? thanks
its 127.0.0.1:5000 from your same pc or the local ip like 192.168.1.x from other pcs on the same lan, check your ip with ipconfig
Change the code to this:
if __name__ == "__main__":
app.run(host='127.0.0.1', debug=True)
127.0.0.1 is the localhost of your computer and does not require internet connections, besides, the address is used to establish an IP connection to the same machine or computer being used by the end-user. So it still can run even without the internet.

Can't preview 'Hello World' application in aws cloud9 c9 python flask

I can't preview this appliation using AWS Cloud9 (c9) python flask:
from flask import Flask
import os
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
host = 'localhost' # '0.0.0.0' #"127.0.0.1" # I tried all of these ip's
if __name__ == '__main__':
app.debug = True
port = int(os.environ.get("PORT", 8080)) # I also tried port was 5000
app.run(host=host, port=port)
# This is the error I got 52.15.73.96 The connection has timed out
#The server at 52.15.73.96 is taking too long to respond.
This is similar to AWS cloud9 timeout when running flask application
Here is the answer: You have to get past the AWS firewall.
You have to
Go into EC2 (from the list of all AWS services)
Click security groups
Click your Cloud9 instance
Click Inbound
Click Edit
Click Add Rule
Add this rule:
For Type, choose Custom TCP Rule. - All Traffic also worked.
For Port Range, type 8080, 8081, or 8082. - If you did 'All Traffic' this will default to all ports.
For Source, choose Anywhere, which looks like 0.0.0.0/0
Here is a screen shot link: https://imgur.com/a/zhQbA
AWS does have this, buried, in their C9 documentation.
https://docs.aws.amazon.com/cloud9/latest/user-guide/app-preview.html#app-preview-share-security-group
In Share a Running Application over the Internet, Step 2: Set Up the Security Group for the Instance
You need to run your server in 0.0.0.0 using port 8080 (or other available C9 ports).
Change your app.run() command to something like this:
app.run(host='0.0.0.0', port=8080, debug=True)
If 8080 doesn’t work, try with 80.
flask run --host=127.0.0.1 --port=8080

Flask server not visible from my public ip address

I'm trying to run a flask server on my desktop PC that is publicly available on the internet. I've done the following:
Set up a static IP address: 192.168.1.11 (http://i.imgur.com/Z9GEBYV.png)
Forwarded port 33 on my router to my static ip address (http://i.imgur.com/KGNQ2Qk.png)
Setup flask to use my static ip and port: 33
I'm using the following code as a test webserver
from flask import Flask, request, redirect
app = Flask(__name__)
#app.route("/")
def hello_world():
return "Test 123 "
if __name__ == "__main__":
app.run(host="0.0.0.0", port="33")
When I open my browser to: http://192.168.1.11:33/ the page displays properly, I see "Test 123"
My problem comes when trying to connect to my webserver from my public ip address When I open my browser to http://xx.xxx.xxx.xx:30 (my ip address) all I see is "this site can't be reached, xx.xxx.xxx.xx refused to connect"
I've looked up all the stack overflow answers, I've done the following:
Turned off windows firewall
Changed host from "192.168.1.11" to "0.0.0.0"
Tried a different port
screenshot of code running and error shown: http://i.imgur.com/a05GvEs.png
My question is: What do I need to do to make my flask server visible from my public ip address?
Do you have DHCP activated on your router?
If yes do you see your host as 192.168.1.11 in there?
You have to use '0.0.0.0' on host, that tells Flask to listen on all addresses.
Try specifying the port with quotes as app.run(host="0.0.0.0", port="33")
change it to app.run(host= '0.0.0.0', port="33") to run on your machines IP address.
Documented on the Flask site under "Externally Visible Server" on the Quickstart page:
http://flask.pocoo.org/docs/0.10/quickstart/#a-minimal-application
Add port forwarding to port 33 in your router
Port forwarding explained here
http://www.howtogeek.com/66214/how-to-forward-ports-on-your-router/
You must give the public ip address/LAN ip address as an argument to app.run method.
When you don't provide host argument, it works fine with http://localhost:8888/ and http://127.0.0.1:888/, but not to access outside the system where you are running the REST services
Following is the example.
app.run(host="192.168.0.29",debug=True, port=8888)
You must try and use the same ip of the development server. Thus, for instance, if the dev server is running on a PC with the address 192.168.1.11
and port 33, other clients must point at the same address: 192.168.1.11:33.
As far as my small experience, it works with the debugger disabled, but I did not check if this is an essential prerequisite.
good luck
Every webservice should be run from different port address.Single service is running from a single port.

Flask - How to make an app externally visible through a router?

The question in short
How do you run a simple Flask-based website visible to the internet, from a host PC which is accessing the internet from a wireless router?
Question details
I would like to make a flask application visible to the internet, as per Quickstart Guide.
If I launch the simple Flask app below, it becomes accessible from a computer on the same network as the host pc, but not from a device connected through the internet through another network.
The problem is similar to the one discussed here and here, with the added element that running from a home pc seems to suggest that external connections point to the xx port of the router, not to the xx port of the host pc, as is suggested in the comments in this post.
What I did
Referencing the code below, here's what I did:
Checked my IP address in Control Panel
disabled all network protection in the antivirus
run `ipconfig /all', being on a windows machine
finally opened a browser in a device connected to another network and pointed it to the appropriate IP:port address
The result is that "The webpage is not available".
Has anybody encountered the same problem? Is this a router issue?
Reference Flask app
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=9000, debug=False)
The basic setup will be to create a rule which will forward request to port 80 and/or port 443 to a destined host in your local network.
Example create NAT(address translation) and port forwarding rule to forward inbound HTTP/S requests to your local network host running your python application.
For example:
app.run(host= '192.168.0.58', port=9000, debug=False)
Your NAT rule should target 192.168.0.58 on port 9000.

How to get CherryPy to listen only on a specific host

I have a flask app that I want to deploy using CherryPy's built in server. I chose CherryPy so that the app can be deployed without having to reverse proxy (ie. nginx in front).
I'm having trouble getting CherryPy to listen for requests on just a single hostname.
Say I'm serving 2 sites: test1.com and test2.com (and have them set in my hosts file to point back to localhost).
My /etc/hosts file:
127.0.0.1 test1.com test2.com
CherryPy is serving test1.com, test2.com doesn't have anything serving it.
My cherrypy file is as follows:
import cherrypy
from my_test_flask_app import app
if __name__ == '__main__':
cherrypy.tree.graft(app, "/")
cherrypy.server.unsubscribe()
server = cherrypy._cpserver.Server()
server.socket_host = "test1.com"
server.socket_port = 8030
server.thread_pool = 30
server.subscribe()
cherrypy.engine.start()
cherrypy.engine.block()
Set up this way, I go to test1.com:8030 on my browser and it works as expected.
But when I go to test2.com:8030, the same app is served. I expected it not to serve anything, since CherryPy isn't set up to listen for test2.com.
To me, it seems that CherryPy is just listening for everything on the given port (8030), and treating the socket_host part as if its 0.0.0.0
Am I missing something here? I've looked through lots of docs and tutorials, but all things suggest that this code snippet should be working as I expected.
Thanks
Here's how you can setup what you want...
root = Root()
RootApp = cherrypy.Application(root)
Domain2App = cherrypy.Application(root)
SecureApp = cherrypy.Application(Secure())
vhost = cherrypy._cpwsgi.VirtualHost(RootApp,
domains={'www.domain2.example': Domain2App,
'www.domain2.example:443': SecureApp,
})
cherrypy.tree.graft(vhost)
https://cherrypy.readthedocs.org/en/3.3.0/refman/_cpwsgi.html#classes
Hope this helps!
You misunderstand the socket listen address - they are IP addresses only, not on DNS names. Set this way, CherryPy listens to the localhost (127.0.0.1) only - try using your Ethernet/Wlan local address and you should get connection refused.
Also, you can wrap your application with a WSGI middleware that checks the Host header for the proper domain, or use CherryPy virtual host facility to check the host header.

Categories