-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
35 lines (26 loc) · 1.01 KB
/
utils.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
32
33
34
35
import urllib.request
import urllib.response
import sys
def save_url_to_file(url, filename):
with urllib.request.urlopen(url) as response:
with open(filename, 'wb') as file:
if file.write(response.read()):
return 0;
return 1
def save_url_to_file_with_auth(url, filename, username, password):
# create a password manager
password_mgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
password_mgr.add_password(None, url, username, password)
handler = urllib.request.HTTPBasicAuthHandler(password_mgr)
# create "opener" (OpenerDirector instance)
opener = urllib.request.build_opener(handler)
# use the opener to fetch a URL
opener.open(url)
# Install the opener.
# Now all calls to urllib.request.urlopen use our opener.
urllib.request.install_opener(opener)
with urllib.request.urlopen(url) as response:
with open(filename, 'wb') as file:
if file.write(response.read()):
return 0;
return 1