Python urllib2基本身份验证问题
更新:基于Lee的评论,我决定将我的代码压缩为一个非常简单的脚本,然后从命令行运行它: import urllib2 import sys username = sys.argv[1] password = sys.argv[2] url = sys.argv[3] print("calling %s with %s:%s\n" % (url, username, password)) passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, url, username, password) urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman))) req = urllib2.Request(url) f = urllib2.urlopen(req) data = f.read() print(data) 不幸的是,它仍然不会生成Authorization标题(每个Wireshark):( 我在通过urllib2发送基本AUTH时遇到问题。我看了这篇文章,并跟随了这个例子。我的代码: passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, "api.foursquare.com", username, password) urllib2.install_opener(urllib2.build_opener(urllib2.HTTPBasicAuthHandler(passman))) …