千家信息网

web开发中如何使用request method过滤

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章将为大家详细讲解有关web开发中如何使用request method过滤,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。request method过滤:一般
千家信息网最后更新 2026年11月01日web开发中如何使用request method过滤

这篇文章将为大家详细讲解有关web开发中如何使用request method过滤,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

request method过滤:

一般来说,即便是同一个url,因为请求方法不同,处理方式也不同;

如一url,GET方法返回网页内容,POST方法browser提交数据过来需要处理,并存入DB,最终返回给browser存储成功或失败;

即,需请求方法和正则同时匹配才能决定执行什么处理函数;

GET,请求指定的页面信息,并返回header和body;

HEAD,类似GET,只不过返回的响应中只有header没有body;

POST,向指定资源提交数据进行处理请求,如提交表单或上传文件,数据被包含在请求正文中,POST请求可能会导致新的资源的建立和已有资源的修改;

PUT,从c向s传送的数据取代指定的文档内容,MVC框架中或restful开发中常用;

DELETE,请求s删除指定的内容;

注:

s端可只支持GET、POST,其它HEAD、PUT、DELETE可不支持;

ver1:

例:

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): #同if regex.search(request.path)

return handler(request)

raise exc.HTTPNotFound()

@Application.register(Application.GET, '/python$') #若非要写成@Application.register('/python$'),看下例

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:

例:

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:

例:

一个url可设置多个方法:

思路1:

如果什么方法都不写,相当于所有方法都支持;

@Application.route('^/$')相当于@Application.route(None,'^/$')

如果一个处理函数需要关联多个请求方法,这样写:

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

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

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

思路2:

调整参数位置,把请求方法放到最后,变成可变参数:

def route(cls,pattern,*methods):

methods若是一个空元组,表示匹配所有方法;

methods若是非空,表示匹配指定方法;

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

@Application.route('^/$')相当于@Application.route('^/$','GET','PUT','POST','HEAD','DELETE')

例:

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,即所有请求方法

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('^/$') #支持所有请求方法,结合__call__()中not methods

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()

关于"web开发中如何使用request method过滤"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

方法 处理 内容 数据 支持 开发 篇文章 资源 不同 函数 参数 多个 思路 更多 不错 实用 成功 可不 一般来说 位置 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技网 山东特亿宝互联网科技有限公司 没上市的科技互联网公司 平安科技 互联网 西安互联网科技 润盈互联网科技 郑州华宇互联网科技 无锡溥仪互联网科技有限公司 成都玖崟互联网科技有限公司乐商圈怎么样 温州喔噻互联网科技有限公司 互联网科技的全部知识 玉溪新华互联网科技好不好 互联网科技圈年度人物 众安科技工业互联网合作 互联网科技数字货币 工业互联网科技创业 广州燕处互联网科技有限公司 互联网科技平台产品运营 互联网的科技体现在哪里 科技发展第三阶段互联网时代 互联网科技保险 互联网下的科技发展 奇酷互联网科技公司 科技大学互联网专业好不好 互联网公司会议桌创意科技软装饰 老年人脱离互联网科技的作文 互联网时代科技改变生活 企晟互联网信息科技有限公司 佛山市翊博互联网络科技有限公司 2021世界互联网领先科技成果发布
0