您好,登錄后才能下訂單哦!
?
?
目錄
MultiDict:... 1
webob.Request對(duì)象:... 2
webob.Response對(duì)象:... 4
webob.dec裝飾器:... 5
?
?
?
webob
environ,環(huán)境數(shù)據(jù)有很多,都存在dict中,字典存取沒有像訪問對(duì)象的屬性使用方便;
使用第三方庫(kù)webob,可把環(huán)境數(shù)據(jù)的解析、封裝成對(duì)象;
?
https://docs.pylonsproject.org/projects/webob/en/stable/#
?
>pip install webob
?
常用:
webob.request
webob.response
webob.dec?? #decorator
webob.exc?? #exception
webob.multidict?? #特殊的字典,key可重復(fù)(允許一個(gè)key存多個(gè)值),因?yàn)榫W(wǎng)頁中的表單框會(huì)有重名,如都叫name
?
特殊的字典,key可重復(fù),允許一個(gè)key對(duì)應(yīng)多個(gè)value;
往里添加時(shí)用add()方法,若用md['a']=2則會(huì)覆蓋之前的value;
例:
from webob.multidict import MultiDict
?
md = MultiDict()
md.add(1, 'magedu')
md.add(1, '.com')
md.add('a', 1)
md.add('a', 2)
md['b'] = '3'?? #若此句放到md.add('b', 4)之后則是覆蓋
md.add('b', 4)
?
for pair in md.items():
??? print(pair)
?
print(md.getall('a'))?? #獲取'a'這個(gè)key對(duì)應(yīng)的所有value
print(md.get('b'))?? #獲取'b'這個(gè)key對(duì)應(yīng)的一個(gè)value
print(md.get('c'))
print(md.getone('c'))?? #getone()只能有一個(gè)值,即'c'這個(gè)key只能對(duì)應(yīng)一個(gè)value
輸出:
(1, 'magedu')
(1, '.com')
('a', 1)
('a', 2)
('b', '3')
('b', 4)
[1, 2]
4
None
……
KeyError: "Multiple values match 'b': ['3', 4]"
?
?
?
將environ環(huán)境參數(shù)解析并封裝成request對(duì)象;
?
GET方法,發(fā)送的數(shù)據(jù)是url中的query string,在request header中;
request.GET就是一個(gè)MultiDict字典,里面封裝著查詢字符串;
?
POST方法,提交的數(shù)據(jù)是放在request body里面,但也可同時(shí)使用query string;
request.POST可獲取request body中的數(shù)據(jù),也是個(gè)Multidict;
用chrome插件postman模擬POST請(qǐng)求;
用fiddler-->右側(cè)composer模擬POST請(qǐng)求;
?
不關(guān)心什么方法提交,只關(guān)心數(shù)據(jù),可用request.params,它里面是所有提交數(shù)據(jù)的封裝;
?
注:
url中的query string,歸request.GET管;
body中的表單,歸request.POST管;
request.params,查詢字符串和body中的提交表單都管;
?
例,webob.Request:
from webob import Request, Response
?
def application(environ, start_response):
??? request = Request(environ)
??? print(request.method)
??? print(request.path)
??? print(request.GET)
??? print(request.POST)?? #MultiDict()
??? print(request.params)?? #NetstedMultiDict()
??? print(request.query_string)
???
??? html = '<h2>test</h2>'.encode()
??? start_response("200 OK", [('Content-Type', 'text/html; charset=utf-8')])
??? return [html]
?
ip = '127.0.0.1'
port = 9999
server = make_server(ip, port, application)
server.serve_forever()
server.shutdown()
server.server_close()
輸出:
GET
/index.html
GET([('id', '5'), ('name', 'jowin'), ('name', 'tom'), ('age', ''), ('age', '18,19')])
<NoVars: Not an HTML form submission (Content-Type: text/plain)>
NestedMultiDict([('id', '5'), ('name', 'jowin'), ('name', 'tom'), ('age', ''), ('age', '18,19')])
id=5&name=jowin&name=tom&age=&age=18,19
?
用fiddler模擬POST:
User-Agent: Fiddler
Content-Type: application/x-www-form-urlencoded
Host: 127.0.0.1:9999
輸出:
POST
/index.php
GET([])
MultiDict([('index.html', ''), ('id', '5'), ('name', 'jowin'), ('name', 'chai'), ('age', ''), ('age', '18,19')])
NestedMultiDict([('index.html', ''), ('id', '5'), ('name', 'jowin'), ('name', 'chai'), ('age', ''), ('age', '18,19')])
?
?
?
查看源碼:
class Response(object):
??? def __init__(self, body=None, status=None, headerlist=None, app_iter=None,
???????????????? content_type=None, conditional_response=None, charset=_marker,
???????????????? **kw):
??? def __call__(self, environ, start_response):
??????? """
??????? WSGI application interface
??????? """
??????? start_response(self.status, headerlist)
??????? return self._app_iter?? #self._app_iter = app_iter,app_iter = [body]
?
例:
def application(environ, start_response):
??? request = Request(environ)
??? print(request.params)
?
??? res = Response()
??? print(res.status)
??? print(res.headerlist)
??? print(res.content_type)
??? print(res.charset)
??? print(res.status_code)
??? res.status_code = 200
??? html = '<h2>test</h2>'.encode()
??? res.body = html
??? start_response(res.status, res.headerlist)
??? return [html]
輸出:
NestedMultiDict([('id', '5'), ('name', 'jowin'), ('name', 'tom'), ('age', ''), ('age', '18,19')])
200 OK
[('Content-Type', 'text/html; charset=UTF-8'), ('Content-Length', '0')]
text/html
UTF-8
200
?
例:
def application(environ, start_response):
??? request = Request(environ)
??? print(request.params)
?
??? res = Response()
??? print(res.status)
??? print(res.headerlist)
??? print(res.content_type)
??? print(res.charset)
??? print(res.status_code)
??? res.status_code = 200
??? html = '<h2>test</h2>'.encode()
??? res.body = html
??? return res(environ, start_response)?? #Response類中有__call__()方法,實(shí)例可調(diào)用
?
?
?
class wsgify(object):
??? RequestClass = Request
??? def __init__(self, func=None, RequestClass=None,
???????????????? args=(), kwargs=None, middleware_wraps=None):
?
例:
from webob import Request, Response, dec
from wsgiref.simple_server import make_server
?
@dec.wsgify
def app(request:Request)->Response:?? #這種方式更貼合實(shí)際,進(jìn)來request,出去response;app()函數(shù)應(yīng)具有一個(gè)參數(shù),是webob.Request類型,是對(duì)字典environ對(duì)象化后的實(shí)例,返回值必須是一個(gè)webob.Response類型,app()函數(shù)中應(yīng)要?jiǎng)?chuàng)建一個(gè)webob.Response類型的實(shí)例
??? print(request.method)
??? print(request.path)
??? print(request.query_string)
??? return Response('<h2>test</h2>'.encode())
?
if __name__ == '__main__':
??? ip = '127.0.0.1'
??? port = 9999
??? server = make_server(ip, port, app)
??? try:
??????? server.serve_forever()
??? except KeyboardInterrupt:
??????? pass
??? finally:
??????? server.shutdown()
??????? server.server_close()
輸出:
GET
/index.html
id=5&name=jowin&name=tom&age=&age=18,19
?
?
?
?
?
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。