1. 简介

Gunicorn(Green Unicorn)是给Unix用的WSGI HTTP 服务器,它与不同的web框架是非常兼容的、易安装、轻、速度快。

2. 示例代码1

def app(environ, start_response):
data = b"Hello World\n"
start_response("200 OK", [
("Content-Type", "test/plain"),
("Content-Length", str(len(data)))
])
return iter([data])

启动

gunicorn -w 4 myapp:app

起来后显示

[2016-12-12 00:20:12 +0000] [11755] [INFO] Starting gunicorn 19.6.0
[2016-12-12 00:20:12 +0000] [11755] [INFO] Listening at: http://127.0.0.1:8000 (11755)
[2016-12-12 00:20:12 +0000] [11755] [INFO] Using worker: sync
[2016-12-12 00:20:12 +0000] [11760] [INFO] Booting worker with pid: 11760
[2016-12-12 00:20:12 +0000] [11761] [INFO] Booting worker with pid: 11761
[2016-12-12 00:20:12 +0000] [11762] [INFO] Booting worker with pid: 11762
[2016-12-12 00:20:12 +0000] [11763] [INFO] Booting worker with pid: 11763

此时,调用http://127.0.0.1:8000

$curl http://127.0.0.1:8000
Hello World

参数说明

-w  处理HTTP请求的worker进程数,以下两种启动方式等价

gunicorn -w 4 myapp:app
gunicorn --workers=4 myapp:app

参考:

  -w INT, --workers INT
The number of worker processes for handling requests.

问题:为何调用 http://ip:8000不行呢, 这个是什么请求呢?

默认有-b参数,参考

  -b ADDRESS, --bind ADDRESS
The socket to bind. [['127.0.0.1:8000']]

以下方式启动就可以用ip的方式启动了

sudo gunicorn -w  -b 0.0.0.0: myapp:app

3. 示例代码2

之前简单的flask方法

from flask import Flask
app = Flask(__name__) @app.route('/hello.world')
def check():
return 'hello world!' if __name__ == '__main__':
app.run()

启动

$sudo gunicorn -b 0.0.0.0:  -w  myapp3:app
[-- :: +] [] [INFO] Starting gunicorn 19.6.
[-- :: +] [] [INFO] Listening at: http://0.0.0.0:300 (21005)
[-- :: +] [] [INFO] Using worker: sync
[-- :: +] [] [INFO] Booting worker with pid:
[-- :: +] [] [INFO] Booting worker with pid:
[-- :: +] [] [INFO] Booting worker with pid:
[-- :: +] [] [INFO] Booting worker with pid:

测试

$curl localhost:/hello.world
hello world!

4. 启动异常:[ERROR] Connection in use: ('127.0.0.1', 8000)

原因之一是之前启动的进程没有杀死。

注:ctrl+z 是挂起进程,但没有终止。ctrl+c是终止进程。

如果使用了ctrl+z再回到进程中可使用fg命令,这样可以用ctrl+c来关闭进程

