I've been unsucessfully trying to upload a file using POST to an https
I've succesfully logged in and gotten to read something from the
I have come up with something like this:

authinfo = urllib2.HTTPBasicAuthHandler()

authinfo.add_password(realm='Configuration Software',

opener = urllib2.build_opener(authinfo)
pagePtr = opener.open(url)

dataRead = str(pagePtr.read())

#Look for 'Software Upload' in the page
if( "Software Upload" in dataRead ):
print "FOUND Software Upload in string...<BR>"
print "Software Upload page not found. Exiting..."

values = { 'filename' : 'pAce34-' }

data = urllib.urlencode( values )
req = urllib2.Request( url, data )
#response = urllib2.urlopen( req )
response = opener.open( req )

the_page = response.read()
#print the_page

except Exception,detail:
print "err ",detail

#Look for 'file must be efs' in the page
if( "file must be efs" in the_page ):
print "file must be efs. Exiting..."
print "OK"

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:

<form method="post" action="/cgi-bin/config.pl" enctype="multipart/
form-data" onsubmit="return validateForm()" name="advance">
<input type="file" name="filename" tabindex="1" size="57" /
&nbsp;&nbsp;<input type="submit" tabindex="2" name="submit"
value="Upload Image" /></form>

Thanks for your hints and suggestions on how I have to go about this!


Search Discussions

  • Brian Jones at Sep 24, 2010 at 2:10 pm
    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):
    self.port = port

    def run(self):
    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,

    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)

    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!
    On Fri, Sep 24, 2010 at 4:23 AM, Dennis Lee Bieber 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-' }

    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
    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:

    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

    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/


    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>

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
postedSep 23, '10 at 6:10p
activeSep 24, '10 at 2:10p

2 users in discussion

Brian Jones: 1 post Cerr: 1 post



site design / logo © 2022 Grokbase