目錄
MultiDict:...1
webob.Request對象:...2
webob.Response對象:...4
webob.dec裝飾器:...5
webob
environ,環(huán)境數(shù)據(jù)有很多,都存在dict中,字典存取沒有像訪問對象的屬性使用方便;
使用第三方庫webob,可把環(huán)境數(shù)據(jù)的解析、封裝成對象;
https://docs.pylonsproject.org/projects/webob/en/stable/#
>pip install webob
常用:
webob.request
webob.response
webob.dec?? #decorator
webob.exc?? #exception
webob.multidict?? #特殊的字典,key可重復(允許一個key存多個值),因為網(wǎng)頁中的表單框會有重名,如都叫name
特殊的字典,key可重復,允許一個key對應多個value;
往里添加時用add()方法,若用md['a']=2則會覆蓋之前的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'這個key對應的所有value
print(md.get('b'))?? #獲取'b'這個key對應的一個value
print(md.get('c'))
print(md.getone('c'))?? #getone()只能有一個值,即'c'這個key只能對應一個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對象;
GET方法,發(fā)送的數(shù)據(jù)是url中的query string,在request header中;
request.GET就是一個MultiDict字典,里面封裝著查詢字符串;
POST方法,提交的數(shù)據(jù)是放在request body里面,但也可同時使用query string;
request.POST可獲取request body中的數(shù)據(jù),也是個Multidict;
用chrome插件postman模擬POST請求;
用fiddler-->右側(cè)composer模擬POST請求;
不關(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__()方法,實例可調(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:?? #這種方式更貼合實際,進來request,出去response;app()函數(shù)應具有一個參數(shù),是webob.Request類型,是對字典environ對象化后的實例,返回值必須是一個webob.Response類型,app()函數(shù)中應要創(chuàng)建一個webob.Response類型的實例
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
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)站名稱:50web開發(fā)2_webob-創(chuàng)新互聯(lián)
文章來源:http://sd-ha.com/article40/gohho.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App設計、Google、面包屑導航、網(wǎng)站建設、網(wǎng)站排名、虛擬主機
聲明:本網(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)