tornado asynchttpclient.fetch使用post返回TypeError

时间:2016-05-04 12:56:40

标签: python tornado tornado-motor

我使用Tornado AsynchHTTPClient()。fetch(url,method =" POST",body = body,headers = headers)与coroutine,但抛出错误:

  

TypeError:预期的字节数,unicode或None;得到了类型' int'

    @gen.coroutine
    def do_post(self, url, data):
        self.checksum_builder()
        headers = {
            'AppKey': self.app_key,
            'Nonce': self.nonce,
            'CurTime': self.current_time,
            'CheckSum': self.checksum,
            'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8',
        }
        postdata = data
        http_client = AsyncHTTPClient()
        print urllib.urlencode(postdata)
        response = yield http_client.fetch(url, method='POST', headers=headers, body=urllib.urlencode(postdata))
        print response.error
        raise gen.Return(response)

    @gen.coroutine
    def create_nim_id(self, accid,  name='', props='', icon='', token=''):
        data = dict({
                'accid':accid,
                'name':name,
                'props':props,
                'icon':icon,
                'token':token
            })
        response = yield self.do_post(self.create_nim_id_url, data)
        raise gen.Return(response)




    @gen.coroutine
    def get(self, *args, **kwargs):

        result = yield netease_im_api.NeteaseAPI().create_nim_id(accid='moonmoonbird1',icon='2',props='321',token='adsadsadsadsd', name='hello')
        print(result)
  

[E 160504 20:32:39网址:1524]未捕获的异常GET   / card / 8 / 76d27dfa11cf11e69d86a45e60dcaf9d(127.0.0.1)       HTTPServerRequest(protocol =' http',host =' 127.0.0.1:8000',method =' GET',uri =' / card / 8 / 76d27dfa11cf11e69d86a45e60dcaf9d&#39 ;,   version =' HTTP / 1.1',remote_ip =' 127.0.0.1',headers = {' Host':   ' 127.0.0.1:8000',' Cookie':   ' _xsrf = 2 | 1d0561f1 | 3c717ed824bff16dc8f804b0a7c990ca | 1462268491&#39 ;,   '连接':'关闭'用户代理':' Paw / 2.2.9(Macintosh;操作系统   X / 10.11.2)GCDHTTPRequest'})       Traceback(最近一次调用最后一次):         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/web.py",   第1445行,在_execute中           结果=产量结果         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/gen.py",   1008行,在运行中           value = future.result()         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/concurrent.py",   结果是第232行           raise_exc_info(self._exc_info)         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/gen.py",   第1014行,在运行中           yielded = self.gen.throw(* exc_info)         File" /Users/moonmoonbird/Documents/kuolie/kuolie/account/utils.py" ;, line   70,在包装中           ret = yield ret         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/gen.py",   1008行,在运行中           value = future.result()         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/concurrent.py",   结果是第232行           raise_exc_info(self._exc_info)         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/gen.py",   第1014行,在运行中           yielded = self.gen.throw(* exc_info)         File" /Users/moonmoonbird/Documents/kuolie/kuolie/wall/handlers.py" ;, line   129,在得到           result = yield netease_im_api.NeteaseAPI()。create_nim_id(accid =' moonmoonbird1',icon =' 2',props =' 321',token =' adsadsadsadsd&#39 ;,   名称='你好')         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/gen.py",   1008行,在运行中           value = future.result()         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/concurrent.py",   结果是第232行           raise_exc_info(self._exc_info)         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/gen.py",   第1014行,在运行中           yielded = self.gen.throw(* exc_info)         File" /Users/moonmoonbird/Documents/kuolie/kuolie/neteaseim/netease_im_api.py",   第84行,在create_nim_id中           response = yield self.do_post(self.create_nim_id_url,data)         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/gen.py",   1008行,在运行中           value = future.result()         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/concurrent.py",   结果是第232行           raise_exc_info(self._exc_info)         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/gen.py",   第1014行,在运行中           yielded = self.gen.throw(* exc_info)         File" /Users/moonmoonbird/Documents/kuolie/kuolie/neteaseim/netease_im_api.py",   第60行,在do_post中           response = yield http_client.fetch(url,method =' POST',headers = headers,body = urllib.urlencode(postdata))         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/gen.py",   1008行,在运行中           value = future.result()         File" /Users/moonmoonbird/Documents/kuolie/lib/python2.7/site-packages/tornado/concurrent.py",   结果是第232行           raise_exc_info(self._exc_info)         文件"",第3行,在raise_exc_info中       TypeError:预期字节,unicode或None;得到了类型int

我无法找到我错的地方,有人可以帮助我,提前谢谢。

0 个答案:

没有答案