一、背景
由於工作需要,最近學習了下Python web的開發,現在做一個簡要總結。
選用的Python web架構如下:
Python web框架:Django
應用服務器: uWSGI
代理服務器:Nginx
其中Django是最爲常用的Python web框架,類似的框架還有:Flask、web2py等。
二、Django
安裝Django可在網上搜索資料。
django-admin
安裝完Django後,運行django-admin
,得到如下結果:
創建Python web工程
運行如下命令django-admin startproject HelloWorld
,可創建名爲HelloWorld
的Python web工程,目錄結構如下:
目錄說明:
- HelloWorld: 項目的根目錄。
- manage.py: 一個實用的命令行工具,可讓你以各種方式與該 Django 項目進行交互,例如通過命令行啓動該web項目,通過pycharm啓動該web項目等。
- HelloWorld/init.py: 一個空文件,告訴 Python 該目錄是一個 Python 包。
- HelloWorld/settings.py: 該 Django 項目的設置/配置。
- HelloWorld/urls.py: 該 Django 項目的 URL 聲明; 該web項目提供的所有接口均在這裏配置。
- HelloWorld/wsgi.py: 一個 WSGI 兼容的 Web 服務器的入口,以便運行你的項目。
web項目啓動
命令行窗口下執行如下命令:
python manage.py runserver 0.0.0.0:8000
0.0.0.0 讓其它電腦可連接到開發服務器,8000 爲端口號。如果不說明,那麼端口號默認爲 8000。
打開瀏覽器,輸入http://localhost:8000/
查看效果:
三、uWSGI
uWSGI是一個應用服務器,可以理解爲地位和Tomcat類似,一般和Nginx配合使用,以提供HTTP服務。
uWSGI使用示例
The first WSGI application
Let’s start with a simple “Hello World” example:
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"]
(save it as foobar.py).
As you can see, it is composed of a single Python function. It is called “application” as this is the default function that the uWSGI Python loader will search for (but you can obviously customize it).
Deploy it on HTTP port 9090
Now start uWSGI to run an HTTP server/router passing requests to your WSGI application:
uwsgi --http :9090 --wsgi-file foobar.py
打開瀏覽器輸入http://localhost:9090/
,結果如下
Adding concurrency and monitoring
The first tuning you would like to make is adding concurrency (by default uWSGI starts with a single process and a single thread).
You can add more processes with the --processes option or more threads with the --threads option (or you can have both).
uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2
This will spawn 4 processes (each with 2 threads), a master process (will respawn your processes when they die) and the HTTP router (seen before).
uWSGI配置
仍以上面的配置爲例:
uwsgi --http :9090 --wsgi-file foobar.py --master --processes 4 --threads 2
Argh! What the hell is this?! dealing with such long command lines is unpractical, foolish and error-prone. Never fear! uWSGI supports various configuration styles. In this quickstart we will use .ini files.
[uwsgi]
socket = 127.0.0.1:3031
chdir = /home/foobar/myproject/
wsgi-file = myproject/wsgi.py
processes = 4
threads = 2
stats = 127.0.0.1:9191
A lot better!
Just run it:
uwsgi yourfile.ini
在瀏覽器中訪問,仍可得到正確結果。
uWSGI詳細資料詳見本文末尾中的資料鏈接。
四、Nginx
配置基本和Java web中的Nginx的配置相同,配置文件如下:
# proxy conf
user admin;
worker_rlimit_nofile 100000;
error_log "/data/logs/error_log" warn;
pid /home/admin/logs/tengine.pid;
events {
use epoll;
worker_connections 20480;
}
http {
include mime.types;
default_type application/octet-stream;
root /home/admin/htdocs;
sendfile on;
tcp_nopush on;
server_tokens off;
keepalive_timeout 0;
client_header_timeout 1m;
send_timeout 1m;
client_max_body_size 3m;
error_page 506 http://your_error_page.html;
index index.html index.htm;
log_format proxyformat "$remote_addr $request_time_usec $http_x_readtime [$time_local] \"$request_method http://$host$request_uri\" $status $body_bytes_sent \"$http_referer\" \"$http_user_agent\" \"$md5_encode_cookie_unb\" \"$md5_encode_$cookie_cookie2\" ";
access_log "/data/logs/access_log" proxyformat;
log_not_found off;
gzip on;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_min_length 1024;
gzip_proxied any;
gzip_vary on;
gzip_disable msie6;
gzip_buffers 96 8k;
gzip_types text/xml text/plain text/css application/javascript application/x-javascript application/rss+xml;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Web-Server-Type nginx;
proxy_set_header WL-Proxy-Client-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_buffers 128 8k;
proxy_intercept_errors on;
# fight mhtml/utf-7 bug
hat_content "\r\n";
hat_types text/html text/css;
server {
listen 8080;
server_name localhost;
location /test/tools {
include uwsgi_params;
uwsgi_pass 127.0.0.1:8000;
chunked_transfer_encoding off;
}
}
}
其中include uwsgi_params;中的配置如下:
uwsgi_param QUERY_STRING $query_string;
uwsgi_param REQUEST_METHOD $request_method;
uwsgi_param CONTENT_TYPE $content_type;
uwsgi_param CONTENT_LENGTH $content_length;
uwsgi_param REQUEST_URI $request_uri;
uwsgi_param PATH_INFO $document_uri;
uwsgi_param DOCUMENT_ROOT $document_root;
uwsgi_param SERVER_PROTOCOL $server_protocol;
uwsgi_param HTTPS $https if_not_empty;
uwsgi_param REMOTE_ADDR $remote_addr;
uwsgi_param REMOTE_PORT $remote_port;
uwsgi_param SERVER_PORT $server_port;
uwsgi_param SERVER_NAME $server_name;
include uwsgi_params的作用如下:
五、資料
Django
- https://docs.djangoproject.com/zh-hans/2.2/
- https://www.jianshu.com/p/679dee0a4193
- http://www.runoob.com/django/django-first-app.html