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

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.

Related

How to access flask application outside terminal? [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 trying to run simple hello world flask application. I followed all the steps mentioned here as I am using Ubuntu 18.04.5 LTS.
When I execute command flask run, I got following output:
But I am not able to get application run even after visiting the address http://10.142.0.2:5000. Tried using another IP address as well.
Opened port 5000, but not able to see in the list of open ports. Tried setting new firewall rule through GCP console as well.
If you are connecting via ssh you can use Gunicorn with nginx as a server. Note: They are running in the background(you can use the terminal when they are running.)
If you are trying to remove the port number you can change the port as 80
Are you trying to access your web app from another machine? That won't work because 127.0.0.1 means localhost. Try
flask run --host=0.0.0.0
This will tell flask to listen on all interfaces and not only the loopback address.
See https://flask.palletsprojects.com/en/latest/quickstart/#public-server for details.

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.

Can I bring down a Flask server after deleting the original code?

I made a Flask app and ran it. I then deleted the Flask app's module without stopping the server and closed the command prompt by mistake. However, the server is still running. What should I do?

Quick way to run flask on 0.0.0.0:80 in company's intranet [duplicate]

This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
Closed 3 years ago.
I am noticing that the Flask default server should not be used for the production server.
However, in my case, I just want to share a prototype web application created by Flask in the company's intranet using port80.
I tried to specify port referring to stackoverflow page by the following code.
if __name__ == "__main__":
app.run(host='0.0.0.0',port=80)
But they still run on 127.0.0.1:5000. (refer to screen capture of command prompt)
Does anyone know what I should revise in order to run the flask application using port 80?
Please try to run it with
python <yourfile>.py
flask run might be a cause of your problem
As mentioned in the Documentation, flask run will run the development server on 127.0.0.1:5000 and ignore your app.run:
The run command will start the development server. It replaces the Flask.run() method in most cases.
https://flask.palletsprojects.com/en/1.0.x/cli/#run-the-development-server
So instead of using flask run just execute your script directly with Python.
The correct way to launch this using flask run is by specifying the -h flag:
flask run -h 0.0.0.0

Flask app on plesk/apache server - how to change port?

I'm new to setting up a server for python apps, slowly getting my head round all the tools and config options.
I'd like to configure a testing instance on an existing server that has plesk and apache installed. I managed to set up the python environment, virtualenv, the flask app inclusive database and run it successfully on http://domain.test:5000 however I'd need to remove the port number from the domain.
Gunicorn seems to be the tool for that, however I'm not sure how to go about it as plesk is apparently installed on port 80 - so is there any way to get this configured on that server with some port hiding/masking/redirect or do I need to move to a standalone server?
Additionally I'd like to add a ssl certificate to that domain but one step at the time...
The method run on a Flask application takes a keyword argument port:
from flask import Flask
app = Flask(__name__)
app.run(port=80)
Of course you'll need root privileges to run on port 80

Categories