最新消息:20210917 已从crifan.com换到crifan.org

[整理]uwsgi作用和语法

Server crifan 3646浏览 0评论

折腾:

[已解决]已安装Flask的虚拟环境中无法正常运行uwsgi

期间,还是先去了解一下,uwsgi是啥

以及具体的语法是啥

[总结]

概述:

WSGI是Python的规范

uWSGI是实现了WSGI的(应用)服务器

目前还存在有其它的,实现了WSGI的(application或web)服务器

但是其中只有很少的是application的服务器,比如CherryPy(只不过巧了,CherryPy也同时实现了web服务器)

详细解释:

1.WSGI

首先对于:

Web Server Gateway Interface – Wikipedia, the free encyclopedia

WSGI==Web Server Gateway Interface

The Web Server Gateway Interface (WSGI) is a specification for simple and universal interface between web servers and web applications or frameworks for the Python programming language.

WSGI was created as a low-level interface between web servers and web applications or frameworks to promote common ground for portable web application development.

WSGI

-》是个协议

-》专门针对于Python语言的

-》定义了简洁和统一的接口

-》定义了在web服务器(web server)和web应用(web application)或web框架 之间的接口

-》

典型的web服务器==服务器端sever side的软件

  • Apache
  • Microsoft IIS
  • nginx
  • Google GWS

典型的Web应用==(运行在浏览器内的)客户端client side的软件==

  • 网络邮件webmail
  • 在线商城online retail sales
  • 在线拍卖online auctions
  • 维基百科wikis
  • 即时消息通讯instant messaging services
  • Word编辑器
  • 项目管理
  • …. 

典型的web框架==web framework (WF)==web application framework (WAF)

  • 按照不同端去分:
    • 服务器端的web框架:
      • Django
      • Zend Framework
      • Ruby on Rails
    • 客户端的web框架:
      • Backbone.js
      • AngularJS
      • EmberJS
      • ReactJS
      • Vue.js
  • 按照不同语言和平台去分:
    • PHP
      • Zend framework
      • Yii
      • ThinkPHP
      • Symfony
      • Laravel
      • Drupal
      • CodeIgniter
      • WindFramework
      • CakePHP
    • Javascript
      • AngularJS
      • Backbone.js
      • Dojo Toolkit
      • jQuery
      • Prototype
    • Python
      • Django
      • Flask
      • TurboGears
      • web2py
      • CherryPy
      • Zope
      • Pylons
      • Pyramid
    • Ruby
      • Ruby On Rails
      • Sinatra
    • Java
      • Spring
      • Struts
      • Hibernate
      • ZK

uwsgi

uwsgi(注意全部是小写)是一种协议,衍生自SCGI

uwsgi同WSGI一样是一种通信协议:

4个字节的头,加上数据

2.uWSGI

而uWSGI是个Application Server,不是Web Server

uWSGI是实现了uwsgi和WSGI两种协议(及其它协议)的Application Server。

uWSGI内部也带了一个Web Server。

但是更常见的用法是:和nginx搭配一起使用。

其是通过uwsgi这个协议去实现,插入nginx,一起使用的

-》意味着uWSGI不必执行http解析了

-》让web服务器将请求,(根据uwsgi 的协议)以二进制的形式,传递给uWSGI

uWSGI是你开发网络应用的一把瑞士军刀。

uWSGI 通常和 HTTP,FastCGI,SCGI 以及它自己特有的协议 “uwsgi”通信。 性能最高的协议显然是 uwsgi,并且早已被 nginx 和 Cherokee 支持 (同时 Apache 也有许多可用的模块)。

uWSGI(项目)目标在于:开发出一个完整的栈

用于开发宿主服务(hosting services)的栈(stack)

-》

