3

I am basically trying to export a configuration file, once a week. While the product in question allows you to log in manually via a web client, enter some information, and get an XML file back when you submit, there's no facility for automating this. I can get away with using Python 2.5 (have used for a while) or 2.6 (unfamiliar) to do this.

  1. I think I need to have some way to authenticate against the product. Although I can view the cookie in Firefox, when I looked through the actual cookie.txt file, it was not present. Didn't show up after clearing my private data and re-authenticating. Odd. Should I be shooting for the Cookie module, or could this be some arcane method of authentication that only looks like cookies? How would I know?

  2. I think I need the httplib module to do the HTTP POST, but I don't know how to do the multipart/form-data encoding. Have I overlooked a handy option, or is this something where I must roll my own?

  3. I assume I can get the XML file back in the HTTPesponse from httplib.

I've fetched web things via Python before, but not with POST, multipart encoding, and authentication in the mix.

Cairnarvon
  • 25,981
  • 9
  • 51
  • 65
  • Duplicate: http://stackoverflow.com/questions/393738/programmatic-form-submit. Also Python 2.6 is so much like 2.5 you can't easily tell the difference. – S.Lott Feb 20 '09 at 20:26
  • Use the following URL to get relevant questions and answers: http://stackoverflow.com/search?q=urllib2 – S.Lott Feb 20 '09 at 20:27
  • http://stackoverflow.com/search?q=%5Bpython%5D+mechanize – jfs Feb 20 '09 at 20:34

3 Answers3

3

urllib2 should cover all of this.

Here's a Basic Authentication example.

Here's a Post with multipart/form-data.

Hank Gay
  • 70,339
  • 36
  • 160
  • 222
2

Try mechanize module.

jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

You should look at the MultipartPostHandler:

http://odin.himinbi.org/MultipartPostHandler.py

And if you need to support unicode file names , see a fix at: http://peerit.blogspot.com/2007/07/multipartposthandler-doesnt-work-for.html

user41767
  • 1,217
  • 1
  • 17
  • 26