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()

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAZkAAAD5CAIAAAB6RN93AAAgAElEQVR4nO2d229UV5bG6z+YV2Op/4Ie+bk1MtBSgjSv8xIFVPMCKK1ckfoBSxYDgsQ2JN04ILvb1hBEcMAOhDJlczEmkBpCys7NmMLuBMYxjeIYxxnadYnt0HW6ax7Oba+91z7n1NVVh+/TJ4RPndrngs+PtS9nrUg6k4VhGG50Rzb8DGAYhss3WAbDcBgMlsEwHAaDZTAMh8FgGQzDYTBYBsNwGGyxLNm5tWlT886z9gcTnZs3NTftGkxPdG7etLVjgv/ymV3NTZuaN3dOqB+ZDQre2tG5m25pJkcs0p98f+3Qvd//x2f/9pubv9p269d7p3ePPjy/4XcThuGNsoZlZ3c3bWpu2jVo0qpJgzMPlqUz2XRmcOem5qZNu89wn5rfLYFlC39bOHTv9/96/V9UH7r3+8DtjLziIPXVEfGji6/a259/dzrAdp3d/Te9dlE46Ctxe5/4a03ijzAMl+GIySkdy9Im5nZ1JifsjQFstWMGd1s6k9bBJjq2OGgz/66N+DysA5npl79+IUAjqaPP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C3itNIXHaOdXOJ0BQ1YX3MSFbxb8mBUH1I7AMMoUC2lBjCyyDICgMAssgCAqDwDIIgsIg1MeEICgM+n8XeA7ZdXwJYwAAAABJRU5ErkJggg==" alt="" />

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

python urllib2使用心得的更多相关文章

  1. python urllib2 模拟网站登陆

    python urllib2 模拟网站登陆 1. 可用浏览器先登陆,然后查看网页源码,分析登录表单 2. 使用python urllib2,cookielib 模拟网页登录 import urllib ...

  2. Python urllib2写爬虫时候每次request open以后一定要关闭

    最近用python urllib2写一个爬虫工具,碰到运行一会程序后就会出现scoket connection peer reset错误.经过多次试验发现原来是在每次request open以后没有及 ...

  3. Python urllib2 调试

    #!/usr/bin/env python # coding=utf-8 __author__ = 'zhaoyingnan' import urllib import urllib2 import ...

  4. python urllib2详解及实例

    urllib2是Python的一个获取URLs(Uniform Resource Locators)的组件.他以urlopen函数的形式提供了一个非常简单的接口, 这是具有利用不同协议获取URLs的能 ...

  5. python urllib2与urllib

    1.urllib2可以接受一个Request对象,并以此可以来设置一个URL的headers,但是urllib只接收一个URL. 2.urllib模块可以提供进行urlencode的方法,该方法用于G ...

  6. python urllib2对http的get,put,post,delete

    #GET: #!/usr/bin/env python# -*- coding:utf-8 -*-import urllib2def get():    URL = 'www.baidu.com'  ...

  7. python urllib2 httplib HTTPConnection

    httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现. import httplib conn  ...

  8. python urllib2 对 http 的 get,put,post,delete

    #GET: #!/usr/bin/env python# -*- coding:utf-8 -*-import urllib2def get():    URL = 'www.baidu.com'   ...

  9. Python unittest模块心得(一)

    关于unittest模块的一些心得,主要是看官网的例子,加上一点自己的理解,官网地址:https://docs.python.org/3.6/library/unittest.html 基础概念介绍: ...

随机推荐

  1. 二叉树的遍历(递归,迭代,Morris遍历)

    二叉树的遍历: 先序,中序,后序: 二叉树的遍历有三种常见的方法, 最简单的实现就是递归调用, 另外就是飞递归的迭代调用, 最后还有O(1)空间的morris遍历: 二叉树的结构定义: struct ...

  2. win7(X64)+VS2013+OpenCV3.1环境配置

    &1 源文件 VS2013: 链接:http://pan.baidu.com/s/1o8EKQq2 密码:open OpenCV3.1:  链接:http://pan.baidu.com/s/ ...

  3. Photoshop Cc高级设计师培训视频教程(共109节课程)

    Photoshop Cc高级设计师培训视频教程(共109节课程) 专业培训Photoshop技能,其他网站收费内容,这里收集了,免费分享给你们哦~ 以下为部分截图: 下载地址: http://fu83 ...

  4. Linux常用指令---系统管理

    四.linux服务器" [have no space]"开发机dev2上不管是upa/upc上传文件时,会挂载在/目录下,而此时这里只有3G空间,通过df -h显示各大挂载点使用情 ...

  5. 20145222黄亚奇《Java程序设计》第2周学习总结

    教材学习内容总结 类型: 整数:short(占2字节).int(4).long(8). 浮点数:float(4).double(8) 字符:char(2) 布尔:boolean类型表示true与fal ...

  6. error C2065: “IDD_DIALOG1” : 未声明的标识符

    编译时提示error C2065: “IDD_DIALOG1” : 未声明的标识符 错误的可能原因及解决方法如下: 1.出错文件中没有包含资源文件ID声明的resource.h文件.在出错文件中加入# ...

  7. (旧)子数涵数·PS ——翻页效果

    一.首先在网络上下载一张图片,作为素材.这是我下载的素材,至于为什么选择这张照片呢,当然不是因为自己的一些羞羞的念头啦. 二.打开Photoshop,我使用的版本是CS3(因为CS3所占的磁盘空间较小 ...

  8. [BZOJ 1483][HNOI 2009]梦幻补丁(有序表启发式合并)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1483 分析: 先将不同的颜色的出现位置从小到大用几条链表串起来,然后统计一下答案 对于 ...

  9. Linux各目录作用

  10. 第五章:Javascript语句

    在javascript中,表达式是短语,那么语句(statement)就是整句或命令.正如英文语句以句号结尾,javascript以分号结尾. 表达式计算出一个值,但语句使某件事发生. “使某件事发生 ...