• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Benjamin

            靜以修身,儉以養(yǎng)德,非澹薄無以明志,非寧靜無以致遠(yuǎn)。
            隨筆 - 397, 文章 - 0, 評(píng)論 - 196, 引用 - 0
            數(shù)據(jù)加載中……

            python Tornado異步客戶端


            AsyncHTTPClient是 tornado.httpclinet 提供的一個(gè)異步http客戶端。使用也比較簡單。與服務(wù)進(jìn)程一樣,AsyncHTTPClient也可以callback和yield兩種使用方式。前者不會(huì)返回結(jié)果,后者則會(huì)返回response。
            示例:
             1 class AsyncHandler(tornado.web.RequestHandler):
             2   @tornado.web.asynchronous
             3   def get(self, *args, **kwargs):
             4  
             5     url = 'https://api.github.com/'
             6     http_client = tornado.httpclient.AsyncHTTPClient()
             7     http_client.fetch(url, self.on_response)
             8     self.finish('It works')
             9  
            10   @tornado.gen.coroutine
            11   def on_response(self, response):
            12     print response.code
            同樣,為了獲取response的結(jié)果,只需要yield函數(shù)。使用yield的時(shí)候,handler是需要 tornado.gen.coroutine。
             1 class AsyncResponseHandler(tornado.web.RequestHandler):
             2   @tornado.web.asynchronous
             3   @tornado.gen.coroutine
             4   def get(self, *args, **kwargs):
             5  
             6     url = 'https://api.github.com/'
             7     http_client = tornado.httpclient.AsyncHTTPClient()
             8     response = yield tornado.gen.Task(http_client.fetch, url)
             9     print response.code
            10     print response.body

            如果業(yè)務(wù)處理并不是在handlers寫的,而是在別的地方,當(dāng)無法直接使用tornado.gen.coroutine的時(shí)候,可以構(gòu)造請(qǐng)求,使用callback的方式。
             1 body = urllib.urlencode(params)
             2 req = tornado.httpclient.HTTPRequest(
             3  url=url, 
             4  method='POST'
             5  body=body, 
             6  validate_cert=False) 
             7  
             8 http_client.fetch(req, self.handler_response)
             9  
            10 def handler_response(self, response):
            11  
            12   print response.code
            AsyncHTTPClient中的fetch方法,第一個(gè)參數(shù)其實(shí)是一個(gè)HTTPRequest實(shí)例對(duì)象,因此對(duì)于一些和http請(qǐng)求有關(guān)的參數(shù),例如method和body,可以使用HTTPRequest先構(gòu)造一個(gè)請(qǐng)求,再扔給fetch方法
             1  def getURL():
             2  2     # 接口地址
             3  3     url = "http://ip:port/Get2"
             4  4 
             5  5 
             6  6     payload = {"key":"914683196200","timestamp":"2019-07-11 11:27:21","partner_id":"1","sign":"C77CAAA496FE0D385410A"}
             7  7     # verify = False 忽略SSH 驗(yàn)證
             8  8 
             9  9       try:
            10             response = requests.post(url, json=payload, timeout=5)  # 超時(shí)的時(shí)候回報(bào)錯(cuò)并重試
            11             print(r.json())
            12         except ReadTimeout:
            13             # 超時(shí)異常
            14             print('Timeout')
            15             return 1
            16         except ConnectionError:
            17             # 連接異常
            18             print('Connection error')
            19             return 2
            20         except RequestException as e:
            21             # 請(qǐng)求異常
            22             print('Error')
            23             print('str(Exception):\t', str(Exception))
            24             print('str(e):\t\t', str(e))
            25             print('repr(e):\t', repr(e))
            注意:使用異步,必須tornado.ioloop.IOLoop.instance().start啟動(dòng) IOLoop 的實(shí)例,啟動(dòng)事件循環(huán)機(jī)制,配合非阻塞的 HTTP Server 工作
            callback_async

            posted on 2019-07-14 15:22 Benjamin 閱讀(463) 評(píng)論(0)  編輯 收藏 引用 所屬分類: python

            久久精品亚洲AV久久久无码| 久久免费看黄a级毛片| 久久福利青草精品资源站免费| 色欲综合久久躁天天躁蜜桃| 久久久无码精品亚洲日韩按摩 | 久久久免费精品re6| 国产99精品久久| 久久久久久毛片免费看| 亚洲精品无码成人片久久| 青青草原综合久久大伊人精品| 欧美大战日韩91综合一区婷婷久久青草 | 天堂久久天堂AV色综合| A级毛片无码久久精品免费| 久久婷婷五月综合色奶水99啪| 国产精品久久国产精品99盘 | 日韩人妻无码一区二区三区久久99| 久久人人爽人人爽人人片AV麻烦| 国产亚洲欧美成人久久片| 久久97精品久久久久久久不卡| 少妇久久久久久被弄到高潮| 996久久国产精品线观看| 波多野结衣久久一区二区| 99国内精品久久久久久久| 久久亚洲国产精品成人AV秋霞| 狠狠色丁香婷婷综合久久来| 久久亚洲精品成人无码网站| 久久久久久极精品久久久| 伊人久久综在合线亚洲2019| 久久人人爽人人爽人人AV东京热| 热RE99久久精品国产66热| 99久久精品国内| 精品精品国产自在久久高清| 久久夜色精品国产噜噜噜亚洲AV| 亚洲欧美日韩久久精品| 人妻中文久久久久| 少妇被又大又粗又爽毛片久久黑人| 久久国产一片免费观看| 精品视频久久久久| 久久久综合香蕉尹人综合网| 人人狠狠综合久久亚洲| 亚洲欧洲中文日韩久久AV乱码|