国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python部署web開發程序的幾種方法

2019-11-25 16:12:07
字體:
來源:轉載
供稿:網友

1、fastcgi ,通過flup模塊來支持,在nginx里對應的配置指令是 fastcgi_pass

2、http,nginx使用proxy_pass轉發,這個要求后端appplication必須內置一個能處理高并發的http server,在python的web框架當中,只能選擇tornado.

3、uwsgi,包括4部分組成:

  • uwsgi協議
  • web server內置支持協議模塊
  • application服務器協議支持模塊
  • 進程控制程序

nginx從0.8.4開始內置支持uwsgi協議,uwsgi協議非常簡單,一個4個字節header+一個body,body可以是很多協議的包,比如說http,cgi等(通過header里面字段標示)。

uwsgi的特點在于自帶的進程控制程序.它是用c語言編寫,使用natvie函數,其實和spawn-fcgi/php-fpm類似。所以uwsgi可以支持多種應用框架,包括(python,lua,ruby,erlang,go)等等

4、mod_python,這是apache內置的模塊,很嚴重的依賴于mod_python編譯使用的python版本,和apache配套使用,不推薦

5、cgi,這個太old,不推薦,而且nginx不支持cgi方式,只能用lighttpd或者apache

6、spawn-fcgi,這個是fastcgi多進程管理程序,lighttpd安裝包附帶的,和 flup效果一樣,區別是flup是 python代碼級引入,spawn-fcgi是外部程序。spawn-fcgi用途很廣,可以支持任意語言開發的代碼,php,python,perl,只要你代碼實現了fastcgi接口,它都可以幫你管理你的進程

7、scgi,全名是Simple Common Gateway Interface,也是cgi的替代版本,scgi協議很簡單,我覺得和fastcgi差不多,只是沒有怎么推廣開來,nginx對應的配置指令是scgi_pass,你想用就用,flup也支持。

8、Gunicorn,和uwsgi類似的工具,從rails的部署工具(Unicorn)移植過來的。但是它使用的協議是 WSGI,全稱是Python Web Server Gateway Interface ,這是python2.5時定義的官方標準(PEP 333 ),根紅苗正,而且部署比較簡單,http://gunicorn.org/ 上有詳細教程

9、mod_wsgi,apache的一個module,也是支持WSGI協議,https://code.google.com/p/modwsgi/

uwsgi

安裝uwsgi

pip install uwsgi

配置uwsgi

uwsgi 有多種配置可用:

1,ini 2,xml 3,json4,yaml

配置示例

$ cat etc/uwsgi.ini [uwsgi]socket = 127.0.0.1:9005chdir = /Users/suoning/python_project/trunk/wsgi-file = main.pyprocesses = 4stats = 127.0.0.1:9000daemonize = /tmp/uwsgiServer.logpidfile = /tmp/uwsgi.pidvacuum = truelog-maxsize = 50000000disable-logging = truecallable = app$

配置參數詳解:

常用選項:

socket : 地址和端口號,例如:socket = 127.0.0.1:50000

processes : 開啟的進程數量

workers : 開啟的進程數量,等同于processes(官網的說法是spawn the specified number of  workers / processes)

chdir : 指定運行目錄(chdir to specified directory before apps loading)

wsgi-file : 載入wsgi-file(load .wsgi file)

stats : 在指定的地址上,開啟狀態服務(enable the stats server on the specified address)

threads : 運行線程。由于GIL的存在,我覺得這個真心沒啥用。(run each worker in prethreaded mode with the specified number of threads)

master : 允許主進程存在(enable master process)

daemonize : 使進程在后臺運行,并將日志打到指定的日志文件或者udp服務器(daemonize uWSGI)。實際上最常用的,還是把運行記錄輸出到一個本地文件上。

log-maxsize :以固定的文件大小(單位KB),切割日志文件。 例如:log-maxsize = 50000000  就是50M一個日志文件。

pidfile : 指定pid文件的位置,記錄主進程的pid號。

vacuum : 當服務器退出的時候自動清理環境,刪除unix socket文件和pid文件(try to remove all of the generated file/sockets)

disable-logging : 不記錄請求信息的日志。只記錄錯誤以及uWSGI內部消息到日志中。如果不開啟這項,那么你的日志中會大量出現這種記錄:

[pid: 347|app: 0|req: 106/367] 117.116.122.172 () {52 vars in 961 bytes} [Thu Jul  7 19:20:56 2016] POST /post => generated 65 bytes in 6 msecs (HTTP/1.1 200) 2 headers in 88 bytes (1 switches on core 0)

配置nginx

$ cat etc/nginx/servers/tongbupan.confserver { listen  80; server_name localhost; location / {  include uwsgi_params;  uwsgi_pass 127.0.0.1:9005; } location /webstatic/ {  expires 7d;  add_header Cache-Control public;  alias /Users/suoning/probject/python_project/webstatic/trunk/; }}$ $ nginx -tnginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is oknginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful$ $ nginx -s reload$

