Hi-nginx-based Web development (python)-Dynamic Routing and Request methods

Source: Internet
Author: User

The provided route adorner for hi.py accepts two parameters, the first parameter specifies the regular mode of dynamic routing, and the second parameter specifies a list of agreed HTTP request methods.

Like what:

 1  @app. Route (R " ^/client/?$   ", ["  get  , "  post   " ])  2  def       client (Req,res,param):  3  Res.content ( '  {}<br>{}<br>{}<br >{}<br>{}   " .format (Req.client ( ), Req.method (), Req.uri (), Req.user_agent (), Req.param ()))  4  res.status ( ) 

This route specifies that the URI is/client or/client/, while the request method is a GET or post HTTP request handled by the function client (Req,res,param). The URI pattern consists of a regular expression, the request method parameter is represented by a list, the acceptable method name includes: Get,post,put,head,patch, and so on, where nginx can understand the HTTP method name can be.

The processing function has three parameters, namely Req,res and Param. They are hi_req,hi_res and group dict that are derived from regular expressions. The first two are APIs provided by Hi-nginx that provide a range of ways to manipulate the HTTP protocol:

Hi_req
    • Uri
    • Method
    • Client
    • Param
    • User_agent
    • Has_header
    • Get_header
    • Has_form
    • Get_form
    • Has_session
    • Get_session
    • Has_cookie
    • Get_cookie
Hi_res
    • Status
    • Content
    • Header
    • Session

The third parameter of the handler function param can be used to parse the data provided by the regular pattern, such as:

 1  @app. Route (R " ^/hello/(? p<who>\w+)? $  , ["  get   " ])  Span style= "COLOR: #008080" >2  def   Hello ( Req,res,param):  3  res.content ( " Span style= "COLOR: #800000" >{}={}   ". Format (  " who   ", Param["   Who   "   4  res.status ($) 

Regular mode ^/hello/(? p<who>\w+) $ is a pattern that the Python re module can understand, and when the URI is/hello/cnblogs, the parameter param is a dict dictionary containing the WHO key, with Cnblogs as its value. As a result, it can be used to provide data outside the form for the operation function and to beautify the URI. For example/hello?who=cnblogs is inferior to/hello/cnblogs beautiful concise.

In addition, the same operation function can correspond to multiple routing rules, such as:

1@app. Route (R'^/test/?$',['POST'])2@app. Route (R"^/$",['GET'])3 defHello_world (req,res,param):4Res.header ('Content-type','Text/plain;charset=utf-8')5Res.content ('Hello,world')6Res.status (200)

In the above code, the Operation function Hello_world can be called when the URI is/or/test or/test/, provided that one request method is get and the other is the Post method. This function, which corresponds to multiple routes, eliminates the hassle of writing the following code:

1 if req.method () = ='GET':2     ... 3 if req.method () = ='POST':4...     

Hi-nginx-based Web development (python)-Dynamic Routing and Request methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.