Python: Making a http post without form

There are times when it is required to post to url without the for data.
Port with Form

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()

Post to a url without form
# 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()
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s