Python模拟HTTPS请求返回HTTP 401 unauthorized错误
2016-05-09 18:12
609 查看
http://blog.csdn.net/testcs_dn/article/details/50451762
Python模拟HTTPS请求返回HTTP 401 unauthorized错误
在文章 Python Web中REST API使用示例——基于云平台+云服务打造自己的在线翻译工具 中,用到了Python模拟HTTPS请求的功能;
开始是使用的 httplib模块,代码如下:
[python] view
plain copy
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])
后来经过一番搜索发现,httplib根本不支持需要身份验证的这种请求;
改为以下代码成功:
[python] view
plain copy
params = { 'source':'en', 'target':'es', 'text':match.group(1) }
surl = 'https://gateway.watsonplatform.net/language-translation/api/v2/translate?' + urllib.urlencode(params)
resContent = ''
try:
passman = urllib2.HTTPPasswordMgrWithDefaultRealm() #创建域验证对象
passman.add_password(None, surl, "c9819718-4660-441c-9df7-07398950ea44", "qUvrJPSdPgOx") #设置域地址,用户名及密码
auth_handler = urllib2.HTTPBasicAuthHandler(passman) #生成处理与远程主机的身份验证的处理程序
opener = urllib2.build_opener(auth_handler) #返回一个openerDirector实例
urllib2.install_opener(opener) #安装一个openerDirector实例作为默认的开启者。
response = urllib2.urlopen(surl) #打开URL链接,返回Response对象
resContent = response.read() #读取响应内容
except:
info=sys.exc_info()
resContent = getAllAttrs(info[0]) + getAllAttrs(info[1]) #获取异常的详细信息
支持需要身份验证的请求的模块有以下几个:
httplib2,urllib2,requests,pycurl
但我安装的Python 2.7.10默认只带了 urllib2,所以就选择使用它了。
Python模拟HTTPS请求返回HTTP 401 unauthorized错误
在文章 Python Web中REST API使用示例——基于云平台+云服务打造自己的在线翻译工具 中,用到了Python模拟HTTPS请求的功能;
开始是使用的 httplib模块,代码如下:
[python] view
plain copy
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])
后来经过一番搜索发现,httplib根本不支持需要身份验证的这种请求;
改为以下代码成功:
[python] view
plain copy
params = { 'source':'en', 'target':'es', 'text':match.group(1) }
surl = 'https://gateway.watsonplatform.net/language-translation/api/v2/translate?' + urllib.urlencode(params)
resContent = ''
try:
passman = urllib2.HTTPPasswordMgrWithDefaultRealm() #创建域验证对象
passman.add_password(None, surl, "c9819718-4660-441c-9df7-07398950ea44", "qUvrJPSdPgOx") #设置域地址,用户名及密码
auth_handler = urllib2.HTTPBasicAuthHandler(passman) #生成处理与远程主机的身份验证的处理程序
opener = urllib2.build_opener(auth_handler) #返回一个openerDirector实例
urllib2.install_opener(opener) #安装一个openerDirector实例作为默认的开启者。
response = urllib2.urlopen(surl) #打开URL链接,返回Response对象
resContent = response.read() #读取响应内容
except:
info=sys.exc_info()
resContent = getAllAttrs(info[0]) + getAllAttrs(info[1]) #获取异常的详细信息
支持需要身份验证的请求的模块有以下几个:
httplib2,urllib2,requests,pycurl
但我安装的Python 2.7.10默认只带了 urllib2,所以就选择使用它了。
相关文章推荐
- 图解命令行http工具curl使用初步
- AJAX(XMLHttpRequest)进行跨域请求方法详解(三)
- AJAX(XMLHttpRequest)进行跨域请求方法详解(二)
- AJAX(XMLHttpRequest)进行跨域请求方法详解(一)
- HTTP的POST提交的四种常见消息主体格式
- web client6.0设置刀片服务器虚拟机网络
- Struts2 HTTP对象传递
- Android OkHttp完全解析 是时候来了解OkHttp了
- 简析TCP的三次握手与四次分手
- delphi使用IdHTTP模拟提交页面方法总结
- NodeJS学习笔记之Http模块
- nodejs http 发送请求
- Nodejs获取网络数据并生成Excel表格
- 《网络游戏服务器编程》
- Delphi IDhttp 获取Cookie
- Linux服务器利用Nethogs监控每个进程的网络使用情况
- [置顶] KMeans算法检测网络异常入侵
- TCP/IP协议学习之TCP、IP篇
- delphi的idhttp的get 和 post的使用
- 网络编程知识(2)--Socket理解