Serve Flask Server Over HTTPS - python

I'm learning AWS and I'm currently trying to deploy a Flask API over HTTPS. I set up an EC2 instance running Apache. I've already set up SSL on the site using ELB, and I tried to deploy flask over HTTPS with the following:
if __name__ == '__main__':
context = ("server.crt", "server.key")
app.run(host="0.0.0.0",port=5000,debug=True,ssl_context=context)
However, I don't think the site is even starting the Flask server properply, as even though everything loads and I receive a message that Flask is running on https://0.0.0.0:5000/, sending a simple GET request over the browser doesn't work as the request just never loads and it eventually times out. It's almost behaving as if there is no server running on port 5000.
On the other hand, when I ran this program over http instead of https, it worked perfectly fine. Can anyone help me out in terms of what I should do? Thank you.

Based on the comments.
The solution to the issue was termination HTTPS connections on the ELB. This way, the communication between ELB and EC2 instance can be conducted using HTTP which is simple and easier to manage on the instances.

Related

How ToSetup a Python Flask Server in IONOS?

I have an ionos server, and I am trying to run a python flask server on it. I connected to it via a Linux Terminal with ssh, however doing python3 main.py runs it locally:
I am new to this, am I doing something wrong? I logged into the ssh with the username and password IONOS gave me in the Web Hosting Essential Page.
I connected to it with SFTP and added this python code in a file named main.py:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def home():
return "Hello, world"
app.run()
This runs it locally, but I can't figure out how to run it on my site. I think I am doing this completely wrong. I tried following the IONOS tutorials, but they don't work.
You try to access it by using a web browser in your computer:
http://your_server_ip:5000
If it can be accessed, everything is good. You can follow this tutorial to deploy it (you can choose a different version same with your version's sever near the top of the tutorial)
If it cannot be accessed, maybe your server IP is a shared IP. Ask IONOS, or use other suppliers such as digitalocean
Flask does require a server to run (apache 2 or Nginx) if you run just app.py you will launch flask server locally which is meant to be used as a way to quickly modify your server files without taking down ur web server. I highly recommend Apache2 as it’s fairly simple to use and very reliable.

Flask flashes appear on development server, but not with uWSGI/Nginx

I have been learning Flask by making a little website and using the built in flask server that runs with python. I have a page where you press a button, and it flashes a message using the flash system inside of flask. These flashes work fine when I am using the built in flask server on my windows machine. However, I have deployed the website to a Linux server, using uWSGI which goes through Nginx. My issue is that when I access this server, the flashes don't work. Most things like loading pages work fine on both servers, but flashing is broken. I don't see any error messages from uWSGI's logs.
The code I am using for the flash is implemented as follows:
flash("Made new post.")
return redirect(url_for("posts"))
The redirect takes me to the correct page, and if I run a print() statement before the redirect the statements are clearly being reached, the flash just doesn't do anything.
The main other issue I am running into is with sessions and trying to store session variables. Nothing happens when I try to do this either. (but it works on my personal machine)
Any ideas why this might be, or at least a way to get an error message from uWSGI?
To properly set cookies (cookies are what make message Flashing work), both nginx and the Flask application need to agree on the server name.
So make sure your server_name in nginx.conf matches SERVER_NAME (or
SESSION_COOKIE_DOMAIN, if set) in your flask configuration.
There are also limits enforced by nginx on the size of cookies, but this should only be a problem if your flashed messages are really large.

Unable to access Flask app hosted on EC2 after adding https support

I have a Flask application currently using http being hosted on an ec2 instance. I want to host my application using https. Currently with http, I am able to access my application (running on 0.0.0.0 port 80) by just accessing the ec2 public IP address (displays Running on http://0.0.0.0:80/, and I am able to use my app at http://IP).
I am following this tutorial to test https support: https://blog.miguelgrinberg.com/post/running-your-flask-application-over-https.
When I add the most basic app.run(ssl_context='adhoc') to my program, the app is now Running on https://0.0.0.0:80/, but I am unable to access my app at https://IP. I opened up my port 403 in my EC2 settings however I am still unable to communicate with my app using https...no response whenever I make a request.
The same no response occurs when I run the application with gunicorn. Am I missing something? I think my requests are well formed, however I am never able to communicate with my app when using https.
Simple way is put your instance behind ALB. ALB is not only load balancer, but also integrate ACM for free.

Slow response from IIS server for my python flask application

I have created a web application in python using the Flask framework. I have hosted this python application in IIS on windows server2012.
when I called this application with URL https://localhost/ on the server machine, I am getting a quick response(less than a second). But when I tried to call the same application from another machine inside the corporate network, it is taking more than 2mins time to get the response. I am calling the web application through the IP address of the server machine i.e XX.XXX.XX.XXX:80.
could anyone answer why is it taking too much time and how to get rid of this latency time?
I have used below resources to create the web application and opening the inbound port.
python deployment:
https://medium.com/#bilalbayasut/deploying-python-web-app-flask-in-windows-server-iis-using-fastcgi-6c1873ae0ad8
Inbound port opening:
https://manage.accuwebhosting.com/knowledgebase/2886/How-to-configure-IIS-to-access-website-using-IP-address.html
Regards,
Naresh.
My problem has been solved, i have tried with host name instead of IP address. It is so fast.
Regards,
Naresh.

Running a flask server

So, probably a dumb question, but I am beginning to learn all this so your feedback will be valuable for me.
The question is: In flask documentation it says start the flask server by entering the command 'python hello.py' and I do it successfully to see the output on localhost:5000. Now, I have a shared hosting plan and if I upload this file over there will i need to initiate the server over there as well like this? If so, when I close the terminal over there, will the flask server shut down (because when I close the terminal on my computer it shuts down the flask server and the results are no more available on localhost:5000)?.. It basically suggests me that I have to keep running the terminal all the time..please tell me what is the basic idea here? Thanks.
What you're asking is how you deploy your app. There are many options, that will depend on your needs, your hosting service, etc.
You should check the flask docs for the options. http://flask.pocoo.org/docs/deploying/
In essence, you'll have your flask app running as a local service on the server, so it's not shut down when you close the terminal, and an HTTP server that somehow proxies requests to that service. I guess the most popular is uWSGI with nginx.
When you upload your code to a remote host, you will need to provide a way to start the server and get things running. How this works is host- and software-dependent. As an example, here is some documentation for how you would fire Flask up on Heroku.

Categories