Scripting HTTP POST - multipart/form-data

Discussion in 'Python' started by Jeff Shannon, Jul 19, 2004.

  1. Jeff Shannon

    Jeff Shannon Guest

    I'm looking to script a routine file-upload through an HTTP server
    (which I don't have access to). I've got specs on the CGI app that
    receives this upload (i.e. field names and expected contents).

    However, it looks like urllib/urllib2 won't handle multipart/form-data
    POSTs. I tried hacking together something with httplib, but I'm
    severely hampered by the fact that I don't know HTTP well enough to know
    what headers I need to send, and my attempts to find (through Google) a
    list of necessary headers haven't turned out too well. I really don't
    want to have to scour the entire HTTP specs for this...

    At a quick glance, it seems that PyCURL may do what I need, but it's...
    well, not exactly user-friendly (at least for someone with zero
    experience with libcurl), and the machine I'm trying to send from (RH9)
    doesn't have libcurl installed. (I'd prefer to not have to install
    anything just for this, if I can avoid it...)

    Is there another (preferably all-python) library that will work for
    this? Or perhaps a friendly how-to that'll explain what I need to know
    to use httplib?

    Jeff Shannon
    Technician/Programmer
    Credit International
    Jeff Shannon, Jul 19, 2004
    #1
    1. Advertising

  2. Jeff Shannon

    John J. Lee Guest

    Jeff Shannon <> writes:

    > I'm looking to script a routine file-upload through an HTTP server
    > (which I don't have access to). I've got specs on the CGI app that
    > receives this upload (i.e. field names and expected contents).
    >
    > However, it looks like urllib/urllib2 won't handle multipart/form-data
    > POSTs.


    Neither should have any problem with multipart/form-data. They don't
    actually provide and specific support for it either, though.

    [...]
    > Is there another (preferably all-python) library that will work for
    > this?

    [...]

    Yes, one that works on top of urllib2:

    http://wwwsearch.sf.net/ClientForm


    Search for .add_file on that page for an example of file upload. IIRC
    I've posted examples of that here in the past, too.


    John
    John J. Lee, Jul 20, 2004
    #2
    1. Advertising

  3. Jeff Shannon

    John J. Lee Guest

    Jeff Shannon <> writes:

    > I'm looking to script a routine file-upload through an HTTP server
    > (which I don't have access to). I've got specs on the CGI app that
    > receives this upload (i.e. field names and expected contents).

    [...]

    Forgot to add: if I had to do this and wanted to use ClientForm (which
    I certainly would ;-), I'd just write a snippet of HTML:

    formHtml = """\
    <form action="http://www.example.com/cgi-bin/spam.cgi">
    <input type="text" name="nrEmails" />

    <input type="checkbox" name="spamType" value="ludicrous" />
    <input type="checkbox" name="spamType" value="absurd" />
    </form>
    """

    import StringIO
    from ClientForm import ParseFile

    forms = ParseFile(StringIO.StringIO(formHtml), "http://www.example.com/")
    ....


    Which is surely a very readable way of writing down such a spec :)

    I should point this out in the docs...


    John
    John J. Lee, Jul 20, 2004
    #3
  4. Jeff Shannon

    Jeff Shannon Guest

    John J. Lee wrote:

    >Jeff Shannon <> writes:
    >
    >
    >
    >>I'm looking to script a routine file-upload through an HTTP server
    >>(which I don't have access to). I've got specs on the CGI app that
    >>receives this upload (i.e. field names and expected contents).
    >>
    >>However, it looks like urllib/urllib2 won't handle multipart/form-data
    >>POSTs.
    >>
    >>

    >
    >Neither should have any problem with multipart/form-data. They don't
    >actually provide and specific support for it either, though.
    >
    >


    True. I realized that, I was merely inexact with my wording. ;) The
    problem being that I don't know how to enhance them to add that support...

    >[...]
    >
    >
    >>Is there another (preferably all-python) library that will work for
    >>this?
    >>
    >>

    >[...]
    >
    >Yes, one that works on top of urllib2:
    >
    >http://wwwsearch.sf.net/ClientForm
    >
    >


    Ah, thank you! That looks like just what I needed.

    Jeff Shannon
    Technician/Programmer
    Credit International
    Jeff Shannon, Jul 20, 2004
    #4
  5. Jeff Shannon <> wrote in message news:<>...
    > I'm looking to script a routine file-upload through an HTTP server
    > (which I don't have access to). I've got specs on the CGI app that
    > receives this upload (i.e. field names and expected contents).
    >


    Here's a recipe I posted to the Python Cookbook a couple of years ago.
    Works with IIS.
    http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306

    -- Wade Leftwich
    Ithaca, NY
    Wade Leftwich, Jul 21, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bruno Dilly
    Replies:
    0
    Views:
    421
    Bruno Dilly
    Aug 18, 2006
  2. Gabriel Genellina
    Replies:
    0
    Views:
    907
    Gabriel Genellina
    Aug 19, 2006
  3. leoboiko
    Replies:
    0
    Views:
    363
    leoboiko
    Aug 6, 2008
  4. Kevin DeValck
    Replies:
    1
    Views:
    703
    7stud --
    May 17, 2011
  5. Replies:
    1
    Views:
    385
Loading...

Share This Page