开发者

python authenticate with urllib2 and cookielib and get login passed/failed result

Here is cut from my code which I use to login into the remote site. My problem is that I don't know how to handle authentication pass/fail result.

def prepareLoginData(self):
    self.post_login_data = urllib.urlencode({
                                                'login': self.user,
                                                'password': self.password,
                                                'Login': 'Login'
                                                })
    return self.post_login_data

def prepareOpener(self):
    cj = cookielib.CookieJar()
    self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
    for header in self.headers:
        self.opener.addheaders.append(header)
    return self.opener

Then I login like below:

self.resp = self.opener.open(self.login_page, self.post_login_data)

and parse the response self.resp.read() to check if login passed with regular expression. How can I get login result based on cookie value? or maybe there is another way? When auth passed or failed the only thing I see in cj is SESSID which does not give info about auth result开发者_运维问答.

Thanks in advance!


Maybe you can look at the source code of the returned page: In a lot of moderns websites, every xhtml element of the page has an id or a class or is child on an element with an id or a class, so i think you can use an xhtml parser like BeautifulSoup to extract error messages from the website.

BeautifulSoup is verry easy to learn and to use, so you'll probabilly find a solution, but if not, give me the url of the website, and i'll try to write a working code...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