connect to a localserver that is running on another server - python

I have server on an external hardware running at port number 162.74.90.100 and i can access all the files and terminal on it using SSH Secure Shell software.
Now i run a Python-Flask server (127.0.0.1:5000) on the existing server (ie. 162.74.90.100 ) which is supposed to run a website.
To access the website, I tried running the IP addresses in a browser like 162.74.90.100/127.0.0.1:5000 but it does not work.
can anyone suggest how can I access 127.0.0.1:5000 using browser? I am stuck and cannot find any relevant documentation.

You can't, not directly. By running on 127.0.0.1 (localhost), you are explicitly not binding to a public IP address and are not visible to the outside world.
Your options are to:
Use SSH port forwarding to redirect traffic from your own machine to that localhost port; add -L 5000:localhost:5000 to your ssh command line and access the Flask server at http://localhost:5000. Use this option if only you should be able to access the server.
Use a 3rd party service like ngrok to tunnel from a public host to your Flask server.
Use another web server serving on a public IP address forwarding connections to localhost:5000. See Proxy Setups in the Flask deployment documentation.
Restart the Flask server to bind to a public IP address, not 127.0.0.1. This is not recommended, as the development web server that comes bundled with Flask is not really suited for the rough world that is the public internet. You can do this by giving app.run() a host argument:
app.run(host='162.74.90.100')
or (using the flask command-line tool) using the --host command-line argument:
flask run --host 162.74.90.100
to bind to your public IP address, or use 0.0.0.0 to bind to all available IP addresses on your server. This will only work if your server is connected directly to the internet (not behind a router) and the firewall allows connections to the port; you'll need to configure the router and firewall otherwise.

The reason why you are not able access your application is because you are not running it off the interface(162.74.90.100, in your case) where you need to access it from.
Since you are using a flask application, and I am assuming your run code looks something like this...
if __name__ == '__main__':
app.run()
This would by default associate your application to the localhost(127.0.0.1) at port 5000. Now for the application to run on port 5000 exposed to outside world you either do this....
if __name__ == '__main__':
app.run(host='162.74.90.100')
or this...
if __name__ == '__main__':
app.run(host='0.0.0.0')
I would suggest latter which runs the application off all the interfaces, hence being accessible from the outside world. Once you have made this change, you could access your application at 162.74.90.100:5000

Related

How can I fix a localhost refused to connect error? [duplicate]

I'm trying to run a simple web server on a Raspberry Pi with Flask. When I run my Flask app, it says:
running on http://127.0.0.1:5000/
But when I enter this address on my laptop's in Chrome, I get
ERR_CONNECTION_REFUSED
I can open 127.0.0.1:5000 on the Raspberry Pi's browser. What do I need to do to connect from another computer?
Run your app like this:
if __name__ == '__main__':
app.run(host='0.0.0.0')
It will make the server externally visible. If the IP address of the machine is 192.168.X.X then, from the same network you can access it in 5000 port. Like, http://192.168.X.X:5000
when you are running the server via flask run change it to flask run --host=0.0.0.0
to connect, find the IPV4 address of the server that your script is running on. On the same network, go to http://[IPV4 address]:5000
A reason could also be in firewall refusing incoming connections on port 5000. Try:
sudo ufw allow 5000
app.run(host='0.0.0.0',port=5000)
if you run your app in this way then your server will be visible externally.
Steps by Setp:
Run your app by using the following command
app.run(host='0.0.0.0',port=5000)
Go to the window cmd . Type ipconfig and get the get the IPV4 adress suppose your IPV4 address is 192.168.X.X
Go to the mobile browser and type the 192.168.X.X:5000
If you have debug = True inside your app.run(), then it will not be visible to other machines either. Specify host and port inside app.run() without the debug = True.
You will have to run the development server such that it listens to requests on all interfaces and not just the local one
Ask Flask to listen on 0.0.0.0:PORT_NUMBER
or any other port you may choose
On MacOS 12.4 (Monterey) I couldn't load localhost nor my local IP but it worked with both of these:
0.0.0.0
127.0.0.1
Just change the URL in the browser if it loads with "localhost".
Both devices must be connected to same network.
Use app.run(host='0.0.0.0',port=5000) and run with your own Ipv4
address like his http://[Your Ipv4 address]:5000
If you are connecting this with android app then don't forget to
put INTERNET permission in manifest file.
Contrary to popular believe 127.0.0.1 is not the same a localhost.
I solved the issue above by setting 127.0.0.1 explicitly on both ends.
Well i was also here to know answer but i guess i found out problem. The reason is that you may not activated or run flask that's why it is showing error. For that you have to start from terminal "flask run" and then surely your flask will run...
The issue may occur, if VPN is on, so try to switch it off.

