Total Pageviews

Thursday, 14 May 2020

The lightning-fast ASGI server

🦄 
Build Status Package version
Documentation: https://www.uvicorn.org
Requirements: Python 3.6+ (For Python 3.5 support, install version 0.8.6.)
Uvicorn is a lightning-fast ASGI server implementation, using uvloop and httptools.
Until recently Python has lacked a minimal low-level server/application interface for asyncio frameworks. The ASGI specification fills this gap, and means we're now able to start building a common set of tooling usable across all asyncio frameworks.
Uvicorn currently supports HTTP/1.1 and WebSockets. Support for HTTP/2 is planned.

Quickstart

Install using pip:
$ pip install uvicorn
Create an application, in example.py:
async def app(scope, receive, send):
    assert scope['type'] == 'http'

    await send({
        'type': 'http.response.start',
        'status': 200,
        'headers': [
            [b'content-type', b'text/plain'],
        ],
    })
    await send({
        'type': 'http.response.body',
        'body': b'Hello, world!',
    })
Run the server:
$ uvicorn example:app
from  https://github.com/encode/uvicorn
---------------------------------------------------

Apache下,https的反向代理程序Uvicorn

Uvicorn是一个Python Web服务器和开发服务器,支持多种框架,如Flask、Django以及FastAPI,Uvicorn以其高性能和易用性收到开发者的欢迎。

一、安装Uvicorn

为本机python安装venv

# apt install python-venv

为了方便与root进行隔离,新创建一般用户mysiteuser

# useradd -m mysiteuser
# passwd mysite

使用mysiteuser登陆ssh

ssh mysiteuser@123.123.123.123

创建python虚拟环境

$ python -m venv ./venv

在虚拟环境中安装uvicorn

$./venv/bin/pip install uvicorn

uvicorn配置,以下是个范例 mymodule.py,用于运行flask程序

#!/bin/python

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
        return 'Hello, World!'

if __name__ == '__main__':
        uvicorn.run(app,host='0.0.0.0',port=3000)

使用以下命令启动服务器:

./venv/bin/uvicorn mymodule:app --reload

其中mymodule是包含Flask应用的Python模块名称

二、安装Apache

Apache的配置文件位于/etc/apache2/sites-available/

创建文件mysite.conf

<VirtualHost *:80>
          
</VirtualHost>

保存配置,并启用配置

a2ensite mysite.conf

重新启动Apache以应用更改

systemctl restart apache2

三、集成

安装apache2的对wsgi的支持

apt-get install libapache2-mod-wsgi-py3

配置Apache,编辑mysite.conf

<VirtualHost *:80>
          SSLEngine On
          SSLProxyEngine On
          SSLProxyVerify none
          SSLProxyCheckPeerCN off
          SSLProxyCheckPeerName off
          SSLCertificateFile "opt/ssl/server.crt"
          SSLCertificateKeyFile “opt/ssl/server.key”

          ProxyRequests Off
          ProxyPreserveHost On

          ProxyPass / http://127.0.0.1:3000/
          ProxyPassReverse / http://127.0.0.1:3000/
</VirtualHost>

安装 CertBot

./venv/bin/pip install certbot

申请证书

./venv/bin/certbot certonly -d mysite.conf --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory --config-dir /opt/ssl --work-dir /opt/ssl --logs-dir /opt/ssl

配置crontab

crontab -e
0 0 0 * * * ./venv/bin/certbot certonly -d mysite.conf --manual --preferred-challenges dns --server https://acme-v02.api.letsencrypt.org/directory --config-dir /opt/ssl --work-dir /opt/ssl --logs-dir /opt/ssl

No comments:

Post a Comment