文章目录

  • 一、Snaic基本功能
    • 1.Routing路由
      • 1.1 传入参数且参数格式规定
      • 1.2 路由的第二种写法
    • 2.Request 请求
    • 3.Response
      • 3.1 文本格式
      • 3.2 HTML
      • 3.3 JSON
      • 3.4 File
        • 案例一:回传图片案例
  • 二、Snaic其他信息
    • 1.app.run参数
      • after_start与before_stop
      • 命令行格式运行
    • 2.报错信息的返回
    • 3.蓝本 Blueprint
      • 延伸一:路由为post,如何写请求?
      • 延伸二:设置sanic 的HTTPS服务
    • 延伸三:压力测试
    • 延伸四:如何回传整张图片
      • 报错:sanic.json 对数字非常严苛
      • 跨域问题 Sanic-CORS
      • py3.5/py3.6版本问题
      • sanic返回HTML
      • 参考文献

Sanic是一个支持 async/await 语法的异步无阻塞框架,Flask的升级版,效率更高,性能会提升不少,我将同一服务分别用Flask和Sanic编写,再将压测的结果进行对比,发现Sanic编写的服务大概是Falsk的1.5倍。
不过Sanic对环境的要求比较苛刻:linux /Mac + python3.5+
window不支持uvloop

先上一个简单案例:

#!/usr/bin/env python
from sanic import Sanic
from sanic.response import textapp = Sanic()@app.route("/")
async def test(request):return text('Hello World!')if __name__ == "__main__":app.run(host="0.0.0.0", port=8000)

来对比一下flask的code:

from flask import Flask
from flask.ext import restfulapp = Flask(__name__)
api = restful.Api(app)class HelloWorld(restful.Resource):def get(self):return {'hello': 'world'}api.add_resource(HelloWorld, '/')if __name__ == '__main__':app.run(debug=True)

从两者对比,可以看到相似性非常高,可以作为flask是完全没有问题的。

一、Snaic基本功能

这里笔者只解读了Snaic的三个方面:Request 、Routing、Response。

1.Routing路由

一个简单的例子:

@app.route('/post7/<param>', methods=['POST','GET'], host='example.com')

'/post7’代表接下来post时候,需要在url后面加上这个后缀:‘http://127.0.0.1:8000/post7’
methods是指request的方式接受那些方式,常见的有post/ get(大写)

1.1 传入参数且参数格式规定

from sanic.response import text@app.route('/tag/<tag>')
async def tag_handler(request, tag):return text('Tag - {}'.format(tag))

这边在URL中会写入一个参数,‘http://127.0.0.1:8000/tag/tag01’,async def tag_handler之中需要写入tag参数
然后该参数即可在函数中任意使用。
相似写法:


@app.route('/number/<integer_arg:int>')
async def integer_handler(request, integer_arg):return text('Integer - {}'.format(integer_arg))@app.route('/number/<number_arg:number>')
async def number_handler(request, number_arg):return text('Number - {}'.format(number_arg))@app.route('/person/<name:[A-z]+>')
async def person_handler(request, name):return text('Person - {}'.format(name))@app.route('/folder/<folder_id:[A-z0-9]{0,4}>')
async def folder_handler(request, folder_id):return text('Folder - {}'.format(folder_id))

1.2 路由的第二种写法

之前看到的路由的写法都是以装饰器的形式出现:

@app.route()
async def function():

其实也可以不用装饰器,简单的写法:

from sanic.response import text# Define the handler functions
async def person_handler2(request, name):return text('Person - {}'.format(name))# Add each handler function as a route
app.add_route(person_handler2, '/person/<name:[A-z]>', methods=['GET'])

通过app.add_route的方式去加路由。
.


2.Request 请求

来看一个比较完整的例子。

# 加载
import aiohttp
import random
from sanic import Sanic
from sanic.exceptions import NotFound
from sanic.response import html, json, redirect
app = Sanic()#定义
@app.route('/matt01')
async def index_json(request):# 用户定义一些传入参数content = request.args.get('titles')content _list = request.args.getlist('titles')# 获取数据return json({'titles':content ,'title_list':content _list,'args1':request.args['titles'],"args2": request.args, "url": request.url, "query_string": request.query_string })
# 启动
if __name__ == "__main__":app.run(host="0.0.0.0", port=8000)

