Larry Martell
Larry Martell

Reputation: 3756

logging into a web site with urllib

I am trying to login to a web site with urllib. This is an internal web site at my company.

The login page looks like this:

<form id="login" action="/accounts/login/" method="POST">
<label for="username">Username</label>
<input name="username" type="text" class="text"><br>
<label for="password">Password</label>
<input name="password" type="password" class="text"><br>
<input name="login_submit" type="submit" value="Sign in" class="submit">

My python looks like this:

url = 'http://10.188.36.250/accounts/login/'
values = {'user_name':'xxxx', 'password':'xxxxx'}
data = urllib.urlencode(values)
req = urllib2.Request(url,data)
res = urllib2.urlopen(req)

I get back:

urllib2.HTTPError: HTTP Error 403: FORBIDDEN

What am I doing wrong?

Upvotes: 3

Views: 678

Answers (3)

Jenzor
Jenzor

Reputation: 11

It is all because you must use headers with your connect. Insert one of most popular web-browsers. in your headers. Mozilla - at least. Your code will look like -

req = urllib2.Request( url , data, headers=headers)

Where headers = {'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.0; en-GB; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12'}

Upvotes: 1

Jonathan Vanasco
Jonathan Vanasco

Reputation: 15680

The default urllib2 open doesn't support cookies well. it actually doesn't support anything well, it's a f(##$@# nightmare to work with.

To accomplish your task using urllib2, look into urllib2.HTTPCookieProcessor() and urllib2.build_opener(). The former is passed into the latter, which you'll open pages with. I don't recall exactly, but your code could look something like:

opener= urllib2.build_opener( urllib2.HTTPCookieProcessor() )
req = urllib2.Request( url , data )
res = opener.open( req )

i'd also suggest looking at the requests module which makes doing internet-y things not suck so much in python ( http://docs.python-requests.org/en/latest/index.html )

Upvotes: 1

Guy Adini
Guy Adini

Reputation: 5494

You're doing everything right, except using "user_name" as the key instead of "username". Notice the form's variables.

Upvotes: 3

Related Questions