从Tornado进行投入/发布工作

时间:2015-06-02 19:34:59

标签: http tornado dropbox-api

我正在尝试向DropBox api创建一个PUT方法(或POST),但它确实有效,我得到一个GET

import tornado.ioloop
import tornado.web
from tornado.httputil import HTTPHeaders
from tornado.httpclient import HTTPClient, HTTPRequest

url = "https://api-content.dropbox.com/1/files_put/sandbox/world.txt"

class MainHandler(tornado.web.RequestHandler):
    def post(self):
        headers = HTTPHeaders({'Authorization': 'Bearer TOKEN_FOR_DROPBOX'})
        HTTPClient().fetch(
        HTTPRequest(url, 'PUT', body="hello there", headers=headers))

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
tornado.ioloop.IOLoop.current().start()

更新:使用GET会出错:HTTPError: HTTP 400: Bad Request

这是一个新代码:

import tornado.ioloop
import tornado.web
from tornado.httpclient import HTTPClient, HTTPRequest

url = "https://api-content.dropbox.com/1/files_put/sandbox/wor.txt"

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.set_header('Authorization', 'Bearer DROPBOX_TOKEN')
        self.set_header('Content-Type', 'text/plain')        
        HTTPClient().fetch(
        HTTPRequest(url, 'PUT', body="hello there"))

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.current().start()

但得到此错误:

Traceback (most recent call last): File "C:\Python27\lib\site-packages\tornado\web.py", line 1415, in _execute result = yield result File "C:\Python27\lib\site-packages\tornado\gen.py", line 870, in run value = future.result() File "C:\Python27\lib\site-packages\tornado\concurrent.py", line 215, in result raise_exc_info(self._exc_info) File "C:\Python27\lib\site-packages\tornado\gen.py", line 230, in wrapper yielded = next(result) File "C:\Users\Abdelouahab\Desktop\ttttt.py", line 14, in get HTTPRequest(url, 'PUT', body="hello there")) File "C:\Python27\lib\site-packages\tornado\httpclient.py", line 102, in fetch self._async_client.fetch, request, **kwargs)) File "C:\Python27\lib\site-packages\tornado\ioloop.py", line 445, in run_sync return future_cell[0].result() File "C:\Python27\lib\site-packages\tornado\concurrent.py", line 215, in result raise_exc_info(self._exc_info) File "<string>", line 3, in raise_exc_info HTTPError: HTTP 401: Unauthorized ERROR:tornado.access:500 GET / (::1) 806.00ms

我尝试使用Mozilla的HTTP请求构建器扩展,并且它有效,所以我想问题是如何在Tornado上做到这一点?

1 个答案:

答案 0 :(得分:0)

抱歉,它似乎错过了content-type

import tornado.ioloop
import tornado.web
from tornado.httputil import HTTPHeaders
from tornado.httpclient import HTTPClient, HTTPRequest
from tornado.gen import coroutine

url = "https://api-content.dropbox.com/1/files_put/sandbox/wor.txt"

class MainHandler(tornado.web.RequestHandler):
    @coroutine    
    def get(self):
        headers = HTTPHeaders({'Authorization': 'Bearer DROPBOX_TOKEN', 'Content-Type':'text/plain'})
        HTTPClient().fetch(
        HTTPRequest(url, 'PUT', body="hello there", headers=headers))


application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.current().start()