upload file using post to https server

Brian Jones bkjones at gmail.com
Fri Sep 24 16:10:12 CEST 2010


I just did this yesterday with the 'poster' module after fumbling around
with various other ideas, which in the end just produced a lot of code and
didn't get me very far. Poster makes this pretty easy, and if you can't
install it to the system python path, use 'setup.py install --user' and
it'll put it in your home directory under ~/.local.

With poster, it's a breeze. Here's what I've done. Hope it helps:

#!/usr/bin/env python
import random
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2
import threading
import time

class Requestor(threading.Thread):
    def __init__(self, port):
        threading.Thread.__init__(self)
        self.port = port

    def run(self):
        register_openers()
        post_param1 = 'cheddar'
        post_param2 = 'leicester'

        params, headers = multipart_encode({"pic": open("image.jpg"),
"param1": post_param1, "param2": post_param2})

        request = urllib2.Request("http://mybox:%s/" % self.port, params,
headers)

        curtime = time.time()
        response = urllib2.urlopen(request).read()
        perftime = time.time() - curtime

        print "Query time: %s\n" % perftime

if __name__ == "__main__":
    for i in range(2000):
        port = random.choice(range(8885,8905))
        t = Requestor(port)
        t.start()

This is a basic threaded performance testing script that sends an image, and
two other paramters, all in a POST request to a web server that opens
multiple ports. If you don't need the threading, you should be able to rip
the 'run()' method out of the class and just run it by itself (save an edit
or two -- like hard-coding or otherwise replacing self.port).

Good luck!
brian

On Fri, Sep 24, 2010 at 4:23 AM, Dennis Lee Bieber <wlfraed at ix.netcom.com>wrote:

> On Thu, 23 Sep 2010 11:10:55 -0700 (PDT), cerr <ron.eggler at gmail.com>
> declaimed the following in gmane.comp.python.general:
>
>
> >         values = { 'filename' : 'pAce34-7.1.2.3-5189k-efs.bin' }
> >
> >         try:
> >                 data = urllib.urlencode( values )
>
> > But the file doesn't seem to get there correctly. What I wanna do, is
> > mocking the upload from the html site with my python script.... the
> > html looks something like this:
>
>         And are you expecting the server to somehow crawl down the socket
> to
> your machine to find the file, then suck it up?
>
>        Where do you actually attach the file data to the outgoing response?
>
>        Suggest you reference the RFC that added file upload to HTML:
> http://www.faqs.org/rfcs/rfc1867.html
>
>        In particular, section 3.3
> -=-=-=-=-=-
> 3.3 use of multipart/form-data
>
>   The definition of multipart/form-data is included in section 7.  A
>   boundary is selected that does not occur in any of the data. (This
>   selection is sometimes done probabilisticly.) Each field of the form
>   is sent, in the order in which it occurs in the form, as a part of
>   the multipart stream.  Each part identifies the INPUT name within the
>   original HTML form. Each part should be labelled with an appropriate
>   content-type if the media type is known (e.g., inferred from the file
>   extension or operating system typing information) or as
>   application/octet-stream.
> -=-=-=-=-=-
>
>        You've just supplied the file NAME part of the form, but not the
> file data.
> --
>        Wulfraed                 Dennis Lee Bieber         AF6VN
>        wlfraed at ix.netcom.com    HTTP://wlfraed.home.netcom.com/
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Brian K. Jones
My Blog          http://www.protocolostomy.com
Follow me      http://twitter.com/bkjones
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100924/6ecd1a7a/attachment.html>


More information about the Python-list mailing list