This article example describes the Sanic framework request and response. Shared for your reference, as follows:
Previously introducedRouting in the Sanic framework, here follows a description of the request and response of the Sanic framework.
synopsis
Sanic is a Flask-like Python 3.5+ web server that writes very fast. In addition to Flask, Sanic supports asynchronous request handlers. This means you can use the shiny new async/wait syntax in Python 3.5 to make your code non-blocking and fast.
preamble:Sanic supports Python 3.5 as a minimum.If you need to learn Sanic, please download it first.Python packages with version no less than 3.5
Request data
When an endpoint receives an HTTP request, the routing function is passed to a request object. The following variables can be accessed as properties of the request object:
- json: JSON data
@("/post_data",methods=["POST"]) async def post_data(request): # will print the passed JSON data print() return text("it is ok!")
-
args: Query string variables. A query string is a part of a URL-like
?name=laozhang&age=20
. If the URL is parsed, then the args dictionary will look like this:{"name":["laozhang"],"age":[20]}
-
raw_args: In many cases, we need to get the url parameters in dictionaries with low compression. For the previous
URL?name=laozhang&age=20
,raw_args
The dictionary will be as follows:{"name":"laozhang","age":20}
- file: Dictionary of file objects, list of files with name, body and type
@("/post_file_data",methods=["POST"]) async def post_file_data(request): info = ("file") print() print() print() return text("it is ok!")
-
form: The form data, the form dictionary will look as follows:
{"name":["laozhang"]}
@("/post_form_data",methods=["POST"]) async def post_form_data(request): name = ("name") return text("it is ok!")
-
body: raw data. Regardless of the content type, this attribute allows the retrieval of the raw data of the request.
byte
typology -
headers: Get a case-insensitive dictionary of request table headers.
dict
typology -
ip: IP address.
str
typology -
port: Ports.
str
typology -
socket: IP address and port of the requestor, (IP address, port).
tuple
typology - app: A reference to the Sanic application object that handles the request.
@("/get_app_info") async def get_app_info(request): print() return text("it is ok!")
- url: The full URL of the request, e.g:http://localhost:5000/get_app_info
-
scheme: Get the URL scheme associated with the request:
http
maybehttps
- host: Get the host associated with the request
-
path: Get the path of the request, e.g:
/get_app_info
-
query_string: Get the query string, e.g:
name=zhangsan
or as a blank string -
uri_template: Get the template for the matching route handler, for example:
/get/<id>
- token: Value of the Authorization header
get vs. getlist
When we access aGETrequest as follows when passing in the relevant parameters:
@("/get_info") async def get_info(request): print(("name")) print(("name") return text("it is ok!")
When we pass in aname
because oflaozhang
When, as mentioned above, the args dictionary will be{"name":["laozhang"]
, so accessing the above route will print the following:
laozhang
["laozhang"]
responsive
utilizationfunction in the module to create the response
Plain text:
from import text @("/text") async def get_text(request): return text("it is text response!")
HTML:
from import html @("/html") async def get_html(request): return html("<p>it is html!</p>")
JSON:
from import json @("/json") async def get_json(request): return json({"name":"laozhang"})
FILE:
from import file @("/file") async def get_file(request): return await file("/xx/aa/")
Remember, nothing less.await
Keywords.
STREAM:
from import stream @("/stream") async def get_stream(request): async def stream_fn(response): ("abc") ("def") return stream(stream_fn,content_type="text/plain")
File Streaming:Combination of top files and streams for large files
from import file_stream @("/file_stream") async def get_file_stream(request): return await file_stream("/xx/aa/")
Remember, nothing less.await
Keywords.
Redirection:
from import redirect @("/redirect") async def get_redirect(request): return redirect("/json")
RAW: Unencoded body response
from import raw @("/raw") async def get_raw(request): return raw(b"it is raw data")
Accessing this interface will immediately download a file namedraw
file, which contains the contentsit is raw data
Modify the request header and status values:If you need to modify the request header and status values, set theheaders
cap (a poem)status
parameter passed to these functions above, and the following is a list of the functions with thejson
e.g.
from import json @("/json") async def get_json(request): return json({"name":"Old Chang."},headers={"age":18},status=403)
After accessing this interface, you will find that the status value that should have been 200 has changed to 403, and the request header information has been increased to{"age":18}
More about Python related content can be viewed on this site's topic: thePython introductory and advanced classic tutorials》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand theSummary of Python file and directory manipulation techniques》
I hope that what I have said in this article will help you in Python programming.