博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python urllib2使用心得
阅读量:5884 次
发布时间:2019-06-19

本文共 2869 字,大约阅读时间需要 9 分钟。

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/shhnwangjiansend_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/shhnwangjianreq = urllib2.Request(url, headers=send_headers) f = urllib2.urlopen(req, timeout=10) response_head = f.info().getheader("Content-Type")  # 获取返回包头中Content-Type内容print response_headf.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/shhnwangjiansend_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/shhnwangjiansend_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_headf.close()

 

7、http post 请求,json数据包

send_headers = {"User-Agent": "shhnwangjian", "Content-Type": "application/json"}url = http://www.cnblogs.com/shhnwangjianbody = 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介绍: 

import base64import urllib2url = http://ip:port/api/channelsbasicpwd = 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包头、包体的学习参考:

你可能感兴趣的文章
2016第6周五
查看>>
ASP.NET 免费开源控件
查看>>
面向对象葵花宝典阅读思维导图(二)
查看>>
volatile关键字与线程间通信
查看>>
优秀大数据GitHub项目一览
查看>>
WPF 窗体显示最前端
查看>>
tit.Atitit. http 代理原理 atiHttpProxy 大木马 h
查看>>
TCP/IP详解学习笔记(8)-DNS域名系统
查看>>
WPF 之 布局(一)
查看>>
Wireshark设置interface 时提示“There are no interfaces on which a capture can be done ”
查看>>
Android Studio使用SVN,与eclipse共同开发。
查看>>
iOS图片上传及压缩
查看>>
CentOS 7 ARM 版发布:支持树莓派2/香蕉派/CubieTruck
查看>>
Android Studio 编写 JNI
查看>>
nginx配置文件或目录404和403
查看>>
基于Bootstrap简单实用的tags标签插件
查看>>
ANDROID版本号和版本名称的重要性介绍
查看>>
DbContext运行时动态附加上一个dbset
查看>>
每天一个linux命令(43):killall命令
查看>>
Java代码块
查看>>