Flask http://0.0.0.0:5000/ not reachable [duplicate] - python

This question already has an answer here:
Unable to connect to Flask local server
(1 answer)
Closed 2 years ago.
Whenever I am running flask python main file, I am getting this ""**Running on http://0.0.0.0:5000/**". When I tap the url I am getting "this site can't be reached". Can anyone explain and solve this issue please?

The URL that it is displaying is not reachable because 0.0.0.0 is just a placeholder. You need to figure out which address it is actually listening on. If you are trying to reach a server from the same machine, try http://localhost:5000/; if you are accessing it over the network, you need to figure out what addresses the server is actually reachable by.
It's not always obvious from within the server itself, though the output of ifconfig or similar probably works at least within the same local network. Try https://whatismyip.com/ or a similar service from within the server if you need to see its publicly routable address (assuming still that there is no firewall or etc in place which is blocking this port from public access).

Please try adding host parameter:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host='localhost') #Or change to your ip, such as '192.168.30.56'
Console out put:
Running on http://localhost:5000/ (Press CTRL+C to quit)

Related

How to change the localhost url of python flask API

I have a python flask api running on my laptop. It starts with a localhost url. Base url is below:
http://localhost:5555/
I am trying to integrate this api with a c++ project but facing few issues with port number. Is it possible to remove the port number and make the base url like below
http://localhost/
Below is the code:
if __name__ == '__main__':
import os
HOST = os.environ.get('SERVER_HOST', 'localhost')
try:
PORT = int(os.environ.get('SERVER_PORT', '5555'))
except ValueError:
PORT = 5555
app.run(HOST, PORT)
If I do app.run(HOST) it still starts with port 5000. Is it not possible to remove port number from the url. Please help. Thanks
Browsers don't show port numbers when you're connecting to the default port for a given protocol (i.e., :80 for http, :443 for https). Running a Flask server on port 80 is doable, but depends on what Operating System you're running on. It's a convention on many operating systems to reserve "low" port numbers (those under 1024) for privileged software, so you'd need to arrange to run Flask in a privileged way.
But you note that the issue is integrating with a C++ project, which hints that you might have a different issue. If the problem you're running in to is that you're unable to make a request from C++ to a local Flask server on localhost:5000, then the issue may be that the HTTP request that the C++ program constructs must include the header Host: localhost:5000.

How to access Flask app from other networks? [duplicate]

