Streaming HTTP Uploads With Python’s Poster

Posted: December 9, 2009 in linux

There’s not much to say beyond the documentation examples, but I was looking for a way to do streaming uploads from Python, and poster delivers nicely on the client side.

Writing a server side streaming downloader in mod_python is infinitely easier and looks something like this:

   def handle_upload(self):
       self.form_data = util.FieldStorage(self.request)
       for key in self.form_data.keys():
           value = self.form_data[key]
           if type(value) == types.InstanceType:
               if isinstance(value, util.StringField):
                   pass
               elif isinstance(value, util.Field):
                   filename = value.filename
                   content_type = value.type
                   base = os.path.basename(value.filename).strip()
                   fd = open("/tmp/apache/%s\n" % base, "wb")
                   while True:
                       data = value.file.read(1024)
                       fd.write(data)
                       if data == "":
                           fd.close()
                       break
       return apache.OK

Security and error handling (as well as writing the rest of the mod_python handler, however minimal that might be) are left as an exercise to the reader.

Advertisement

Leave a Reply

Please log in using one of these methods to post your comment:

Gravatar
WordPress.com Logo

Please log in to WordPress.com to post a comment to your blog.

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s