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. 自定义一个可复用的BaseAdapter

    1.我们一点点开始改: 首先我们自定义BaseAdapter,等下我们就要对他进行升级改造 /** * Created by Jay on 2015/9/21 0021. */ public clas ...

  2. 。net 文件依赖缓存

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  3. easyui from 缓存问题处理

    1 这是ie低版本,缓存了easyui form load事件获取的服务器端数据,给ajax时间加上清除缓存就ok. 找到easyui 中的form load事件  添加cache:false, /* ...

  4. iis发布后,未能找到编译器可执行文件 csc.exe

    iis 未能找到编译器可执行文件 csc.exe在一台新安装完的Windows Server 2003上,打上Framework 3.5,配置好WebService的IIS,结果浏览时出现:未找到编译 ...

  5. EntityFramework Core 学习笔记 —— 添加主键约束

    原文地址:https://docs.efproject.net/en/latest/modeling/keys.html Keys (primary) Key 是每个实体例的主要唯一标识.EF Cor ...

  6. 今天谈谈流,什么是IO流?

    无标题 (5) :first-child { margin-top: 0; } blockquote > :last-child { margin-bottom: 0; } img { bord ...

  7. Windows使用shipyard

    步骤: 1. 安装go语言环境,配置go语言环境变量 如果是64位,GOARCH=amd64 http://my.oschina.net/pandao/blog/161667 2. 找一台linux或 ...

  8. Python3基础 访问列表 大于等于指定索引值的所有元素

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  9. Arduino入门笔记【1】

    刚刚接触这个东西只知道这是类似于单片机的开发板,可以做一些单片机实现或者不能实现的东西,但是比单片机要简单得多. Arduino到底是什么? 维基百科上的描述是:Arduino是一块开发板的微控制器和 ...

  10. vnc连接kali 2.0 报错:A problem has occurred and the system can't recover.

    kali版本: root@kali:~# uname -a Linux kali -kali1-amd64 # SMP Debian -7kali2 (--) x86_64 GNU/Linux 第一步 ...