2

I'm new to python and after struggling with myself a little bit I almost got the code to working.

import urllib, urllib2, cookielib

username = 'myuser'
password = 'mypass'

cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
login = urllib.urlencode({'user' : username, 'pass' : password})
opener.open('http://www.ok.com/', login)
mailb = opener.open('http://www.ok.com/mailbox').read()
print mailb

But the output I got after print is just a redirect page.

<html>

<head>

<META HTTP-EQUIV="Refresh" CONTENT="0;URL=https://login.ok.com/login.html?skin=login-page&dest=REDIR|http://www.ok.com/mailbox">

<HTML dir=ltr><HEAD><TITLE>OK :: Redirecting</TITLE>

</head>

</html>

Thanks

1
  • You can now use this cj to browse website. Commented Mar 2, 2011 at 19:21

1 Answer 1

3

If a browser got that response, it would interpret it as a request to redirect to the URL specified.

You will need to do something similar with your script. You need to parse the <META> tag and locate the URL and then do a GET on that URL.

Sign up to request clarification or add additional context in comments.

3 Comments

At last it worked, thanks. I also think I were a bit lucky. if I tried the same thing in another website, it could have not worked. it will always output the redirect page first ? I can always rely on this method ?
@sophiaw, Who knows what ok.com will do? They're under no obligation to continue showing a redirect on login. It is up to you to continue testing your app to ensure it works with the current version of ok.com.
@S.Lott, thanks for the edit, that's much more helpful to the OP.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.