配置application

flask 示例

...app = Flask('pan')...if __name__ == '__main__': # app.run(host='0.0.0.0', port=5000) app.run()# 注意:變量app對應uwsgi配置文件uwsgi.ini中 callable = app

啟動uwsgi

$ $ uwsgi --ini /usr/local/etc/uwsgi.ini[uWSGI] getting INI configuration from /usr/local/etc/uwsgi.ini$ $ ps -ef|grep uwsgi11428  1 0 11:40下午 ??   0:01.23 uwsgi --ini /usr/local/etc/uwsgi.ini11432 11428 0 11:40下午 ??   0:00.00 uwsgi --ini /usr/local/etc/uwsgi.ini11433 11428 0 11:40下午 ??   0:00.00 uwsgi --ini /usr/local/etc/uwsgi.ini11434 11428 0 11:40下午 ??   0:00.00 uwsgi --ini /usr/local/etc/uwsgi.ini11435 11428 0 11:40下午 ??   0:00.00 uwsgi --ini /usr/local/etc/uwsgi.ini11440 69240 0 11:40下午 ttys000 0:00.00 grep uwsgi$ $ lsof -i tcp:9000COMMAND PID USER FD TYPE    DEVICE SIZE/OFF NODE NAMEuwsgi 11428 suoning 28u IPv4 0x5583e11534d24e73  0t0 TCP localhost:cslistener (LISTEN)$$ lsof -i tcp:9005COMMAND PID USER FD TYPE    DEVICE SIZE/OFF NODE NAMEuwsgi 11428 suoning 6u IPv4 0x5583e11535699e73  0t0 TCP localhost:9005 (LISTEN)uwsgi 11432 suoning 6u IPv4 0x5583e11535699e73  0t0 TCP localhost:9005 (LISTEN)uwsgi 11433 suoning 6u IPv4 0x5583e11535699e73  0t0 TCP localhost:9005 (LISTEN)uwsgi 11434 suoning 6u IPv4 0x5583e11535699e73  0t0 TCP localhost:9005 (LISTEN)uwsgi 11435 suoning 6u IPv4 0x5583e11535699e73  0t0 TCP localhost:9005 (LISTEN)$

FCGI

參考:http://webpy.org/cookbook/fastcgi-nginx

配置Nginx

$ cat etc/nginx/servers/pan.confserver { listen  80; server_name localhost; error_page 500 502 503 504 /50x.html; location = /50x.html {  root html; } location / {  fastcgi_param REQUEST_METHOD $request_method;  fastcgi_param QUERY_STRING $query_string;  fastcgi_param CONTENT_TYPE $content_type;  fastcgi_param CONTENT_LENGTH $content_length;  fastcgi_param GATEWAY_INTERFACE CGI/1.1;  fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;  fastcgi_param REMOTE_ADDR $remote_addr;  fastcgi_param REMOTE_PORT $remote_port;  fastcgi_param SERVER_ADDR $server_addr;  fastcgi_param SERVER_PORT $server_port;  fastcgi_param SERVER_NAME $server_name;  fastcgi_param SERVER_PROTOCOL $server_protocol;  fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;  fastcgi_param PATH_INFO $fastcgi_script_name;  fastcgi_pass 127.0.0.1:9005; } location /webstatic/ {  expires 7d;  add_header Cache-Control public;  alias /Users/suoning/probject/python_project/webstatic/trunk/; }}$

配置application

簡單示例

from flup.server.fcgi import WSGIServerfrom pan import appWSGIServer( app, bindAddress=(host, port), maxThreads=threads).run()

生產環境示例

#!/usr/bin/env python# -*- coding: utf-8 -*-__author__ = 'suoning'import sysimport argparsefrom flup.server.fcgi import WSGIServerfrom lib.daemon import Daemonfrom pan import appAPP_NAME = 'pan_platform'APP_INST_NAME = '20170501'parser = argparse.ArgumentParser(description=u'Run an pan FastCGI server')parser.add_argument('command', type=str,     help=u'command [start|stop|restart]',     choices=['start', 'stop', 'restart'])parser.add_argument('-p', '--port', type=int,     help=u'port of this server', required=True)parser.add_argument('-t', '--threads', type=int, default=50,     help=u'max number of threads')parser.add_argument('-host', '--host', default='0.0.0.0',     help=u'Listen to the main clause')class panPlatformDaemon(Daemon): def run(self):  # 運行服務  try:   WSGIServer(    app,    bindAddress=(args.host, args.port),    maxThreads=args.threads,    umask=0111   ).run()  except:   sys.stderr.write('oops')def gen_pidfile(port): return '/var/run/%s_%s_%d.pid' % (APP_NAME, APP_INST_NAME, port)if __name__ == '__main__': args = parser.parse_args() daemon = panPlatformDaemon(gen_pidfile(args.port)) if 'start' == args.command:  daemon.start() elif 'stop' == args.command:  daemon.stop() elif 'restart' == args.command:  daemon.restart() else:  print "Unknown command"  sys.exit(2) sys.exit(0)

