Simon Willison wants a Python version of Perl's WWW::Mechanize. I believe I've already written it. It appears in the form of two projects I've used to test the eKit website:
- The more recent web performance tester called PyWebPerf. PyWebPerf is a performance measuring tool written in Python which simulates a web browser fetching a page (handles cookies, multiple threads, image and css download). Command-line and cgi interfaces are provided. I wrote this as a replacement for apachebench so I could have my testing behave more like a web browser.
- The earlier web unit testing framework.
The code (and API) is designed to let your script look like a regular web browser. Recently I used a slightly modified version of PyWebPerf to do the following:
fetcher = WebFetcher() login = fetcher.fetch('https://www.xxxxxxxx.com/') response = login.postForm(0, login.POST, {'UserID': 'xxxxxxx', 'Password': 'xxxxxxxx'}) r2 = response.fetch('/account/msg_status.asp', {'FromDate': '1/Jan/2003', 'ToDate': '3/Feb/2003', 'MsgStatus': 'Failed', 'NumRec': '20', 'GenerateCSV': 'blah'}) r3 = r2.fetch('/account/preview/xxxxxxx.csv') print r3.body
Note that each successive fetch uses the previous fetch result object - thus carrying over any cookies sent back from the server. The WebFetcher code can be (and indeed has been in the above application) trivially changed to turn off the automatic image/css loading and timing marks.
Aside: yes, this is a real example of code I have to use to automatically get failure reports from one of our service providers. Yecch.