forked from schedutron/CPAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
urlopen_auth3.py
31 lines (24 loc) · 900 Bytes
/
urlopen_auth3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python2
import urllib.request, urllib.error, urllib.parse
LOGIN = 'schedutron'
PASSWD = 'somerandomstuff'
URL = 'http://localhost'
REALM = 'Secure Archive'
def handler_version(url):
hdlr = urllib.request.HTTPBasicAuthHandler()
hdlr.add_password(REALM, urllib.parse.urlparse(url)[1], LOGIN, PASSWD)
opener = urllib.request.build_opener(hdlr)
urllib.request.install_opener(opener)
return url
def request_version(url):
from base64 import encodestring
req = urllib.request.Request(url)
b64str = encodestring(bytes('%s:%s' % (LOGIN, PASSWD), 'utf-8'))[:-1]
req.add_header("Authorization", "Basic %s" % b64str)
return req
for func_type in ('handler', 'request'):
print("*** Using %s:" % func_type.upper())
url = eval("%s_version" % func_type)(URL)
f = urllib.request.urlopen(url)
print(str(f.readline(), 'utf-8'))
f.close()