python Gunicorn的更多相关文章

  1. python gunicorn详解

    Gunicorn是一个unix上被广泛使用的高性能的Python WSGI UNIX HTTP Server.和大多数的web框架兼容,并具有实现简单,轻量级,高性能等特点. gunicorn 安装 ...

  2. gunicorn Python部署应用

    对于flask应用 启动命令为 python app.py 使用gunicorn启动 pip install gunicorn python gunicorn --workers=7 switch_a ...

  3. 利用Haproxy搭建 HTTP 请求走私(Request smuggling)环境

    Haproxy 介绍 HAProxy是一个使用C语言编写的自由及开放源代码软件,其提供高可用性.负载均衡,以及基于TCP和HTTP的应用程序代理. 请求走私(Request smuggling)概念证 ...

  4. virtualenv 环境下 Nginx + Flask + Gunicorn+ Supervisor 搭建 Python Web

    在这篇文章里,我们将搭建一个简单的 Web 应用,在虚拟环境中基于 Flask 框架,用 Gunicorn 做 wsgi 容器,用 Supervisor 管理进程,然后使用 Python 探针来监测应 ...

  5. Python第十三天 django 1.6 导入模板 定义数据模型 访问数据库 GET和POST方法 SimpleCMDB项目 urllib模块 urllib2模块 httplib模块 django和web服务器整合 wsgi模块 gunicorn模块

    Python第十三天   django 1.6   导入模板   定义数据模型   访问数据库   GET和POST方法    SimpleCMDB项目   urllib模块   urllib2模块 ...

  6. python之gunicorn的配置

    https://www.cnblogs.com/cwp-bg/p/8780204.html python常见的web部署搭配nginx+gunicorn,下面记录一下gunicorn的配置使用. 安装 ...

  7. 【Python】Centos + gunicorn+flask 报错ImportError: No module named request

    今天用Python去下载图片,用到了 urllib.request,这个是python3的方法.python2 使用的是urllib2 遇到了这么个问题后台报错,ImportError: No mod ...

  8. python独角兽 Flask + Gunicorn

    1.构建程序运行所需的虚拟环境 安装Miniconda 创建虚拟环境 添加程序运行依赖包 添加Gunicorn依赖 方式一:最简单的使用 easy_install 安装或者更新 方式二:下载源码安装 ...

  9. python web开发c6——阿里云上ubuntu+flask+gunicorn+nginx服务器部署(一)简单测试

    简述 Nginx在服务器部署中的作用 请求通过Nginx实现反向代理,将请求提交给代理服务器.本文中只用了一台服务器,所以是代理到本机. gunicorn的作用 作为服务器代码的容器.接收Nginx的 ...

随机推荐

  1. [译]:Orchard入门——媒体文件的添加与管理

    原文链接:Adding and Managing Media Content 注:此文内容相对较老,实际操作指导性不强,仅适合做研究 当你利用富文本编辑器上传图片时(或者使用XML-RPC客户端,例如 ...

  2. JS控制按钮不能连续被点击

    将下面代码拷贝进一个html文件中就可以运行查看效果. function downLoad(evt) { disabledButton(); MyPeriodicalExecuter(evt); } ...

  3. [开源]用MQL4实现MD5加密

    本文转载自博客园:混沌的世界 原文地址:http://www.cnblogs.com/niniwzw/archive/2009/12/05/1617685.html 在用MQL4进行金融交易的时候,经 ...

  4. IE8+兼容经验小结

    最近一段时间,我都使用Flask+Bootstrap3的框架组合进行开发.本文就是在这种技术组合下,分享IE8+兼容性问题的解决方法.根据我的实践经验,如果你在写HTML/CSS时候是按照W3C推荐的 ...

  5. CI框架如何在主目录application目录之外使用uploadify上传插件和bootstrap前端框架:

    19:29 2016/3/10CI框架如何在主目录application目录之外使用uploadify上传插件和bootstrap前端框架:项目主路径:F:\wamp\www\graduationPr ...

  6. 最简单的html轮播图制作适合新手

    html代码 --------------------------------------------------------------------------------------------- ...

  7. Java实现验证码制作之一Kaptcha验证码

    Kaptcha验证码 是google提供的验证码插件,使用起来相对简单,设置的干扰线以及字体扭曲不易让其他人读取破解. 这里我们需要 导入一个 kaptcha-2.3.jar  下载地址:http:/ ...

  8. 【noip 2004】 合并果子

    noip2016结束后的第一份代码--优先队列的练习 合并果子 原题在这里 #include <iostream> #include <queue> #include < ...

  9. SQL21日自学通笔记(1)

    查找不重复数据 DISTINCT 查询日期 在access中格式是yyyy-mm-dd,Query执行的语句中用‘#’+Formatdata(‘yyyy-mm-dd’,date)+‘#’ SQL运算符 ...

  10. SSH框架简化

    通过对ssh框架有了基础性的学习,本文主要是使用注解的方式来简化ssh框架的代码编写. 注意事项: 1.运行环境:Windows 8-64位,Eclipse(开发工具),jdk1.8.0_91,Tom ...