這篇文章將為大家詳細講解有關(guān)web開發(fā)中如何使用request method過濾,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)公司主要從事成都做網(wǎng)站、網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)噶爾,10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575一般來說,即便是同一個url,因為請求方法不同,處理方式也不同;
如一url,GET方法返回網(wǎng)頁內(nèi)容,POST方法browser提交數(shù)據(jù)過來需要處理,并存入DB,最終返回給browser存儲成功或失??;
即,需請求方法和正則同時匹配才能決定執(zhí)行什么處理函數(shù);
GET,請求指定的頁面信息,并返回header和body;
HEAD,類似GET,只不過返回的響應(yīng)中只有header沒有body;
POST,向指定資源提交數(shù)據(jù)進行處理請求,如提交表單或上傳文件,數(shù)據(jù)被包含在請求正文中,POST請求可能會導(dǎo)致新的資源的建立和已有資源的修改;
PUT,從c向s傳送的數(shù)據(jù)取代指定的文檔內(nèi)容,MVC框架中或restful開發(fā)中常用;
DELETE,請求s刪除指定的內(nèi)容;
注:
s端可只支持GET、POST,其它HEAD、PUT、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 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 = '<h2>hello python</h2>'.encode()
return res
@Application.register('post', '^/$')
def index(request):
res = Response()
res.body = '<h2>welcome</h2>'.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()
例:
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 = '<h2>hello python</h2>'.encode()
return res
@Application.post('^/$')
def index(request):
res = Response()
res.body = '<h2>welcome</h2>'.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()
例:
一個url可設(shè)置多個方法:
思路1:
如果什么方法都不寫,相當(dāng)于所有方法都支持;
@Application.route('^/$')相當(dāng)于@Application.route(None,'^/$')
如果一個處理函數(shù)需要關(guān)聯(lián)多個請求方法,這樣寫:
@Application.route(['GET','PUT','DELETE'],'^/$')
@Application.route(('GET','PUT','POST'),'^/$')
@Application.route({'GET','PUT','DELETE'},'^/$')
思路2:
調(diào)整參數(shù)位置,把請求方法放到最后,變成可變參數(shù):
def route(cls,pattern,*methods):
methods若是一個空元組,表示匹配所有方法;
methods若是非空,表示匹配指定方法;
@Application.route('^/$','POST','PUT','DELETE')
@Application.route('^/$')相當(dāng)于@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 = '<h2>hello python</h2>'.encode()
return res
@Application.route('^/$') #支持所有請求方法,結(jié)合__call__()中not methods
def index(request):
res = Response()
res.body = '<h2>welcome</h2>'.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()
關(guān)于“web開發(fā)中如何使用request method過濾”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學(xué)到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。
新聞名稱:web開發(fā)中如何使用requestmethod過濾-創(chuàng)新互聯(lián)
網(wǎng)站地址:http://www.rwnh.cn/article12/dhhdgc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、標簽優(yōu)化、域名注冊、ChatGPT、App設(shè)計、服務(wù)器托管
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)