request.args.get---->得到{‘titles’: [‘yes! hello’,‘no!’]}中的第一个’yes! hello’,
request.args.getlist---->得到list所有内容[‘yes! hello’, ‘no!’],
request.args[‘titles’],---->得到[‘yes! hello’,‘no!’],
request.args---->得到{‘titles’: [‘yes! hello’, ‘no!’]},
request.url---->传入URL的所有内容,
request.query_string---->IP+端口+Routing之后的所有内容,

有两种获取结果的写法:
'http://127.0.0.1:8000/matt01’后面可以直接接dict,也可以用?后面接上。

get('http://127.0.0.1:8000/matt01',{'titles': ['yes! hello','no!']}).json()>>> {'args1': ['yes! hello', 'no!'],>>>  'args2': {'titles': ['yes! hello', 'no!']},>>>  'query_string': 'titles=yes%21+hello&titles=no%21',>>>  'title_list': ['yes! hello', 'no!'],>>>  'titles': 'yes! hello',>>>  'url': 'http://127.0.0.1:8000/matt01?titles=yes%21+hello&titles=no%21'}get('http://127.0.0.1:8000/matt01?titles=value1&titles=value2').json()>>> {'args1': ['value1', 'value2'],>>>  'args2': {'titles': ['value1', 'value2']},>>>  'query_string': 'titles=value1&titles=value2',>>>  'title_list': ['value1', 'value2'],>>>  'titles': 'value1',>>>  'url': 'http://127.0.0.1:8000/matt01?titles=value1&titles=value2'}

.


3.Response

在Request 之中,较多的都是以json格式,也可以是很多其他的格式:text、HTML、file、Streaming等。

3.1 文本格式

from sanic import response@app.route('/text')
def handle_request(request):return response.text('Hello world!')

3.2 HTML

from sanic import response@app.route('/html')
def handle_request(request):return response.html('<p>Hello world!</p>')

3.3 JSON

from sanic import response@app.route('/json')
def handle_request(request):return response.json({'message': 'Hello world!'})

3.4 File

from sanic import response@app.route('/file')
async def handle_request(request):return await response.file('/srv/www/whatever.png')

案例一:回传图片案例

回传图片用的是response.file

# 实验8:传回一张图from sanic import response
@app.route('/file8')
async def handle_request(request):return await response.file('/mnt/flask/out3.jpg')
# 实验8 返回图片
get('http://127.0.0.1:8000/file8')
>>> <Response [200]>

返回的是bytes格式的图片。
.


二、Snaic其他信息

1.app.run参数

来源于:Sanic框架

