当前位置: 动力学知识库 > 问答 > 编程问答 >

Python Authentication

问题描述:

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

网友答案:

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.

分享给朋友:
您可能感兴趣的文章:
随机阅读: