I'm trying to create a control system that will run on a Raspberry PI configured as a server. I'll have a python program running on the server that will control a process (i.e. temperature).
My question is how would I go about communicating the the running python program via a web browser? I'd like to be able to set a control point and get back the current process value.
I'm new to web development and have been looking at PHP and CGI, but that doesn't feel right to me. Someone also suggested communicating via SQL, where a script and the python program would both have access, but this doesn't feel right either?
What is the preferred method for doing this?
Thanks
The easiest thing would be install Flask:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def main():
return "Hey cool, it works!"
if __name__ == "__main__":
app.run("0.0.0.0", port=80, debug=True) # Might have to run as sudo for port 80
Related
I created a droplet that runs a flask application. My question is when I ssh into the droplet and restart the apache2 server, do I have to keep the console open all the time (that is I should not shut down my computer) for the application to be live?
What if I have a dynamic application that runs scripts in the background, do I have to keep the console open all the time for the dynamic parts to work?
P.S:
there's a similar question in SO about a NodeJs app but some parts of the answer they provided are irrelevant to my Flask app.
You can use the "screen" command to mantain the sesion open.
please see https://www.rackaid.com/blog/linux-screen-tutorial-and-how-to/
In my opinion it is not a good practice to use remote computers for the development stage unless you don't have an other option. If you want to make your application available after logging out from the ssh console, screen works, but it still a workaround.
I would suggest taking a look at this great tutorial on how to daemonize flask applications with Gunicorn+Nginx.
You needn't keep the console on, the app will still running after you close the console on your computer. But you may need to set a log to monitor it.
I wrote a simple Flask application on a VPS server in Apache
from flask import Flask, render_template, request
import sys
app = Flask(__name__)
#app.route("/")
def hello():
return "123"
if __name__ == "__main__":
app.run()
When I change the string return "123" to return "123456" and save it, it doesn't change when refreshing the site with Ctrl+F5.
It changes only when I restart the Apache server.
How can I change the file without a restart?
You can try reloading. Even that will impact the incoming traffic though.If there are more than one servers you can do a rolling reload or restart.
If not you can do a graceful restart of apache where traffic wont be impacted, using
apachectl -k graceful
You can do without a restart or reload as well.
The reason why you are getting older code is,
you might have a compiled python code(bytecode) file which will be served for every new request, if your script is myscript.py, check if there is a myscript.pyc, which is a bytecode containing the older version of the script. You need to remove that.
If you are using wsgi module for serving python in apache, you need to look at the following link on how to reload source code.
https://code.google.com/archive/p/modwsgi/wikis/ReloadingSourceCode.wiki
Currently I'm developing web services in python using web.py, that serves different functions. Something like
BigQueryService.py
LogicImplementer.py
PostgreService.py
Each service works perfectly when running on the local machine. After deploying on the server, due to I'm refering an other python script, it returns a module error.
Since we have to run all the services on the same port, I pasted all the scripts into a single file named Engine and made it to work using the command
$ nohup python Engine.py 8080 &
Is there any better way to structure the service in web.py? Or is there a way to run all the individual scripts on the same port?
If each service creates its own listener/server socket on the port, then the answer is no. You will need to use the equivalent of an app server that has a single server port and distributes the incoming request to the relevant app (running on the server) based typically on the relative path - so e.g. http://myserver.net:8080/bqs paths get passed to your BiqQueryService, /li to LinkImplementor, /pgs to PostgreService. Flask will do something like this, I'm sure other web service frameworks will too. The server will handle all the communication stuff, pass requests to the app (e.g. bqs) and handle sending response to the client.
I have an ubuntu EC2 server and want to run a flask server. I want to hit the server using my domain name, api.example.com, without having to include the port number. Right now, I can successfully access the server by doing api.example.com:5000/... but I can't figure out how to do api.example.com/....
Right now I'm just running the flask server directly, using python flask_server.py.
In flask_server.py:
if __name__ == '__main__':
app.run(host=0.0.0.0)
The run method takes a port optional argument:
if __name__ == '__main__':
app.run(host="0.0.0.0", port=80)
You can do this for testing, but for production I highly recommend you read the deployment options section in the documentation which details ways to run flask with various front end WSGI servers.
If you need help understanding how all these components work together and how to set them up; this gist has a nice summary.
Update: The host param needs to be a string.
You need sudo privilege in order to use port 80.
sudo python3 app.py
That will solve the problem.
Correct syntax to use Flask server on port 80 is:
if __name__ == '__main__':
app.run(host='0.0.0.0', port=80)
Keep in mind that you might need super user privileges.
This kind of workaround wound be acceptable if your were still building your application.
If, as I understood, you plan to deploy your app in production then you'd need to do it properly.
Here you find step by step information for Ubuntu, as requested:
https://www.digitalocean.com/community/tutorials/how-to-deploy-a-flask-application-on-an-ubuntu-vps
I am learning Flask.
I was able to run the Hello World tutorial as shown here
Then I tried to build the Flaskr program following the tutorial http://flask.pocoo.org/docs/tutorial/introduction/
I ran into an issue with the Flaskr program accessing the database,specifically "sqlite3.OperationalError
OperationalError: unable to open database file"
so I took a break and went back to seeing if I could run my "Hello World" program.
Now when I go to the url 127.0.0.1:5000/, instead of seeing "hello world" I still see my data base error from the Flaskr program.
It seem like I need to reset the server instance or something? Please help!
kill python task in the task-manager and then run your server
If you're testing or working on multiple projects at the same time, please run each one in a dedicated virtual environment and serve at a different port because by default flask serves at 127.0.0.1:5000.
Use something like this below:
if __name__ == "__main__":
app.run(host='0.0.0.0',port=8001)
You can change the port in each other project and run all of them without any problem.
Happy coding,
J.