[twill] Making a POST request without a form

Ryan van Roode rvanroode at gmail.com
Wed Jul 4 18:40:45 PDT 2007


Thanks very much, Titus. After experimenting a bit, I was able to do
this with mechanize:

--
import urllib, mechanize

br = mechanize.Browser()

# log in
br.open("http://www.blah.com/")
br.select_form(nr=0)
br["username"] = "test"
br["password"] = "test"
r = br.submit()
assert "Welcome" in r.get_data()

# now post a request without a form
data = urllib.urlencode({'some_var':'blah','another_var':'foo'})
r = br.open("http://www.blah.com/postRequestHere/", data)

# print the response
print r.read()
--

The next step is to figure out how to mix twill and mechanize commands
in the same browser session. Thanks again.

~rvr

On 7/3/07, Titus Brown <titus at caltech.edu> wrote:
> On Tue, Jul 03, 2007 at 12:53:42PM +0800, Ryan van Roode wrote:
> -> Hello, and sorry if I've already sent this. Is there a way to send a
> -> POST request through twill without using an actual form? I'm trying to
> -> mimic some requests from a flash app. If there isn't, does anyone know
> -> another way to do this? I'd like to be able to navigate a site, change
> -> some things, send these POST requests, then navigate some more, all
> -> within the same session. Thanks very much. Twill is a great tool.
>
> Hi, Ryan,
>
> you should be able to do this with urllib2; see
>
>         http://www.voidspace.org.uk/python/articles/urllib2.shtml
>
> (search for POST).
>
> mechanize can do this as well.  I don't have an API for it in twill,
> though.
>
> cheers,
>
> --titus
>



More information about the twill mailing list