Flask server not visible from my public ip address - python

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.

Related

How to access Flask test server from the outside of local network? [duplicate]

This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
Closed 3 months ago.
I am doing experiments with requests trough proxy for web-scraping project.
In order to test requests headers and content i've build a simple flask server like this:
from flask import Flask
from flask import request
app = Flask(__name__)
#app.route("/")
def index():
ret_str = f'<- {request} ->\n' \
f'<----- HEADERS ----->\n' \
f'{request.headers}\n' \
f'<----- DATA ----->\n' \
f'{request.data}\n'
print(ret_str)
return ret_str
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)
Which is run perfectly fine if access through localhost by 127.0.0.1:5000 (with app.run()) and through local network by requesting the local address 10.214.14.222:5000 (which is displayed by ipconfig).
But if i try to access it through my 4G connection or through proxy, request fails. According to https://ipchecker.io/, my outside ip is, let's say 216.94.151.186, but requesting my page using 216.94.151.186:5000 gives 'page not found'.
How to access the test server from outside the local network? Can anyone please help?
SOLUTION: Set up port forwarding on router and make computer that run flask server IP-address static on router.
What you'll need to do is make your specified port available on your router. This is done with port forwarding. The location of this tab can switch depending on your router provider but once this is done you can just visit This link which shows your public IP. Then you can just visit: 000.00.000.00:5000 <-- Replace zero's with public IP.
Make sure that port 5000 is opened in your router.
If you have access to linux, you can use nmap to check which ports are open or go to https://portchecker.co/ and check to see if port 5000 is open.

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.

I can't connect to my Bottle server on external devices

I'm trying to get a Bottle server in Python to work. Here is my code:
from bottle import route, run, template
from socket import gethostname, gethostbyname
from time import sleep
ip = str(gethostbyname(gethostname()))
#route('/')
def index():
return 'Hello World!'
run(host=ip, port=1234)
I run this, and on my computer where I'm running it, I navigate to http://127.0.1.1:1234/, and my website shows up, with Hello World!.
However, if I try to connect to it on my phone or my sister's Chromebook, it says that the website refused to connect.
I have tried replacing str(gethostbyname(gethostname())) with '0.0.0.0' and 'localhost', but none have worked.
Get rid of this line; it's not necessary:
ip = str(gethostbyname(gethostname()))
Make your run line look like this:
run(host='0.0.0.0', port=1234)
The address 0.0.0.0 means "listen on all addresses".
Lastly, figure out the network address of the host on which your app is running. Then other devices on the same network should be able to connect to <that ip address>:1234. Devices not on the same network would only be able to connect to the service if you had a publicly routeable address (or if you arranged to forward the appropriate port from a router that has a public address).
You'll want to make sure the system on which your app is running doesn't have firewall rules that would prevent an otherwise successful connection.

Accessing locally hosted site through public ip

I have setup a django app on a apache server in a VM. The site is accessible when I use the apache server IP in the VM browser. It is also accessible from the host browser through it's local IP. But, I cannot access it through the public IP over the internet. I get a site can't be reached error
I have set up port forwarding such that:
1. All router requests on port 80 are forwarded to local IP
2. All requests to local IP on port 80 are forwarded to the VM
I checked if my port is open on my public ip using http://www.yougetsignal.com/tools/open-ports/
It says that my port is closed. Same results with http://canyouseeme.org/
I am able to ping my public ip successfully.
I have tried disabling all my firewalls but this has not helped. Please tell me if you need any code to be shared. Any help would be appreciated.
Edit:
Extra information: It seems my router's WAN IP is different from my public IP. I can access the site through the WAN IP from the host browser but again, I am not able to access it over the internet.
You just need a public IP address or push your application to the hosting (like this for example https://gpdhost.com/offers/).
ToDo: learn DMZ, learn WAN-LAN packet forwarding process, learn TCP/IP routing, learn public and private IP addressing and learn NAT.
Description: http/https connection conversation (client-outside vs your-server):
1) client: in browser write: sharan-site/;
2) get IP by DNS name from public servers? But public servers don't know your ip:dns-name pair...
=> fail
next example:
1) client: 192.168.1.1/ - where IP is your server
2) so where is it?
=> nowhere, it is private IP address! Fail...
Desc+: Port forwarding it is NAT feature. Your router must have public IP address, and you must setup DMZ like scheme in your local network: https://www.cisco.com/c/en/us/support/docs/ip/network-address-translation-nat/13772-12.html
Desc++: that sites check outside tcp/80 port on your router and PC, and it's open, no questions... But this no help for your task.

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.

Categories