Created
August 9, 2011 19:33
-
-
Save nephics/1134964 to your computer and use it in GitHub Desktop.
Test of experimental Tornado feature for a streaming request body handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Test of experimental Tornado feature for a streaming request body handler, see | |
# https://github.com/nephics/tornado/commit/1bd964488926aac9ef6b52170d5bec76b36df8a6 | |
# | |
# | |
# Client sending file to server | |
# | |
import tornado.httpclient as httpclient | |
with open('test.pdf', 'r') as f: | |
body = f.read() | |
http_client = httpclient.HTTPClient() | |
request = httpclient.HTTPRequest('http://localhost:8888', 'POST', body=body, | |
headers={'Content-Type': 'application/pdf'}) | |
try: | |
response = http_client.fetch(request) | |
print('File uploaded, server response: {0}'.format(response.body)) | |
except httpclient.HTTPError, e: | |
print "Error:", e | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Test of experimental Tornado feature for a streaming request body handler, see | |
# https://github.com/nephics/tornado/commit/1bd964488926aac9ef6b52170d5bec76b36df8a6 | |
# | |
# | |
# Server with request handler reading of file stream from client | |
# | |
import tornado.ioloop | |
import tornado.web | |
import uuid | |
@tornado.web.stream_body | |
class MainHandler(tornado.web.RequestHandler): | |
def post(self): | |
# open a file or a socket to stream the uploaded data to | |
self.fp = open('file_{0}'.format(uuid.uuid1().hex), 'w') | |
self.request.request_continue() | |
self.read_bytes = 0 | |
self._read_chunk() | |
def _read_chunk(self): | |
# set the chunk size and read bytes from the stream | |
chunk_length = min(10000, | |
self.request.content_length - self.read_bytes) | |
if chunk_length > 0: | |
self.request.connection.stream.read_bytes( | |
chunk_length, self._on_chunk) | |
else: | |
self.fp.close() | |
self._on_uploaded() | |
def _on_chunk(self, chunk): | |
if chunk: | |
# write chunk of data to disk | |
self.fp.write(chunk) | |
self.read_bytes += len(chunk) | |
else: | |
# no more incoming data, set correct content_length | |
self.content_length = self.read_bytes | |
self._read_chunk() | |
def _on_uploaded(self): | |
self.write("Uploaded!") | |
self.finish() | |
if __name__ == "__main__": | |
application = tornado.web.Application([ | |
(r"/", MainHandler), | |
]) | |
application.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello,
Can this example be used to stream the file from form upload to MongoDB GridFS ?
:self-answer:
Yes, it can.