Python run flask on https with port 80 - python

I'm trying to run my flask app on localhost with https and port 80. It works, but turns out I need to specify the port in the browser url when navigating to the app.
So, if I go to https://localhost on my browser, the app doesn't show, but if I go to https://localhost:80 the page works.
Is there a way to reach the app without specifying the port when typing the url? Shoudln't it be 80 by default when I don't specify the port?
Thanks in advance!
I'm expecting to reach the website without needing to specify the port when it is 80.

The problem is that you are running your app on port 80(default port for http) with https configured.
When accessing https://localhost the browser tries to access the port 443 (the default port for https), while your app is hosted on port 80.
Changing the hosting port to 443 should allow you to access https://localhost without specifying the port.

Related

django nginx gunicorn config needs to work on ip:port

I have currently config nginx gunicorn everything is working fine i am building a rest api the django should work something like this ip:port but instead if i enter the IP address in the browser the django application is working there is no port used how to change it to run on a specified port.
your nginx use port 80 by default
Port 80 is the port number assigned to commonly used internet communication protocol, Hypertext Transfer Protocol (HTTP). It is the port from which a computer sends and receives Web client-based communication and messages from a Web server and is used to send and receive HTML pages or data.
You can run gunicorn specifying the port. In the example below 8000. And then point nginx to it.
gunicorn application_module --bind=0.0.0.0:8000

Why does Flask use port 5000 locally and 80 when deployed?

I have been testing my Flask app locally and then deploying it on an AWS EC2 instance. Where is the default port defined? If I don't specify any port, it uses port 5000 locally; when deployed it uses port 80. Is it defined in the Flask code or is it part of the web server settings?
Flask's (Werkzeug's) dev server defaults to port 5000 if no port is specified. This is because binding to ports below 1024 requires elevated permissions.
You are not (or if you are, you shouldn't be) using the dev server in production, you're using a real WSGI server and HTTP server, such as uWSGI and Nginx, or Amazon's WSGI handler. The web server, independent of Flask, binds to port 80.

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.

connect to a localserver that is running on another server

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

Python LDAP Authentication from remote web server

I have a django application hosted on webfaction which now has a static/private ip.
Our network in the office is obviously behind a firewall and the AD server is running behind this firewall. From inside the network i can authenticate using python-ldap with the AD's internal IP address and the port 389 and all works well.
When i move this to the hosted webserver i change the ip address and port that has been openend up on our firewall. For simplicity the port we opened up is 389 however the requests to authenticate always timeout. When logged into webfaction and running python from the shell and querying the ipaddress i get webfactional's general ip address rather than my static ip.
Is this whats happening when i try and auth in django? the request comes from the underlying ip address that python is running on rather than the static ip that my firewall is expecting?
Im fairly clueless to all this networking and port mapping so any help would be much appreciated!
Hope that makes sense?
I would recommend against opening the port on the firewall directly to LDAP. Instead I would suggest making an SSH tunnel. This will put the necessary encryptionn around the LDAP traffic. Here is an example.
ssh -N -p 22 username#ldapserver -L 2222/localhost/389
This assumes that the ssh server is running on port 22 of your ldap server, and is accessible from your web host. It will create a tunnel from port 389 on the ldap server to port 2222 on the web host. Then, you configure your django application on the web host to think that the LDAP server is running on localhost port 2222.
There are quite a few components between your hosted django application and your internal AD. You will need to test each to see if everything in the pathways between them is correct.
So your AD server is sitting behind your firewall. Your firewall has ip "a.b.c.d" and all traffic to the firewall ip on port 389 is forwarded to the AD server. I would recommend that you change this to a higher more random port on your firewall, btw. Less scans there.
With the shell access you can test to see if you can reach your network. Have your firewall admin check the firewall logs while you try one of the following (or something similar with python) :
check the route to your firewall (this might not work if webfaction blocks this, otherwise you will see a list of hosts along which your traffic will pass - if there is a firewall on the route somewhere you will see that your connection is lost there as this is dropped by default on most firewalls):
tracert a.b.c.d
do a telnet to your firewall ip on port 389 (the telnet test will allow your firewall admin to see the connection attempts coming in on port 389 in his log. If those do arrive, that means that external comm should work fine):
telnet a.b.c.d 389
Similarly, you need to check that your AD server receives these requests (check your logs) and as well can respond to them. Perhaps your AD server is not set up to talk to the firewall ?

Categories