Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use request method filtering in web Development

2025-02-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to use request method filtering in web development. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Request method filtering:

Generally speaking, even the same url is handled differently because the request method is different.

For example, if a url,GET method returns web page content, the POST method browser submits the data to be processed, stores it in DB, and finally returns it to browser for storage success or failure

That is, the request method and the rule need to be matched at the same time to decide what processing function to execute.

GET, request the specified page information, and return header and body

HEAD, similar to GET, except that only header has no body in the returned response

POST, submitting data to a specified resource for processing a request, such as submitting a form or uploading a file, the data is included in the body of the request, and the POST request may result in the creation of new resources and modification of existing resources

PUT, data transferred from c to s instead of specified document content, commonly used in MVC frameworks or in restful development

DELETE, request s to delete the specified content

Note:

S side can only support GET and POST, while other HEAD, PUT and DELETE can not support it.

Ver1:

Example:

From wsgiref.simple_server import make_server

From webob import Request, Response, dec, exc

Import re

Class Application:

# ROUTE_TABLE = {}

ROUTE_TABLE = [] # [(method, re.compile (pattern), handler)]

GET = 'GET'

@ classmethod

Def register (cls, method, pattern):

Def wrapper (handler):

Cls.ROUTE_TABLE.append ((method, re.compile (pattern), handler))

Return handler

Return wrapper

@ dec.wsgify

Def _ call__ (self, request:Request)-> Response:

For method, regex, handler in self.ROUTE_TABLE:

If request.method.upper ()! = method:

Continue

If regex.match (request.path): # same as if regex.search (request.path)

Return handler (request)

Raise exc.HTTPNotFound ()

@ Application.register (Application.GET,'/ python$') # if you don't want to write @ Application.register ('/ python$'), see the following example

Def showpython (request):

Res = Response ()

Res.body = 'hello python'.encode ()

Return res

@ Application.register ('post',' ^ / $')

Def index (request):

Res = Response ()

Res.body = 'welcome'.encode ()

Return res

If _ _ name__ = ='_ _ main__':

Ip = '127.0.0.1'

Port = 9999

Server = make_server (ip, port, Application ())

Try:

Server.serve_forever ()

Except Exception as e:

Print (e)

Finally:

Server.shutdown ()

Server.server_close ()

VER2:

Example:

From wsgiref.simple_server import make_server

From webob import Request, Response, dec, exc

Import re

Class Application:

# ROUTE_TABLE = {}

ROUTE_TABLE = [] # [(method, re.compile (pattern), handler)]

GET = 'GET'

@ classmethod

Def route (cls, method, pattern):

Def wrapper (handler):

Cls.ROUTE_TABLE.append ((method, re.compile (pattern), handler))

Return handler

Return wrapper

@ classmethod

Def get (cls, pattern):

Return cls.route ('GET', pattern)

@ classmethod

Def post (cls, pattern):

Return cls.route ('POST', pattern)

@ classmethod

Def head (cls, pattern):

Return cls.route ('HEAD', pattern)

@ dec.wsgify

Def _ call__ (self, request:Request)-> Response:

For method, regex, handler in self.ROUTE_TABLE:

Print (method, regex, handler)

If request.method.upper ()! = method:

Continue

Matcher = regex.search (request.path)

Print (matcher)

If matcher: # if regex.search (request.path)

Return handler (request)

Raise exc.HTTPNotFound ()

@ Application.get ('/ python$')

Def showpython (request):

Res = Response ()

Res.body = 'hello python'.encode ()

Return res

@ Application.post ('^ / $')

Def index (request):

Res = Response ()

Res.body = 'welcome'.encode ()

Return res

If _ _ name__ = ='_ _ main__':

Ip = '127.0.0.1'

Port = 9999

Server = make_server (ip, port, Application ())

Try:

Server.serve_forever ()

Except Exception as e:

Print (e)

Finally:

Server.shutdown ()

Server.server_close ()

VER3:

Example:

A url can set multiple methods:

Idea 1:

If no method is written, it means that all methods support it.

@ Application.route ('^ / $') is equivalent to @ Application.route (None,' ^ / $')

If a handler needs to associate multiple request methods, write:

@ Application.route (['GET','PUT','DELETE'],' ^ / $')

@ Application.route (('GET','PUT','POST'),' ^ / $')

@ Application.route ({'GET','PUT','DELETE'},' ^ / $')

Idea 2:

Adjust the parameter position, put the request method at the end, and change it to a variable parameter:

Def route (cls,pattern,*methods):

If methods is an empty tuple, it matches all methods.

If methods is not empty, it matches the specified method.

@ Application.route ('^ / $', 'POST','PUT','DELETE')

@ Application.route ('^ / $') is equivalent to @ Application.route ('^ / $', 'GET','PUT','POST','HEAD','DELETE')

Example:

From wsgiref.simple_server import make_server

From webob import Request, Response, dec, exc

Import re

Class Application:

# ROUTE_TABLE = {}

ROUTE_TABLE = [] # [(method, re.compile (pattern), handler)]

GET = 'GET'

@ classmethod

Def route (cls, pattern, * methods):

Def wrapper (handler):

Cls.ROUTE_TABLE.append ((methods, re.compile (pattern), handler))

Return handler

Return wrapper

@ classmethod

Def get (cls, pattern):

Return cls.route (pattern, 'GET')

@ classmethod

Def post (cls, pattern):

Return cls.route (pattern, 'POST')

@ classmethod

Def head (cls, pattern):

Return cls.route (pattern, 'HEAD')

@ dec.wsgify

Def _ call__ (self, request:Request)-> Response:

For methods, regex, handler in self.ROUTE_TABLE:

Print (methods, regex, handler)

If not methods or request.method.upper () in methods: # not methods, that is, all request methods

Matcher = regex.search (request.path)

Print (matcher)

If matcher:

Return handler (request)

Raise exc.HTTPNotFound ()

@ Application.get ('/ python$')

Def showpython (request):

Res = Response ()

Res.body = 'hello python'.encode ()

Return res

@ Application.route ('^ / $') # supports all request methods, combined with not methods in _ _ call__ ()

Def index (request):

Res = Response ()

Res.body = 'welcome'.encode ()

Return res

If _ _ name__ = ='_ _ main__':

Ip = '127.0.0.1'

Port = 9999

Server = make_server (ip, port, Application ())

Try:

Server.serve_forever ()

Except Exception as e:

Print (e)

Finally:

Server.shutdown ()

Server.server_close ()

This is the end of this article on "how to use request method filtering in web development". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report