python Gunicorn服务器使用方法详解

yipeiwu_com5年前服务器

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 2 -b 0.0.0.0:4000 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:300 -w 4 myapp3:app
[2016-12-18 19:19:51 +0000] [21005] [INFO] Starting gunicorn 19.6.0
[2016-12-18 19:19:51 +0000] [21005] [INFO] Listening at: http://0.0.0.0:300 (21005)
[2016-12-18 19:19:51 +0000] [21005] [INFO] Using worker: sync
[2016-12-18 19:19:51 +0000] [21010] [INFO] Booting worker with pid: 21010
[2016-12-18 19:19:51 +0000] [21011] [INFO] Booting worker with pid: 21011
[2016-12-18 19:19:51 +0000] [21014] [INFO] Booting worker with pid: 21014
[2016-12-18 19:19:51 +0000] [21017] [INFO] Booting worker with pid: 21017

测试

$curl localhost:300/hello.world
hello world!

4. 启动异常

[ERROR] Connection in use: ('127.0.0.1', 8000)

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Nginx搭建HTTPS服务器和强制使用HTTPS访问的方法

Nginx搭建HTTPS服务器和强制使用HTTPS访问的方法

HTTPS简介 HTTPS(Hyper Text Transfer Protocol Secure),是一种基于SSL/TLS的HTTP,所有的HTTP数据都是在SSL/TLS协议封装之...

PHP连接Nginx服务器并解析Nginx日志的方法

php与nginx整合 PHP-FPM也是一个第三方的FastCGI进程管理器,它是作为PHP的一个补丁来开发的,在安装的时候也需要和PHP源码一起编译,也就是说PHP-FPM被编译到P...

Python通过paramiko远程下载Linux服务器上的文件实例

如下所示: #!/usr/local/bin/python # encoding:utf-8 import paramiko import os HOST_IP='59.11...

Python socket网络编程TCP/IP服务器与客户端通信

Python socket网络编程 初学 python,前段时间买了两本书《python 编程从入门到实践》《Python 核心编程第三版》,第一本书主要讲的是一些基本语法和一些基本的使...

使用pycharm在本地开发并实时同步到服务器

使用pycharm在本地开发并实时同步到服务器

本教程使用的是python 3   和django为事例 打开pycharm file  ->   new project...  ->...