fastcgi協議和http協議在代碼部署中的的優劣對比

  • fastcgi雖然是二進制協議,相對于http協議,并不節省資源。二進制協議,只能節省數字的表達,比如 1234567,用字符串表示需要7個Byte,用數字就是4個Byte,而字符串到哪里都一樣
  • fastcgi在傳輸數據的時候,為了兼容cgi協議,還要帶上一堆cgi的環境變量,所以和http協議相比,用fastcgi傳輸數據并不省,反而多一些
  • fastcgi 唯一的優點是,它是長連接的,用戶并發1000個request,fastcgi可能就用10個 鏈接轉發給后端的appplication,如果用http協議,那來多少給多少,會向后端appplication 發起1000個請求
  • http代理轉發方式,在面對超高并發的情況下會出問題,因為,tcp協議棧當中,port是int16整型 你本地新建一個connect,需要消耗一個端口,最多能到65536。外部并發幾十萬個請求,port池耗干,你的服務器只能拒絕響應了

CGI, FCGI, SCGI, WSGI 區別

WIKI Links:

CGI - http://en.wikipedia.org/wiki/Common_Gateway_Interface
FCGI - http://en.wikipedia.org/wiki/Fcgi
SCGI - http://en.wikipedia.org/wiki/SCGI
WSGI - http://en.wikipedia.org/wiki/Wsgi 

Other reference:

http://helpful.knobs-dials.com/index.php/CGI%2C_FastCGI%2C_SCGI%2C_WSGI%2C_servlets_and_such#FastCGI_and_SCGI

CGI = Common Gateway Interface

顧名思義,它是一種接口規范。該規范詳細定義了Web服務器中運行的服務器代理程序,怎樣獲取及返回網頁生成過程中,服務器環境上下文和HTTP協議中的參數名稱,如大家所熟知的:REQUEST_METHOD,QUERY_STRING,CONTENT_TYPE等等。絕大部分的Web服務器程序,是以腳本的形式代理接受并處理HTTP請求,返回HTTP頁面或響應。這些腳本程序,就是大家所熟知的PHP、ASP、JSP等等。

FCGI = Fast CGI

它其實是CGI在具體實現中的的一個變種。其設計思路是,通過減少CGI代理程序和Web宿主服務程序的通信開銷,從而達到提高Web服務性能的最終目的。由此可見,FCGI在規范上跟CGI并沒有不同,只是具體實現方式上有所改進:CGI的做法是,對于每個HTTP請求,Web宿主服務程序都建立新的進程以調用服務器腳本,相應該請求;FCGI的做法是,建立一個獨立的FCGI服務程序進程,和Web宿主服務程序進程通信,FCGI服務進程被一旦啟動后,自己分配資源、創建線程響應HTTP請求、并決定自身生命周期,從而大大降低了系統為了創建進程而做出的資源開銷。現代流行的Web服務器程序,如PHP、ASP.Net,基本都是FCGI的實現。

SCGI = Simple CGI

它是FCGI在精簡數據協議和響應過程后的產物。其設計目的是為了適應越來越多基于AJAX或REST的HTTP請求,而做出更快更簡潔的應答。并且SCGI約定,當服務器返回對一個HTTP協議請求響應后,立刻關閉該HTTP連接。所以不難看出,SCGI更加適合于普遍意義上SOA所提倡的“請求-忘記”這種通信模式。

WSGI = Web Server Gateway Interface

此協議是Python語言的專利,它定義了一組在Web服務宿主程序和HTTP響應代理程序之間通信的普遍適用的接口。它的產生是因為Python程序員注意到,對于Web框架和Web宿主服務器程序間,有嚴重的耦合性,比如說,某些框架是針對Apache的mod_python設計的。于是,WSGI就定義了一套非常低級別的接口。常見的Python Web框架都實現了這個協議:如 CherryPy, Django, web.py, web2py, TurboGears, Tornado, Pylons, BlueBream, Google App Engine[dubious

主站蜘蛛池模板: 宽城| 江川县| 阿鲁科尔沁旗| 永吉县| 福清市| 区。| 易门县| 南安市| 珲春市| 廊坊市| 西乌珠穆沁旗| 资讯 | 南陵县| 石狮市| 夏邑县| 桃江县| 卢龙县| 固阳县| 陵水| 开鲁县| 元朗区| 舞阳县| 丰镇市| 辰溪县| 石楼县| 长岭县| 蒙阴县| 同江市| 博兴县| 花垣县| 新晃| 洛南县| 西畴县| 自贡市| 元阳县| 依兰县| 栾川县| 拉萨市| 安泽县| 拜泉县| 台中县|