This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
Closed 5 years ago.
I am trying to create a Flask app (using Python 2.7) that I can access from across networks, as the title says. I have managed to get the app up and running, and it works successfully all across the Wi-Fi network of my computer where the app is running. I have been accessing it using my internal IP address and port 8000. Whenever I connect from my phone, which is connected to the Wi-Fi, it works. However, when I switch to cellular data and connect from that, it doesn't work, and instead says "took too long to respond". The code for the app itself is as follows:
from flask import Flask, render_template
from GradeScraper import scrapeforgrades
app = Flask(__name__)
#app.route('/')
def home():
scrapeforgrades()
return render_template("Grades.html")
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000, debug=True)
GradeScraper is just a web scraper I have written, to be called in the app. I don't believe that file is the reason for my problems.
I have also tried connecting to the app from my external IP address, but it says "refused to connect". I went to this StackOverflow page I found, and the answer said something about security settings. I didn't understand at all how to change those. The person who asked the question commented at the end "I played around with security settings and finally got it working" which leads me to the conclusion that I should do the same. Unfortunately, I have no idea how to do that. Could anybody point me in the right direction? I'll provide any and all extra information needed.
Thanks,
Me
For a start probably (hopefully!) your router is blocking all incoming connections; even if it didn't, your computer's "internal IP Address" (the one you connect to via WiFi) is not only unreachable from outside your network, but also unroutable (in other words, your cellular's operator network has no idea how to find it).
You should look for a NAT (network Address translation) function in your router, pick an external port (any port will do - I recommend one above 1024) and map that port to your computer's IP address, port 8000.
IF you don't know what you're doing, that is however A Really Bad Idea - you will be immediately targeted by port scanners looking for vulnerabilities.
To expand on the topic...
your Computer's IP will be something like 192.168.1.101, while your router may be something like 192.168.1.1 - the 192.168/16 space is deemed a "private IP space" and unroutable from the "public IP" space (the one where all external services live - for example, from my location, google.com maps to 172.17.6.46.
Your phone has two radios (and thus, logically, not physically, 2 NICs): the WiFi (which will get an address from the DHCP server in your router: something like 192.168.1.102) and the LTE/4G radio (which will have an IP address of choice from your provider - something like 174.94.66.23, or, possibly, an IPv6): the former can get to your local server, but the latter can't.
Your ISP will have assigned to your router's WAN port one of these "public IPs" form its pool (they usually rotate them, unless you paid for a static IP address) - that one is what you need to find out (http://formyip.com being the easiest way) and then point to it from your phone (or an external computer or whatever).
The matter is complicated and, as mentioned, dangerous for the novice - educate yourself about routing and NAT: just be aware that opening up your network and keeping a server running on the public network is pretty scary stuff these days (I wouldn't do it, and I used to be at Google and I am now with another "large well-known" corporation :)
Alternatively, if your router allows, you could setup an OpenVPN connection: that is more secure, but an altogether other level of complexity.
Hope this helps.

Flask server not visible local network

I'm trying to get the server that comes with Flask to run inside my network but I can't get it to work properly, it simply doesn't load.
For test purposes I'm running a simple hello world app using app.run(host='0.0.0.0') and it loads fine from the host but I get a timeout when connecting from another machine.
I tried forcing other ports using app.run(host='0.0.0.0', port='80') but I had no success. Using the IP explicitly like app.run(host='10.X.X.X') didn't help either.
However I don't think it is a network problem since I can successfully do a ping 10.X.X.X from the second machine and get the response.
The question: Can it maybe have anything to do with the IP being in the format "10.X.X.X"? What else can be causing this behavior?

Pyro4 Remote connection blocked

I am using Pyro4 to make a remote connection between a raspberry and a computer. I've tested the code local on my computer. But now I want to use it on the raspberry. Only problem the target machine refused it. Nameserver is set, I can ask for the metadata, client is not giving any error.
Server code:
daemon = Pyro4.core.Daemon("192.168.0.199")
Pyro4.config.HOST = "192.168.0.199"
ns = Pyro4.locateNS()
print ns.lookup("client", return_metadata=True) #this works
callback = MainController()
daemon.register(callback)
vc2 = Pyro4.core.Proxy("PYRONAME:client#192.168.0.199:12345")
Client code:
ns = Pyro4.locateNS()
Pyro4.config.HOST = "192.168.0.199"
uri = daemon.register(VehicleController)
ns.register("client#192.168.0.199:12345", uri)
print "Connection set!"
daemon.requestLoop()
Firewall is also off.
Thanks
The main issue is that the server never runs the daemon request loop and so cannot respond to requests.
But there are a lot of issues with the code as shown:
it is not complete.
you're mixing up server and client responsibilities; why is the client running a deamon? That's the server's job.
you're registering an object with a logical name that appears to be a physical one. That's not how the name server works.
you're registering things in both the client and server.
the server never runs the request loop of the daemon it creates.
what is that 'vc2' proxy doing in the server? Clients are supposed to create proxies to server objects.
it's generally best to set Pyro's config variables before doing anything else, this way you don't have to repeat yourself with the IP address the daemon binds on.
All in all you seem to be confused about various core concepts of Pyro.
Getting a better understanding (have you worked through the tutorial chapter of the manual?) and fixing the code accordingly will likely resolve your issue.
Okay, got some more info
I can connect when I edit my Pyro4 Core URL from obj_ x #0.0.0.0: x to obj_ x #192.168.0.199: x and connect manually. So I guess there is something wrong with the way I register the address to the nameserver.
I'll keep you in touch
Tom

Python. Need to be sure the connection is made from the local machine?

Imagine you have a HTTP server on your local machine, this is a typical Python/Twisted application. This server is used to access your local data, server is used just as a GUI interface. So user can use his web browser or special application ( acts like a web browser ) to access his local data.
Now you want to be sure that only local user who physically sit near this machine get access to the HTTP server.
Also I will have FTP server and it must be protected the same way too.
At the moment I am running such code for my HTTP server:
class LocalSite(server.Site):
def buildProtocol(self, addr):
if addr.host != '127.0.0.1':
print 'WARNING connection from ' + str(addr)
return None
try:
res = server.Site.buildProtocol(self, addr)
except:
res = None
return res
So I am just check the IP address at the moment and I am not sure this is enough.
Is there any ways to emulate local IP from remote machine.?
Well, If a bad guy get access over my OS I have no way to protect - but this is not my deal. My firewall and antivirus should care about this, right?
Anyway, I would like to listen any extra ideas about increase security of such HTTP server.
May be we can use MAC address to verify connection.?
Check the processes on local machine and detect which is actually executes connection?
We can use HTTPS, but in my understanding this acts in opposite direction: this is for user to trust to the server, not server to trust to the user.
Using CAPTCHA is a kind of solution. But I do not like this at all (it strains users) and this will not work for FTP server.
I am also use random port number every time application starts.
The type of internet connection is not defined - this is a p2p application. Any user in the WEB can use my software and it must be protected against remote access.
I believe the way you handled it is good enough. About it being cross-platform, I believe it is as Windows(starting from windows 7) too maps localhost to 127.0.0.1 but for previous versions, you have to define localhost in the main hosts file.

Categories

Resources