# coding=utf-8
# urllib_get_file=urllib.request.urlretrieve(url=None,filename="test.zip")
# basic usage of urllib
from urllib import request
url = "https://www.cn.com/SunshineKimi/"
msg = request.Request(url)
# msg.add_header("1",2)
# msg.set_proxy()
rep = request.urlopen(msg)
print(rep.read())
# how to use proxy to request in urllib
proxies = {"http": "user:passwd@ip:port"} # buy proxy
proxy = {"http": "111.79.44.217:9999"} # free proxy
proxy_handler = request.ProxyHandler(proxy) # there also exist HttpHandler() basic handler
opener = request.build_opener(proxy_handler)
response = opener.open(url, data=None)
print(response.headers)
# auth by proxy
passwd_manager=request.HTTPPasswordMgrWithDefaultRealm()
passwd_manager.add_password(realm=None,uri=proxy,user="user",passwd="password")
buy_auth_handler=request.ProxyBasicAuthHandler(passwd_manager)
opener_auth=request.build_opener(buy_auth_handler)
response_auth=opener_auth.open(url=None,data=None).read()
print(response_auth)