How to view the website of a running Flask server that I am SSHing to?

I am SSHing to a server, where Flask is set up. When I execute the flask application, the terminal says Running on http://0.0.0.0:80/, as is written in the file.
Is there any way to view this site on my local machine? Going to the domain above on my local machine just gives a Site Not Found error.
Do I need to just change the IP, or is this not possible?
In the Internet Protocol Version 4, the address 0.0.0.0 is a non-routable meta-address used to designate an invalid, unknown or non-applicable target...
In the context of servers, 0.0.0.0 means "all IPv4 addresses on the local machine"
-- Wikipedia: 0.0.0.0
Key words being "on the local machine". Which means http://0.0.0.0:80/ is not the actual address of the server, just the address it is "listening on".
You need to use the public IP address of the server that you are accessing. If you SSH'd into this machine, you should be able to use the same IP address or hostname to access the now running webserver.

How to make flask server running in a VM externally available?

If I run my flask app on my local machine I get proper results by connecting to http://127.0.0.1:5000/report?id=1
But now I want to make it externally visible by deploying my flask in a VM in azure. I have opened the port 80 on my VM. And I'm running the flask app using this:
if __name__ == '__main__':
app.run(host='0.0.0.0')
I'm still not able to connect to my flask server using this (assume the public IP address of my VM is x.x.x.x):
http://x.x.x.x:5000/report?id=1
Any suggestions how should I go ahead with it?
Edit: I'm able to psping my VM's public IP address on port 80.
The problem isn't related to Flask, since you opened up your application to listen on any public IP (0.0.0.0).
Moreover you should do a proper port mapping in your azure configuration. Google said, you might have a look here: https://azure.microsoft.com/en-us/documentation/articles/virtual-machines-set-up-endpoints/
EDIT
Another idea, where some colleages often run into, is that you may have skype open which somewhat uses port 80/443 and therefore is blocking it. Shutdown skype if you do so or use a different port for your webapp.
So here's the deal with Azure:
If you open a port from the Azure portal, the firewall in your VM STILL blocks that port. You have to manually go in and create a firewall setting in your VM to keep the port 5000 open for your flask server. Once that is done, you should be able to connect to it.

Flask isn't recognising connections from other clients

I have an apache server setup on a Pi, and i'm trying to learn Flask. I set it up so that The 'view' from the index '/' returns "hello world". then i ran my main program. nothing happens from the browser on the PC i'm SSH'ing from,I just get an error saying , but when i used the Pi directly and went to http:localhost:5000/ i got a response.I read about setting Host to '0.0.0.0' but that didnt help. how can i get my Flask to accept all connections? does it make a difference that I have an 'index.html' in '/'?
you need to configure your firewall on your server/workstation to allow connections on port 5000. setting the ip to 0.0.0.0 allows connections to your machine but only if you have the port open. also, you will need to connect via the ip of your machine and not localhost since localhost will only work from the machine where the server is running.

How do I access bottle development server from another PC on the LAN?

I'm running the bottle.py tutorial on one PC, and I was able to access it using
http://localhost:8080/hello/world
However, when I tried to access it (IP address is 192.168.1.10) from another PC on the LAN, using
http://192.168.1.10:8080/hello/world
I received the "Cannot Open Page" error.
I have the Apache web server running on the PC, and I can access the web server without any problem using
http://192.168.1.10
Any suggestions? Thanks.
Assuming you're talking about the Quickstart: “Hello World” example:
Change this line:
run(host='localhost', port=8080, debug=True)
To bind to the public IPv4 address of your computer:
run(host='192.168.1.10', port=8080, debug=True)
Or to this to listen on all interfaces including the external one [Source: bottle.run, Bottle API Reference]:
run(host='0.0.0.0', port=8080, debug=True)
Then you should be able to access http://192.168.1.10:8080/hello/world from your local PC as well as another PC on the LAN. Alternatively use a Fully Qualified Domain Name (FQDN).
If connections are still refused, check your firewall settings.

Categories