urllib2 是Python自带的标准模块, 用来发送HTTP Request的。  类似于 .NET中的,  HttpWebRequest类

urllib2 的优点

Python urllib2 发出的HTTP Request, 能自动被Fiddler截获, 方便了调试。

Python 可以自动处理Cookie

urllib2 的缺点

Python urllib2 发出的http Request, 中的header 会被修改成“首字母大写”,

比如你的代码里写的header 是: content-TYPE=application/x-www-form-urlencoded ,  会被修改为 Content-Type=application/x-www-form-urlencoded

实例一,  Get方法, 并且自定义header

# -* - coding: UTF-8 -* -
import urllib2 request = urllib2.Request("http://www.baidu.com/")
request.add_header('content-TYPE', 'application/x-www-form-urlencoded')
response = urllib2.urlopen(request)
print response.getcode()
print response.geturl()
print response.read()

实例二, post方法

# -* - coding: UTF-8 -* -
import urllib2
import urllib request = urllib2.Request("http://passport.cnblogs.com/login.aspx")
request.add_header('content-TYPE', 'application/x-www-form-urlencoded')
data={"tbUserName":"test_username", "tbPassword":"test_password"} response = urllib2.urlopen(request, urllib.urlencode(data))
print response.getcode()
print response.geturl()
print response.read()

实例三: Cookie 的处理

# -* - coding: UTF-8 -* -
import urllib2
import urllib
import cookielib cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) request = urllib2.Request("https://dynamic.12306.cn/otsweb/")
request.add_header('content-TYPE', 'application/x-www-form-urlencoded')
data={"tbUserName":"test_username", "tbPassword":"test_password"} response = opener.open(request, urllib.urlencode(data)) # send again, you will see cookie sent to web server
response = opener.open(request, urllib.urlencode(data)) print response.getcode()
print response.geturl()
print response.read()

实例四:如何处理跳转

创建Opener时, ul2.HTTPRedirectHandler是默认被加上的handler之一

Python自动化测试 (九)urllib2 发送HTTP Request的更多相关文章

  1. 《Python自动化测试九章经》

    Python是当前非常流行的一门编程语言,它除了在人工智能.数据处理.Web开发.网络爬虫等领域得到广泛使用之外,他也非常适合软件测试人员使用,但是,对于刚入行的测试小白来说,并不知道学习Python ...

  2. python自动化测试学习笔记-6urllib模块&request模块

    python3的urllib 模块提供了获取页面的功能. urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capat ...

  3. python中urllib, urllib2,urllib3, httplib,httplib2, request的区别

    permike原文python中urllib, urllib2,urllib3, httplib,httplib2, request的区别 若只使用python3.X, 下面可以不看了, 记住有个ur ...

  4. python自动化测试框架学习

    今天发现python有多个框架可以用于自动化测试方面,下面整理了下splinter和urllib2框架,对于pywinauto框架和ruby框架先记录下以后需要用到再学习. python有个splin ...

  5. python使用代理ip发送http请求

    一.需求背景 网站刷票时,经常会遇到限制一个ip只能投票一次的限制,为此需要使用代理ip 二.脚本如下: 1.Proxy_http.py使用代理ip发送httpr的get和post请求 #coding ...

  6. Python 标准库 urllib2 的使用细节

    刚好用到,这篇文章写得不错,转过来收藏.    转载自 道可道 | Python 标准库 urllib2 的使用细节 Python 标准库中有很多实用的工具类,但是在具体使用时,标准库文档上对使用细节 ...

  7. Python:urllib和urllib2的区别(转)

    原文链接:http://www.cnblogs.com/yuxc/ 作为一个Python菜鸟,之前一直懵懂于urllib和urllib2,以为2是1的升级版.今天看到老外写的一篇<Python: ...

  8. python urllib和urllib2 区别

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

  9. 道可叨 | Python 标准库 urllib2 的使用细节

    道可叨 | Python 标准库 urllib2 的使用细节 request = urllib2.Request(uri) request.add_header('User-Agent', 'fake ...

随机推荐

  1. angular源码阅读,依赖注入的原理:injector,provider,module之间的关系。

    最开始使用angular的时候,总是觉得它的依赖注入方式非常神奇. 如果你跳槽的时候对新公司说,我曾经使用过angular,那他们肯定会问你angular的依赖注入原理是什么? 这篇博客其实是angu ...

  2. (转载)The One Sign You Will Be Rich-(by Brian de Haaff Founder and CEO Aha! -- world's #1 product roadmap software)

    When I was studying Philosophy at Berkeley, a friend told me that she could tell who was going to be ...

  3. mariadb用户和权限管理

    mysql -u root -p insert into mysql.user(Host,User,Password) values('localhost','guest',password('123 ...

  4. [poj2247] Humble Numbers (DP水题)

    DP 水题 Description A number whose only prime factors are 2,3,5 or 7 is called a humble number. The se ...

  5. 手机响应式js轮播基础

    onmousedown --->ontuchstart onmousemove --->ontouchmove onmouseup --->ontouchend ontuchstar ...

  6. IE8兼容性经验小结

    DOCTYPE 首先需要确保HTML页面开始部分要有DOCTYPE声明.DOCTYPE告诉浏览器使用什么样的HTML或XHTML规范来解析HTML文档,具体会影响: 1.对标记,attributes, ...

  7. JQ完成表格单元格顺序的上移下调

    如有指教及疑问,欢迎留言 HTML代码 <table class="exampletable"> <thead> <tr> <th> ...

  8. Linux截屏工具scrot用法详细介绍

    Scrot是Linux命令行中使用的截图工具,能够进行全屏.选取等操作,下面小编将针对Scrot截图工具的用法给大家做个详细介绍,通过操作实例来学习Scrot的使用.   在Linux中安装Scrot ...

  9. 慕课网__HTML5 存储

    application cache 只能更新全部,不能更新单独的文件 在更新后,要重新打开浏览器,缓存才会生效, 不能实时生效

  10. 初识App安全性测试

    目前手机App测试还是以发现bug为主,主要测试流程就是服务器接口测试,客户端功能性覆盖,以及自动化配合的性能,适配,压测等,对于App安全性测试貌似没有系统全面统一的标准和流程,其实安全性bug也可 ...