所以uWSGI是个大集合,包含了一堆的东西:

  • 核心Core:
    • 配置configuration
    • sockets creation
    • 进程管理器 processes manager
    • 监视器monitor
    • 日志logging
    • 共享内存shared memory areas
    • 进程间通讯ipc
    • 集群cluster membership
    • uWSGI Subscription Server:键值对的映射系统
  • 请求插件Request plugins:
    • 应用服务器(Application Server),支持多种语言和平台:
      • WSGI
      • PSGI
      • Rack
      • Lua WSAPI
      • CGI
      • PHP
      • Go
  • 网关Gateways:
    • 负载平衡load balancers
    • 代理 proxy
    • 路由routers
  • Emperor:
    • 负责管理和监控大量的实例
  • 循环引擎Loop engines
    • 事件events
    • 并发concurrency
    • components可以以多种方式运行:
      • preforking
      • threaded
      • asynchronous/evented
      • green thread/coroutine
    • 支持多种技术:
      • uGreen
      • Greenlet
      • Stackless
      • Gevent
      • Coro::AnyEvent
      • Tornado
      • Goroutines
      • Fibers 

-》uWSGI设计的架构就是插件式的,可插入式的pluggble

-》可以被扩展

-》扩展支持更多其它的语言和平台

-》你可以去用不同语言( C, C++ and Objective-C)去写插件以添加更多功能

对应的:

uWSGI的名字的由来:

uWSGI =u + WSGI

u

-》估计是对应着universal,即通用的,使用广泛的

-》希望自己这个uWSGI这个项目被大家喜爱,让大家觉得有用,被广泛的使用

-》希望被广泛用于创建各种web服务系统

WSGI

-》来自于Python的规范:WSGI

-》是uWSGI第一个支持的插件就是WSGI

uWSGI 的特点/优点:

  • 多样性Versatility
  • 性能好performance
  • 占用资源少low-resource usage
  • 可靠性高reliability

uWSGI针对很多种不同的协议和语言(,…),实现了统一的接口(不同语言的API都是一样的)和类似的配置方式的(配置选项都一致,支持ini,xml等多种配置方式)

uWSGI的语法和用法

自己通过:

uwsgi–help

看到一些前面用过的参数的解释:

    -s|–socket                            bind to the specified UNIX/TCP socket using default protocol
    -s|–uwsgi-socket                      bind to the specified UNIX/TCP socket using uwsgi protocol
    –suwsgi-socket                        bind to the specified UNIX/TCP socket using uwsgi protocol over SSL
    –ssl-socket                           bind to the specified UNIX/TCP socket using uwsgi protocol over SSL
    –http-socket                          bind to the specified UNIX/TCP socket using HTTP protocol
    –http-socket-modifier1                force the specified modifier1 when using HTTP protocol
    –http-socket-modifier2                force the specified modifier2 when using HTTP protocol
    –https-socket                         bind to the specified UNIX/TCP socket using HTTPS protocol
    –wsgi-file                            load .wsgi file
    –file                                 load .wsgi file
   -w|–module                            load a WSGI module
    -w|–wsgi                              load a WSGI module
    –callable                             set default WSGI callable name
    -H|–home                              set PYTHONHOME/virtualenv
    -H|–virtualenv                        set PYTHONHOME/virtualenv
    -H|–venv                              set PYTHONHOME/virtualenv
    -H|–pyhome                            set PYTHONHOME/virtualenv
    –pythonpath                           add directory (or glob) to pythonpath
    –python-path                          add directory (or glob) to pythonpath
    –pyshell-oneshot                      run an interactive python shell in the uWSGI environment (one-shot variant)
    –python                               run a python script in the uWSGI environment
    –py                                   run a python script in the uWSGI environment
    –pyrun                                run a python script in the uWSGI environment

关于uWSGI的参数,详见官网:

uWSGI Options — uWSGI 2.0 documentation

里面解释的更全。

Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。

参考资料

python – WSGI vs uWSGi with Nginx – Stack Overflow

Python/WSGI 应用快速入门 — uWSGI 2.0 文档

The uWSGI project — uWSGI 2.0 documentation

Blog – uWSGI for great good | Skimlinks

uwsgi  uWSGI

The uwsgi Protocol — uWSGI 2.0 documentation

Frequently Asked Questions (FAQ) — uWSGI 2.0 documentation

转载请注明:在路上 » [整理]uwsgi作用和语法

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
91 queries in 0.187 seconds, using 23.35MB memory