Use the mechanism to access an authenticated HTTP web page.

In my current program, I am accessing an HTTP authenticated page like this one that works fine:

import urllib2
url = 'http://test.localdomain/test.pl'
realm = 'Test DB'
username = 'foo'
password = 'bar'
auth_handler = urllib2.HTTPBasicAuthHandler()
auth_handler.add_password(realm, uri , username, password)
opener = urllib2.build_opener(auth_handler)
data = opener.open(url).read()

Now I want to click the button on this page after logging in. I found a mechanics library for Python that can easily do such things. Unfortunately, I was not able to successfully complete the same basic authentication as above when using mechanize. This is what I tried:

from mechanize import Browser
url = 'http://test.localdomain/test.pl'
realm = 'Test DB'
username = 'foo'
password = 'bar'
browser = Browser()
browser.add_password(url, username, password, realm)
browser.open(url)

But then I get the following exception:

HTTP Error refresh: The HTTP server returned a redirect error that would lead to an     
infinite loop.
The last 30x error message was:
OK

How can i fix this? Or can I let the mechanization use the already existing authhander created by urllib2 in my first fragment?

+3
source share
2 answers

. .

-3

script . .

browser.add_password(url, username, password, realm)
urllib2.urlopen(url)

browser.click(), , , / " " urllib2.

-1

Source: https://habr.com/ru/post/1778280/


All Articles