try:serve(host=host,port=port,debug=debug,# 服务开始后启动的函数after_start=after_start,# 在服务关闭前启动的函数before_stop=before_stop,# Sanic(__name__).handle_request()request_handler=self.handle_request,# 默认读取Configrequest_timeout=self.config.REQUEST_TIMEOUT,request_max_size=self.config.REQUEST_MAX_SIZE,)
except:pass
  • host(默认“127.0.0.1”): 服务器主机的地址。
  • port(默认8000): 服务器的端口。
  • debug(默认False): 启用调试(减慢服务器速度)。
  • ssl(默认None): 用于工作者SSL加密的SSLContext。
  • sock(默认None):服务器接受连接的Socket。
  • worker(默认值1):生成的工作进程数。
  • loop(默认None): asyncio兼容的事件循环。如果没有指定,Sanic会创建自己的事件循环。
  • protocol(默认HttpProtocol):asyncio.protocol的子类。
    默认情况下,Sanic在主进程中只侦听一个CPU内核。要启动其它核心,只需指定run参数中进程的数量。
app.run(host='0.0.0.0', port=1337, workers=4)

Sanic将自动启动多个进程并在它们之间建立路由路径。建议进程数和CPU核心数一样。

after_start与before_stop

相当于:

请求 —> before_stop()函数(适合检验权限)—> 执行函数reponse —> after_start()函数(适合检验输出结果)

before_request()函数被app.before_request修饰以后,每一次请求到来后,都会先进入函数before_request()中,如上代码,获取请求的ip以及url,并打印出来,执行完毕后请求才会正常进入到app.route修饰的函数中响应,如果有多个函数被app.before_request修饰了,那么这些函数会被依次执行。
app.before_request修饰器在开发中用处非常大,比如判断某个ip是否有恶意访问行为,从而进行拦截等操作。

@app.before_request
def before_request():ip = request.remote_addrurl = request.urlprint ip,print url
@app.app.before_request
def before_request():#可在此处检查jwt等auth_key是否合法,#abort(401)#然后根据endpoint,检查此api是否有权限,需要自行处理#print(["endpoint",connexion.request.url_rule.endpoint])#abort(401)#也可做ip检查,以阻挡受限制的ip等

因为使用restful方式,因此每次用户访问都会上传带入auth_key,如jwt等,因此可在@app.before_request中做权限的检查。

命令行格式运行

如果你将Sanic应用程序在名为server.py的文件中初始化,那么可以像这样运行服务:

python -m sanic server.app --host=0.0.0.0 --port=1337 --workers=4

2.报错信息的返回

from sanic.response import text
from sanic.exceptions import NotFound@app.exception(NotFound)
def ignore_404s(request, exception):return text("Yep, I totally found the page: {}".format(request.url))

如果出现错误就会显示出以下的内容:

b'Yep, I totally found the page: http://127.0.0.1:8000/users/matt'

3.蓝本 Blueprint

把一些小功能包装在一个小集合Blueprint里面。参考

from sanic import Blueprint
from sanic.response import html, json, redirectapp = Sanic()
blueprint = Blueprint('name', url_prefix='/my_blueprint')
blueprint2 = Blueprint('name2', url_prefix='/my_blueprint2')@blueprint.route('/foo')
async def foo(request):return json({'msg': 'hi from blueprint'})@blueprint2.route('/foo')
async def foo2(request):return json({'msg': 'hi from blueprint2'})app.register_blueprint(blueprint)
app.register_blueprint(blueprint2)app.run(host="0.0.0.0", port=8000, debug=True)

Blueprint(‘name’, url_prefix=’/my_blueprint’)中为,该小蓝本的名字为‘name’(好像没啥用,后面都用不着),前缀为:’/my_blueprint’;
定义蓝本之后,就要定义蓝本内容,@blueprint.route('/foo')还有@blueprint2.route('/foo'),即为相关内容;
app.register_blueprint(blueprint)app.register_blueprint(blueprint2)把蓝本合并到app这个大服务中,然后一起打包给出。

因为每个蓝本有不同的前缀,所以需要在URL之后加入自己的前缀内容以示区分:

get('http://0.0.0.0:8000/my_blueprint2/foo').content
>>> b'{"msg":"hi from blueprint2"}'
get('http://0.0.0.0:8000/my_blueprint/foo').content
>>> b'{"msg":"hi from blueprint"}'

来用小蓝本进行代码简化:

code1.py

from sanic import Blueprint
from sanic.response import html, json, redirectapp = Sanic()
blueprint_v1 = Blueprint('name', url_prefix='/my_blueprint')@blueprint_v1.route('/foo')
async def foo(request):return json({'msg': 'hi from blueprint'})

sanic.py

# !/usr/bin/env python
import sysfrom sanic import Sanicsys.path.append('../')
from code1 import blueprint_v1app = Sanic(__name__)
app.blueprint(blueprint_v1)if __name__ == "__main__":app.run(host="0.0.0.0", port=8000)

蓝本请求:

ip:0000/my_blueprint/foo

参考:
https://github.com/howie6879/Sanic-For-Pythoneer/blob/master/examples/demo04/sample01/src/run.py
https://github.com/howie6879/Sanic-For-Pythoneer/blob/master/examples/demo04/sample01/src/views/rss_html.py


延伸一:路由为post,如何写请求?

对于小白来说,post的方式太难懂了。

@app.route('/post12', methods=['POST'])
async def get_handler(request):return json('POST request - {}'.format(request.body))# 启动
if __name__ == "__main__":app.run(host="0.0.0.0", port=8000)

这边用request.get 或者request.args好像在post都不太好用,只能在get请求上可以使用。这边只有request.body比较使用。
那么Post以及Post之后的结果可见:

post('http://127.0.0.1:8000/post12',{'titles': ['value1', 'value2']}).json()
>>> "POST request - b'titles=value1&titles=value2'"
post('http://127.0.0.1:8000/post12','value1').json()
>>> "POST request - b'value1'"
# 实践不出来的几种方式
post('http://127.0.0.1:8000/post12?titles=value1&titles=value2').json()
>>> 'POST request - None'

第二个再Post之下,比较适合使用的 是request.json
这个比request.body好的一点是,中文就不用做encode(‘utf-8编码’),就是需要变为json格式。

from requests import post
import json
sentence ='一直用'
post_data = {'sentence':sentence,'len' :3}
post('http://xxxxxx:2222/test', json.dumps(post_data)).json()#在后台自然会返回:
request.json
>>> {'right_exclude_len': 3, 'sentence': '一直用的这款,很好,这次价格做的也比较便宜,第一次购买,比想象中要少得多,还有满减活动,很划算啊,好评!', 'left_exclude_len': 3, 'WordSpacing': 100}

如果是request.body

from requests import post
sentence ='一直用'
post('http://xxxxxx:2222/test', sentence .encode('utf-8')).json()

延伸二:设置sanic 的HTTPS服务

网上教程很多,利用 stunnel 建立一个 SSL 加密通道绑定到 Django/Flask 内建服务器,好像不好用。
HTTPS相当于需要设置SSL证书,sanic官方文档写的是:

ssl = {'cert': "/path/to/cert", 'key': "/path/to/keyfile"}
app.run(host="0.0.0.0", port=8443, ssl=ssl)

那么这里可以使用openssl,就需要设置一步:

openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365

然后依次输入:

> openssl req -new -out ca/ca-req.csr -key ca/ca-key.pemCountry Name (2 letter code) [AU]:cn
State or Province Name (full name) [Some-State]:guangdong
Locality Name (eg, city) []:guangzhou
Organization Name (eg, company) [Internet Widgits Pty Ltd]:test
Organizational Unit Name (eg, section) []:test
Common Name (eg, YOUR name) []:root
Email Address []:test

一些个人信息都可以随意填时该目就会生成: key.pem,key.pem

ssl = {'cert': "./cert.pem", 'key': "./key.pem"}
app.run(host="0.0.0.0", port=xxxx,ssl = ssl)

延伸三:压力测试

测试环节有两个部分:单元测试与压力测试。单元测试这边其实可简单可复杂,sanic有自己的测试环节,插件:pytest,这边提一下压力测试。使用的是:locust,压力测试最好在内外网都进行测试下,当然服务器配置是你定。(主要参考:9. 测试与部署)

from requests import post,get
from locust import TaskSet, task
from locust import HttpLocust
import sys
# 端口调用环节
def action_rss(client):data = '心心念念的要来吃花和,好不容易找到个周日的中午有时间,气温刚好不热,组个小团来吃顿棒棒的海鲜,味道超级好。'.encode("utf-8")       # mattroute = '/seg'         # mattheaders = {'content-type': 'application/json'}return client.post(route, data =data,headers=headers).json()# 压力测试
class RssBehavior(TaskSet):@task(1)def interface_rss(self):action_rss(self.client)# locust
class Rss(HttpLocust):host = 'http://0.0.0.0:7777'     # matttask_set = RssBehaviormin_wait = 20max_wait = 3000

执行的时候,在终端输入:locust -f locustfile.py --no-web -c 1,这个结果直接在终端显示,也可以使用:locust -f locustfile.py,可以通过访问IP:http://0.0.0.0:8089/,在web端口看到压力测试的结果。

再来说一下,里面的逻辑,

  • class Rss(HttpLocust):为主函数
    min_wait/max_wait为最小、最大重复测试次数,host为主要请求的端口,此时不能加路由route class

  • RssBehavior(TaskSet)执行函数
    笔者之前没有接触这样的模块,这段代码这段很诡异的是,一开始笔者很难理解,self.client在哪定义了??
    不过,原来在这段函数中,只要执行了Rss,那么隐藏了这么一条信息:self.client = 'http://0.0.0.0:7777'

  • action_rss(client) 比较奇怪的就是: client.pos环节,那么这边隐藏的条件为:还有路由'/seg'和数据data其实都可以及时变更。

client.post(route, data =data,headers=headers).json() = 'http://0.0.0.0:7777'.post('/seg',data = data)

.


延伸四:如何回传整张图片

之前以为把图片压缩成字节型,回传就会没有问题,但是仍然报错:

TypeError: a bytes-like object is required, not 'str'

那么怎么样的才算是合理的?
借鉴百度API的写法,用base64编码。

    import base64# 读入def get_file_content_base64(filePath):with open(filePath,"rb") as f:base64_data = base64.b64encode(f.read())return base64_data.decode('utf-8')
# 保存
bytespic = get_file_content_base64(filePath)with open(name, 'wb') as file:file.write(base64.b64decode(bytespic))

报错:sanic.json 对数字非常严苛

 File "/usr/local/lib/python3.5/dist-packages/sanic/app.py", line 556, in handle_requestresponse = await responseFile "<ipython-input-263-38c8d86e5e18>", line 283, in text_segmentationreturn json(result)File "/usr/local/lib/python3.5/dist-packages/sanic/response.py", line 242, in jsonreturn HTTPResponse(dumps(body, **kwargs), headers=headers,
OverflowError: Maximum recursion level reached

这个报错,基本是因为json体中有一些数字格式有错误,需要调整为:

n = 1
{'version':n}
# 调整为:
{'version':int(n)}

还有遇到另外一种情况也会出现问题:

{[[...], [...]]}

跨域问题 Sanic-CORS

类似flask-cor,GitHub:https://github.com/ashleysommer/sanic-cors
最简单的使用:

from sanic import Sanic
from sanic.response import text
from sanic_cors import CORS, cross_originapp = Sanic(__name__)
CORS(app)@app.route("/", methods=['GET', 'OPTIONS'])
def hello_world(request):return text("Hello, cross-origin-world!")

py3.5/py3.6版本问题

由于Sanic不支持19.6版和更高版本的python 3.5。然而,到2020年12月,18.12LTS版本得到了支持。3.5版的官方python支持将于2020年9月到期。
参考:https://www.osgeo.cn/sanic/index.html

那么,只有19.3.1之前的版本才支持py3.5,譬如:0.6.0.2
目前支持py3.5的有:

Could not find a version that satisfies the requirement sanic==19.3 (from versions: 0.1.0, 0.1.1, 0.1.3, 0.1.4, 0.1.5, 0.1.6, 0.1.7, 0.1.8, 0.1.9, 0.2.0, 0.3.0, 0.3.1, 0.4.0, 0.4.1, 0.5.0, 0.5.1, 0.5.2, 0.5.4, 0.6.0, 0.7.0, 0.8.0, 0.8.1, 0.8.2, 0.8.3, 18.12.0, 19.3.1, 19.6.0, 19.6.2)
No matching distribution found for sanic==19.3

不然这样的代码,在py3.5中是会报错的:

(f"{host}:{port}").encode())

sanic返回HTML

@app.route('/html', methods=['POST','GET', 'OPTIONS'])
async def handle_request(request):htmlf=open('index.html','r',encoding="utf-8")htmlcont=htmlf.read()return html(htmlcont)

后面是可以直接通过:ip:port/html进行访问得到的。


参考文献

Running Your Flask Application Over HTTPS
【Flask】在Flask中使用HTTPS
【网络安全】在局域网里创建个人CA证书
python Flask 使用https 安全协议

1、Sanic教程:快速开始其howie6879/Sanic-For-Pythoneer
2、官网地址
3、howie6879/Sanic-For-Pythoneer技术文档地址
4、Sanic框架

**公众号“素质云笔记”定期更新博客内容:**

python︱微服务Sanic制作一个简易本地restful API相关推荐

  1. 笔记︱利用python + flask制作一个简易本地restful API

    原版官网:http://flask-restful.readthedocs.io/en/latest/ 中文官网:http://www.pythondoc.com/Flask-RESTful/quic ...

  2. python实现api server_使用Python的http.server实现一个简易的Web Api对外提供HanLP拼音转换服务...

    由于采集省市区镇数据需要对地名进行拼音转换,由于第三方高准确度接口对IP进行了限制,处理大量数据变得异常缓慢. 使用了一个折中的办法,省市区 3级(3千+)用高准确度接口(几乎没有拼错的地名),镇级( ...

  3. 使用Python的http.server实现一个简易的Web Api对外提供HanLP拼音转换服务

    由于采集省市区镇数据需要对地名进行拼音转换,由于第三方高准确度接口对IP进行了限制,处理大量数据变得异常缓慢. 使用了一个折中的办法,省市区 3级(3千+)用高准确度接口(几乎没有拼错的地名),镇级( ...

  4. python 拼音地名对应关系,使用Python的http.server实现一个简易的Web Api对外提供HanLP拼音转换服务...

    由于采集省市区镇数据需要对地名进行拼音转换,由于第三方高准确度接口对IP进行了限制,处理大量数据变得异常缓慢. 使用了一个折中的办法,省市区 3级(3千+)用高准确度接口(几乎没有拼错的地名),镇级( ...

  5. 使用python制作一个简易的远控终端

    使用python制作一个简易的远控终端 远控终端的本质 1.服务端(攻击者)传输消息 ----> socket连接 ----> 客户端(被攻击者)接收消息 2.客户端执行消息内容(即执行服 ...

  6. 用Python制作一个简易的计时器

    前言 今天又带来个小玩意 - 用Python制作一个简易的计时器 这个其实也能自定义一些东西的 就比如名字 颜色啥的 自己看着改就行 有想法的朋友也能自己再写写改改出其他的小功能 效果展示 实现代码 ...

  7. 用python写管理系统局域网_详解用python -m http.server搭一个简易的本地局域网

    工作时同事间几mb小文件的传输,一般使用QQ或者微信就足够了,但当传输文件几百MB或者几十G时,这种方法的效率就显得不足了.本篇就是简单说明一个python小功能,让大家能利用python方便的搭建一 ...

  8. python微服务 企业管理_Python写的微服务如何融入Spring Cloud体系?

    前言 在今天的文章中小码哥将会给大家分享一个目前工作中遇到的一个比较有趣的案例,就是如何将Python写的微服务融入到以Java技术栈为主的Spring Cloud微服务体系中?也许有朋友会有疑问,到 ...

  9. 使用Java制作一个简易的远控终端

    使用Java制作一个简易的远控终端 远控终端的本质 1.服务端(攻击者)传输消息 ----> socket连接 ----> 客户端(被攻击者)接收消息 2.客户端执行消息内容(即执行服务端 ...

最新文章

  1. ROS Melodic安装、配置和使用turtlebot2(集成众多源代码直接下载)
  2. java 重载与覆盖_Java重载与覆盖
  3. 自主定义适合自己的Keil主题
  4. xp系统怎么弄清微软服务器名称,xp系统电脑怎么远程云服务器
  5. PHP中文URL编解码(urlencode()rawurlencode()
  6. 深度linux卡顿,Deepin很卡怎么办?Deepin卡顿解决方法盘点
  7. asp.net中异步调用WebService(异步页)[转]
  8. 昆明学院C语言期末考试,昆大c试于题b.doc
  9. 红米K40 Pro的root步骤(MIUI12.5 稳定版 安卓11)
  10. YUV422 转换成 RGB
  11. 收藏的博客 -- Qt有关的GitHub/Gitee开源项目
  12. 用坚果云同步mysql_坚果云安装完成以后, 如何同步文件?
  13. 简简单单说外键和级联
  14. 网络通信详解-深入浅出
  15. 互联网赚钱:普通人月入上万的秘密,连载1
  16. [WARNING]:登录失败:密码错误或账号被冻结
  17. 广东高考成绩及录取分数线揭晓
  18. C# 控制TSC打印机功能
  19. PRIMES is in P
  20. 淘宝API应用调用官方买家信息数据

热门文章

  1. jQuery ajax error函数(交互错误信息的获取)
  2. 完全自定义TabBar(八)
  3. python 类型转换操作
  4. 反编译获取线上任何微信小程序源码(转)
  5. spring集成testng
  6. DOM4j-中文API
  7. 使用seaborn制图(箱型图)
  8. Volley的原理解析
  9. nodejs基础 -- web模块
  10. error C2265: 'Unknown' : reference to a zero-sized array is illegal