preface
python falcon is a framework closer to python wsgi. It has faster speed and higher performance than falsk and Django.
But the disadvantage is that no one uses it, no one uses it and no one uses it. In addition, it is too young and close to the bottom. Many things have to be written by itself. There are not enough instances of official documents. Compared with flash and Django, community experience is as rare as programmer's hair. Of course, everyone has different feelings and definitions of 'more' or 'less'. As in Baidu search, you copy me, and I copy your experience articles will not work.
If you want to use it to develop actual projects, I advise you to consider running away. Come on!
If you want to use it to learn about Python WSGI and the Internet, you can try it.
This article only uses Gunicorn, which is suitable for small / small websites. If you encounter large websites, you'd better add Nginx honestly.
Tag: linux systemctl service python falcon gunicorn
Installation and setting
1. Install Python 3 and pip
Please refer to the resource article on the network. It doesn't matter to install it with yum and source code. Python 3 is recommended seven
2. Install virtual environment
cd to your file directory. Here is an example
cd /var/local/Falcon
Install virtualenv (venv creator)
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple virtualenv
Create venv
virtualenv --python=python3 venv
Enable venv
source ./venv/bin/activate
Setting environment variables is not required for this project
skip
Installing falcon and gunicorn
pip3 install falcon gunicorn
3. Firewall and security settings
- Selinux settings. Save trouble and close directly - > reference Baidu experience - how to turn off selinux in Linux
You can't just turn it off when you get paid - Set up a firewall. Detailed reference CSDN - view status and some basic operations of Linux Firewall
# Query whether port 80 is open firewall-cmd --query-port=80/tcp # Open port 80 firewall-cmd --permanent --add-port=80/tcp # Remove port firewall-cmd --permanent --remove-port=8080/tcp #Restart the firewall (restart the firewall after modifying the configuration) firewall-cmd --reload
Writing Python programs
1. Write Falcon program
Falcon is used to provide web services, which is equivalent to Flask, Django, etc. Here, take the program of Falcon official document as an example. In fact, after being developed locally, it can be copied directly through SSH.
Note: This Python should not be called falcon Py and the like have the same name as the python class
Sample program:
# /var/local/Falcon/FalconApp.py from wsgiref.simple_server import make_server import falcon class ThingsResource: def on_get(self, req, resp): """Handles GET requests""" resp.status = falcon.HTTP_200 resp.content_type = falcon.MEDIA_TEXT resp.text = ('\nTwo things awe me most, the starry sky ' 'above me and the moral law within me.\n' '\n' ' ~ Immanuel Kant\n\n') app = falcon.App() things = ThingsResource() app.add_route('/things', things) if __name__ == '__main__': with make_server('', 8000, app) as httpd: print('Serving on port 8000...') httpd.serve_forever()
At this point, you can simply run the Web by running the following code, just like flash and so on.
python3 FalconApp.py
After running successfully, the command line will output Serving on port 8000, Browser access http://ip Address / things to access the page (direct access) http://ip The address / (first page) is 404 because the route is not defined). However, such a service will not be available after SSH is disconnected.
2. Preparation of Gunicorn
Gunicorn has two startup methods: one is to directly start the command + parameter to realize the configuration, and the other is to write the configuration file. This article describes how to invoke the configuration file to start.
Create a gunicorn-conf.py, which can be placed anywhere. This article is placed in the same level directory / var/local/Falcon /. The configuration file needs to be Py, otherwise there will be Warning
# gunicorn.conf bind = "0.0.0.0:80" #Listen to port 80 of all ip addresses workers = 4 #Enable 4 threads. Generally, workers = number of CPU cores + 1 is more appropriate backlog = 2048 pidfile = "/var/local/Falcon/gunicorn.pid" accesslog = "/var/local/Falcon/access.log" #User access log location errorlog = "/var/local/Falcon/error.log" #Program exception log location timeout = 30 debug = False capture_output = True
It is best to create an empty file for the log used
touch access.log error.log
At this point, you can run Gunicorn with the following code.
gunicorn --config gunicorn-conf.py FalconApp:app
At this time, the command line has no output and will be empty. Just click ctrl+C. Access with browser http://ip The website can be seen at the address / things, and after SSH is disconnected, persistent services can be provided.
Write service program
Write a * Service system service file. The file name can be customized. It will be used later in systemctl [start/stop/status] service name. Falcon is used here service
vim /usr/lib/systemd/system/falcon.service
Then copy the following:
# /usr/lib/systemd/system/falcon.service [Unit] Description=Falcon (python WSGI) After=network.target [Service] Type=forking WorkingDirectory = /var/local/Falcon PrivateTmp = true Restart = on-failure PIDFile = /var/local/Falcon/gunicorn.pid ExecStart = /var/local/Falcon/venv/bin/gunicorn --config /var/local/Falcon/gunicorn-conf.py FalconApp:app [Install] WantedBy=multi-user.target
Refresh the system service list after saving
systemctl daemon-reload
Then you can control the startup and shutdown of the whole Web server like other system services.
# Start service systemctl start falcon # Shut down service systemctl stop falcon # Restart service systemctl restart falcon # View status systemctl status falcon -l
reference material
CSDN - summarize the five methods of deploying Python + Flash project in linux
Baidu experience - how to turn off selinux in Linux
CSDN - view status and some basic operations of Linux Firewall