requests 旧城等待, 2021-06-24 14:36 436阅读 0赞 在网上看到两个对比的例子,我觉得挺好的,我那过来给大家看看 **例子1:** #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib2 gh_url = 'https://api.github.com' req = urllib2.Request(gh_url) password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm() password_manager.add_password(None, gh_url, 'user', 'pass') auth_manager = urllib2.HTTPBasicAuthHandler(password_manager) opener = urllib2.build_opener(auth_manager) urllib2.install_opener(opener) handler = urllib2.urlopen(req) print handler.getcode() print handler.headers.getheader('content-type') # ------ # 200 # 'application/json' **例子2: requests==2.11.1** #!/usr/bin/env python # -*- coding: utf-8 -*- import requests r = requests.get('https://api.github.com', auth=('user', 'pass')) print r.status_code print r.headers['content-type'] # ------ # 200 # 'application/json' 代码的编写简易立分,而且requests包还支持post,put,patch,delete方式的请求,真心很不错.
还没有评论,来说两句吧...