Base Auth

mail@pastecode.io avatar
unknown
python
2 years ago
834 B
10
Indexable
import urllib2
import ssl

username = 'mylogin'
password = 'mypass'
login_url = 'https://lyrmin.ru/auth/test.php'
retrieve_status_page(username, password, login_url)

def retrieve_status_page(user, password, url):

    try:
        ssl._create_unverified_context
    except AttributeError:
        pass
    else:
        ssl._create_default_https_context = ssl._create_unverified_context

    password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    password_mgr.add_password(None, url, user, password)
    handler = urllib2.HTTPBasicAuthHandler(password_mgr)
    opener = urllib2.build_opener(handler)
    urllib2.install_opener(opener)

    req = urllib2.Request(url)
    try:
        response = urllib2.urlopen(req)
        return response.read()
    except Exception:
        raise CrawlError("can't access to http://%s", url)