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 &