码迷,mamicode.com
首页 > Web开发 > 详细

重新认识urllib

时间:2020-02-13 09:35:23      阅读:64      评论:0      收藏:0      [点我收藏+]

标签:data   header   open   com   read   from   ica   pre   test   

# 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.cnblogs.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)

  

重新认识urllib

标签:data   header   open   com   read   from   ica   pre   test   

原文地址:https://www.cnblogs.com/SunshineKimi/p/12301974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!