Skip to content

Commit 96639a0

Browse files
committed
set urllib user-agent
1 parent 9c4fff9 commit 96639a0

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

pythonforandroid/recipe.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sh
1010
import shutil
1111
import fnmatch
12+
import urllib.request
1213
from urllib.request import urlretrieve
1314
from os import listdir, unlink, environ, mkdir, curdir, walk
1415
from sys import stdout
@@ -23,6 +24,11 @@
2324
from pythonforandroid.util import load_source as import_recipe
2425

2526

27+
url_opener = urllib.request.build_opener()
28+
url_orig_headers = url_opener.addheaders
29+
urllib.request.install_opener(url_opener)
30+
31+
2632
class RecipeMeta(type):
2733
def __new__(cls, name, bases, dct):
2834
if name != 'Recipe':
@@ -206,6 +212,8 @@ def report_hook(index, blksize, size):
206212
seconds = 1
207213
while True:
208214
try:
215+
# jqueryui.com returns a 403 w/ the default user agent
216+
url_opener.addheaders = [('User-agent', 'Mozilla/5.0')]
209217
urlretrieve(url, target, report_hook)
210218
except OSError as e:
211219
attempts += 1
@@ -215,6 +223,8 @@ def report_hook(index, blksize, size):
215223
time.sleep(seconds)
216224
seconds *= 2
217225
continue
226+
finally:
227+
url_opener.addheaders = url_orig_headers
218228
break
219229
return target
220230
elif parsed_url.scheme in ('git', 'git+file', 'git+ssh', 'git+http', 'git+https'):

0 commit comments

Comments
 (0)