Python模拟HTTPS请求返回HTTP 401 unauthorized错误
在文章 Python Web中REST API使用示例——基于云平台+云服务打造自己的在线翻译工具 中,用到了Python模拟HTTPS请求的功能;
开始是使用的 httplib模块,代码如下:
header = {"Content-type": "application/json", "Accept": "*/*" }
params = { 'source':'en', 'target':'es', 'text':match.group(1) }
data = urllib.urlencode(params)
surl = urlparse('https://gateway.watsonplatform.net/language-translation/api/v2/translate')
#surl = urlparse('https://www.baidu.com/')
resContent = ''
try:
conn = httplib.HTTPSConnection(surl.netloc)
conn.request('GET', surl.path + '?' + data)
response = conn.getresponse()
resContent = data + "<br />" + getAllAttrs(response) #response.read() #
except:
info=sys.exc_info()
resContent = getAllAttrs(info[0]) + getAllAttrs(info[1])后来经过一
Python HTTPS模拟请求遇到401错误解决方案

本文介绍了在使用Python 2.7.10的urllib2模块进行HTTPS请求时遇到HTTP 401 unauthorized错误的问题。在尝试httplib2、requests和pycurl等替代库后,最终通过调整urllib2的代码成功解决了问题。
最低0.47元/天 解锁文章
2382

被折叠的 条评论
为什么被折叠?



