How ToSetup a Python Flask Server in IONOS? - python

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.

Related

Why is my pycharm flask server not running? [duplicate]

This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
Closed 1 year ago.
I am doing a project on an android app with flask backend on Pycharm and came across an issue failing to connect to flask server. I found out that my server was failing to start properly for some reason even though it shows
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
Error displayed in browser
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
Also noticed I couldn't change ports and it only ran on this address.
Just to clarify a doubt I run an old project, which was working, and found that too failed to run showing same error.
Lastly I just created a fresh project to run the default auto generated code for hello world, and that too showed error.
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
What could be the problem? Does it have something to do with pycharm?
It has to do with the way you actually run Flask. If you just use flask run and you don't have any specific setup in the definition of the flask app inside your code, it will run in development mode, and only be accessible from the machine running it.
Running on http://127.0.0.1:5000/ means that it's only running locally. Look into what the 127.0.0.1 IP address means. Note that localhost is a synonym for this. Wikipedia article here.
If you want the app to be used over local network (IIRC that's needed for an Android app to connect to it during dev) you'll need to change the way you run the Flask app. To copy/paste from the Flask quickstart docs:
Externally Visible Server
If you run the server you will notice that
the server is only accessible from your own computer, not from any
other in the network. This is the default because in debugging mode a
user of the application can execute arbitrary Python code on your
computer.
If you have the debugger disabled or trust the users on your network,
you can make the server publicly available simply by adding
--host=0.0.0.0 to the command line:
$ flask run --host=0.0.0.0
This tells your operating system to listen on all public IPs.

Serve Flask Server Over HTTPS

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.

Use waitress as a production server for Flask

I have written an application in Python/Flask which I need to deploy in production on a Windows Server(unfortunately). I came across a suggestion to use Waitress. It was a simple modification to make the web app use waitress, but my issues remain unsolved.
To server using waitress I have modified the code as below(just a basic example)
from flask import Flask
from waitress import serve
app = Flask(__name__)
#app.route('/')
def hello():
return 'Hello World!'
if __name__ == '__main__':
# app.run()
serve(app, host='0.0.0.0', port=8000)
Now I can run this from the command prompt via(same as without waitress)
python myapp.py
But I cannot use this in production.
How do I make sure that the server keeps running in case someone closes the cmd prompt.
How to make sure when the server is rebooted, the webapp comes up automatically without a user having to login and launch the app again.
If these two basic issues are not solvable then I wonder why waitress claims to be a production ready server :)
Waitress isn't responsible to init your server.
It's just a way to activate WSGI
To init the service after restart just add to windows startup programs.
Validate the server is up there are few ways to do so,the simple running a shell script that check the if the process up and execute it if not
I have just posted a answer to my own similar question here.
I think you will find that it answers this question too :-)

How to run flask python website permanently?

I'm following Flask Quickstart guide and can run my web app via http://myip.com:5000.
One issue is that my web is only accessible as long as I keep my SSH remote connection session - when I sleep/shutdown my PC, the website shutdown too.
How can I make it permanent available?
You need to use a regular web server, such as apache2. You can't use the python server for production purposes. Here is how you do it with apache: http://flask.pocoo.org/docs/0.10/deploying/mod_wsgi/

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