Skip to content

Commit d59841f

Browse files
authored
enable proxy config (#28)
* enable proxy config * modified: learn.py
1 parent 45d1a6a commit d59841f

File tree

1 file changed

+22
-11
lines changed

1 file changed

+22
-11
lines changed

learn.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,26 @@
1414
import ssl
1515

1616
ssl._create_default_https_context = ssl._create_unverified_context
17-
global dist_path
18-
dist_path = ''
17+
global dist_path, url, user_agent, headers, cookie, opener, err404
18+
dist_path = url = user_agent = headers = cookie = opener = err404 = None
1919

20-
url = 'https://learn.tsinghua.edu.cn'
21-
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
22-
headers = {'User-Agent': user_agent, 'Connection': 'keep-alive'}
23-
cookie = http.cookiejar.MozillaCookieJar()
24-
handler = urllib.request.HTTPCookieProcessor(cookie)
25-
opener = urllib.request.build_opener(handler)
26-
urllib.request.install_opener(opener)
27-
err404 = '\r\n\r\n\r\n<script type="text/javascript">\r\n\tlocation.href="/";\r\n</script>'
2820

21+
def build_global(args):
22+
global dist_path, url, user_agent, headers, cookie, opener, err404
23+
dist_path = args.dist
24+
url = 'https://learn.tsinghua.edu.cn'
25+
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'
26+
headers = {'User-Agent': user_agent, 'Connection': 'keep-alive'}
27+
handlers = []
28+
if args.http_proxy:
29+
handlers.append(urllib.request.ProxyHandler({'http': args.http_proxy}))
30+
if args.https_proxy:
31+
handlers.append(urllib.request.ProxyHandler({'https': args.https_proxy}))
32+
cookie = http.cookiejar.MozillaCookieJar()
33+
handlers.append(urllib.request.HTTPCookieProcessor(cookie))
34+
opener = urllib.request.build_opener(*handlers)
35+
urllib.request.install_opener(opener)
36+
err404 = '\r\n\r\n\r\n<script type="text/javascript">\r\n\tlocation.href="/";\r\n</script>'
2937

3038
def get_xsrf_token():
3139
cookie_obj = cookie._cookies.get('learn.tsinghua.edu.cn', dict()).get('/', dict()).get('XSRF-TOKEN', None)
@@ -435,13 +443,16 @@ def get_args():
435443
parser.add_argument('-p', "--_pass", type=str, default='.pass')
436444
parser.add_argument('-c', "--cookie", type=str, default='', help='Netscape HTTP Cookie File')
437445
parser.add_argument('-d', '--dist', type=str, default='', help='download path')
446+
parser.add_argument('--http_proxy', type=str, default='', help='http proxy')
447+
parser.add_argument('--https_proxy', type=str, default='', help='https proxy')
438448
args = parser.parse_args()
439449
return args
440450

441451

442452
def main(args):
443453
global dist_path
444-
dist_path = args.dist
454+
build_global(args)
455+
assert (dist_path is not None) and (url is not None) and (user_agent is not None) and (headers is not None) and (cookie is not None) and (opener is not None) and (err404 is not None)
445456
if args.clear:
446457
clear(args)
447458
exit()

0 commit comments

Comments
 (0)