欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python對(duì)Tornado請(qǐng)求與響應(yīng)的數(shù)據(jù)處理

 更新時(shí)間:2020年02月12日 15:30:39   作者:豬笨是念來過倒  
這篇文章主要介紹了Python對(duì)Tornado請(qǐng)求與響應(yīng)的數(shù)據(jù)處理,需要的朋友可以參考下

獲取查詢字符串參數(shù)

get_query_argument(name, default=_ARG_DEFAULT, strip=True)

從請(qǐng)求的查詢字符串中返回指定參數(shù)name的值,如果出現(xiàn)多個(gè)同名參數(shù),則返回最后一個(gè)的值;

default為路由中未傳name參數(shù)時(shí)返回的默認(rèn)值,如default未設(shè)置,則會(huì)拋出tornado.web.MissingArgumentError異常;

strip表示是否過濾掉左右兩邊的空白字符,默認(rèn)為過濾。

get_query_arguments(name, strip=True)

從請(qǐng)求的查詢字符串中返回指定參數(shù)name的值,注意返回的是list列表(即使對(duì)應(yīng)name參數(shù)只有一個(gè)值),若未找到name參數(shù),則返回空列表。

獲取請(qǐng)求體數(shù)據(jù)

get_body_argument(name, default=_ARG_DEFAULT, strip=True)

從請(qǐng)求體中返回指定參數(shù)name的值,如出現(xiàn)多個(gè)同名參數(shù),則返回最后一個(gè)的值;

default與strip同上。

get_body_arguments(name, strip=True)

從請(qǐng)求體中返回指定參數(shù)name的值,注意返回的是list列表(即使對(duì)應(yīng)name參數(shù)只有一個(gè)值),若未找到name參數(shù),則返回空列表。

說明:對(duì)于請(qǐng)求體中數(shù)據(jù)為json或xml的,無法通過這兩個(gè)方法獲取。

前兩類方法的整合

get_argument(name, default=_ARG_DEFAULT, strip=True)

get_arguments(name, strip=True)

說明:對(duì)于請(qǐng)求體中數(shù)據(jù)為json或xml的,無法通過這兩個(gè)方法獲取。

這兩個(gè)方法最常用

獲取請(qǐng)求體中的json或xml數(shù)據(jù)

body_json = self.request.body

body_dict = json.loads(body_json)

需要先判斷請(qǐng)求體的數(shù)據(jù)是否為 application/json 格式:

if self.reuqest.headers.get("Content-Type", "").startswith("application/json"):
try:
  # 防止請(qǐng)求頭內(nèi)容是application/json,實(shí)際內(nèi)容不是json數(shù)據(jù)
  body_dict = json.loads(self.request.body)
except Exception as e:
  logger({"message": e})
    body_dict = dict()

正則提取uri中的參數(shù)

第一種是未命名的方式,按照匹配順序進(jìn)行傳遞:

...
def get(self, subject, city):
 
...
(r"/subject/(.+)/(.+)", SubjectCityHandler),
...

第二種是以命名的方式進(jìn)行傳遞:

...
def get(self, city, date):
 
...
(r"/subject/(?P<date>.+)/(?P<city>.+)", SubjectCityHandler),
...

Tornado的Request對(duì)象屬性

屬性 說明
self.request.method  http的請(qǐng)求頭;get, post等
self.reuqest.uri  客戶端請(qǐng)求完整的uri
self.request.path  uri的路徑名,不包含查詢的字符串
self.request.query uri中的查詢字符串
self.request.version http/1.1
self.request.headers 請(qǐng)求頭
self.requset.body 字符串的消息
self.request.remote_ip 客戶端請(qǐng)求的ip
self.request.protocol http的協(xié)議http or https
self.request.host 請(qǐng)求消息的主機(jī)名
self.request.files 以字典的方式表達(dá)客戶端上傳的文件
self.request.cookies 客戶端的cookies字典
self.request.arguments  客戶端提交的參數(shù)
import tornado.web
import tornado.ioloop
import tornado.httpserver
import tornado.options # 新導(dǎo)入的options模塊
 
tornado.options.define("port", default=8000, type=int, help="服務(wù)器監(jiān)聽端口號(hào)")
tornado.options.define("content", default=[], type=str, multiple=True, help="控制臺(tái)輸出內(nèi)容")
 
class IndexHandler(tornado.web.RequestHandler):
  """主路由處理類"""
  def get(self):
    self.write("Hello World!")
    self.write("\n-------method:\n")
    self.write(self.request.method)
    self.write("\n-------uri:\n")
    self.write(self.request.uri)
    self.write("\n-------path:\n")
    self.write(self.request.path)
    self.write("\n-------query:\n")
    self.write(self.request.query)
    self.write("\n-------version:\n")
    self.write(self.request.version)
    self.write("\n-------headers['Accept-Language']:\n")
    self.write(self.request.headers["Accept-Language"])
    self.write("\n-------body:\n")
    self.write(self.request.body)
    self.write("\n-------remote_ip:\n")
    self.write(self.request.remote_ip)
    self.write("\n-------protocol:\n")
    self.write(self.request.protocol)
    self.write("\n-------host:\n")
    self.write(self.request.host)
    self.write("\n-------arguments:\n")
    self.write(self.request.arguments)
    self.write("\n-------query_arguments:\n")
    self.write(self.request.query_arguments)
    self.write("\n-------body_arguments:\n")
    self.write(self.request.body_arguments)
    self.write("\n-------files:\n")
    self.write(self.request.files)
    self.write("\n-------cookies:\n")
    self.write(self.request.cookies)
    self.write("\n")
 
  #自定義settings
settings = {
  "template_path":"templates",
  "static_path":"statics",
}
 
if __name__ == "__main__":
  tornado.options.parse_config_file("./config")
  print(tornado.options.options.content) # 控制臺(tái)輸出內(nèi)容
 
  app = tornado.web.Application([
    (r"/", IndexHandler),
  ],debug=True,**settings)
 
  http_server = tornado.httpserver.HTTPServer(app)
  http_server.listen(tornado.options.options.port)

更多關(guān)于Python對(duì)Tornado請(qǐng)求與響應(yīng)的數(shù)據(jù)處理方法請(qǐng)查看下面的相關(guān)鏈接

相關(guān)文章

最新評(píng)論