Search code examples
pythonpoststreamingtornado

Python requests post stream file and tornado to receive it


I've a file called '' with random numbers inside. Have a code to send post request with this file, but as a stream, not normal POST file:

with open('stream-file') as f:
   requests.post('http://localhost:8888/service', data=f)

Now, there is a tornado server running, which catch this post and should read it (with chunks, not all together):

import tornado.ioloop
import tornado.web
import tornado.options

class ServiceHandler(tornado.web.RequestHandler):
    def post(self):
        # here code to read this streamed file byte by byte.
        pass

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

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

Can someone give me a hint how to work with it? I've found this example: https://gist.github.com/nephics/1134964 but it's not working, i've problem with @tornado.web.stream_body section. I get:

 AttributeError: 'module' object has no attribute 'stream_body'

My config:

requests==1.2.3

tornado==3.1.1


Solution

  • The stream_body gist refers to someone else's fork of tornado, not any official release (you can install the commit mentioned in the gist's comments if you want to try it). There is currently (as of Tornado 3.1) no support for streaming uploads to a Tornado server.