python urllib2使用心得
1、http GET请求
过程:获取返回结果,关闭连接,打印结果
f = urllib2.urlopen(req, timeout=10)
the_page = f.read()
f.close()
print the_page
2、http GET请求 + 包头
paras = "Token=1234567890;Uuid=0987654321"
url = http://www.cnblogs.com/shhnwangjian
send_headers = {"Cookie": paras, "User-Agent": "shhnwangjian", "Content-Type": "application/json"}
req = urllib2.Request(url, headers=send_headers) # 生成页面请求的完整数据
f = urllib2.urlopen(req, timeout=10) # 发送页面请求
the_page = f.read()
f.close()
print the_page
3、http GET请求 + 包头,处理返回响应包头
paras = "Token=1234567890;Uuid=0987654321"
send_headers = {"Cookie": paras, "User-Agent": "shhnwangjian", "Content-Type": "application/json"}
url = http://www.cnblogs.com/shhnwangjian
req = urllib2.Request(url, headers=send_headers)
f = urllib2.urlopen(req, timeout=10)
response_head = f.info().getheader("Content-Type") # 获取返回包头中Content-Type内容
print response_head
f.close()
4、http POST请求
postdata = urllib.urlencode({"username": "test", "password": "123456", "type": "Z"})
f = urllib2.urlopen(url, postdata, timeout=10)
the_page = f.read()
f.close()
print the_page
5、http POST请求 + 包头
postdata = urllib.urlencode({"username": "test", "password": "123456", "type": "Z"})
paras = "Token=1234567890;Uuid=0987654321"
url = http://www.cnblogs.com/shhnwangjian
send_headers = {"Cookie": paras, "User-Agent": "shhnwangjian", "Content-Type": "application/json"}
req = urllib2.Request(url, headers=send_headers) # 包头
f = urllib2.urlopen(req, postdata, timeout=10)
the_page = f.read()
f.close()
print the_page
6、http POST请求 + 包头,处理返回响应包头
postdata = urllib.urlencode({"username": "test", "password": "123456", "type": "Z"})
paras = "Token=1234567890;Uuid=0987654321"
url = http://www.cnblogs.com/shhnwangjian
send_headers = {"Cookie": paras, "User-Agent": "shhnwangjian", "Content-Type": "application/json"}
req = urllib2.Request(url, headers=send_headers) # 包头
f = urllib2.urlopen(req, postdata, timeout=10)
response_head = f.info().getheader("Cookie") # 获取返回包头中Cookie内容
print response_head
f.close()
7、http post 请求,json数据包
send_headers = {"User-Agent": "shhnwangjian", "Content-Type": "application/json"}
url = http://www.cnblogs.com/shhnwangjian
body = json.dumps({
"version": 1,
"sblx": "1",
"yjxx": "wangjian_AUTH",
"token": token,
"filemd5": "",
"crc": 123
})
req = urllib2.Request(url, data=body, headers=send_headers)
f = urllib2.urlopen(req, timeout=2)
the_page = f.read()
f.close()
8、基于Basic access authentication的http请求,如调用RabbitMQ API接口
实现方式一:请求头中添加Authorization
Basic access authentication介绍: https://en.wikipedia.org/wiki/Basic_access_authentication
import base64
import urllib2
url = http://ip:port/api/channels
basicpwd = base64.b64encode("guest:guest") # 账号密码
send_headers = {"User-Agent": "TEST", "Content-Type": "application/json", "Authorization": "Basic %s" % basicpwd}
req = urllib2.Request(url, headers=self.head)
f = urllib2.urlopen(req, timeout=6)
the_page = f.read()
f.close()
实现方式二:http://guest:guest@ip:port/api/channels
http包头、包体的学习参考:http://www.cnblogs.com/shhnwangjian/p/5110304.html