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. C# 线程通信 一

    C#多线程通信 using System; using System.Collections.Generic; using System.Linq; using System.Text; using ...

  2. wcscpy_s与wcsncpy

    今天运行以下代码时一直出错 void Foo(const wchar_t* lpch, int len) { ... wchar_t *str = ]; wcscpy_s(str, len, lpch ...

  3. mysql 实现 start with

    自己写service----> 传入map(idsql,rssql,prior)   idsql 查询id   rssql 查询结果集    调用 以下方法 @param ids 要查询的起始 ...

  4. HDU 1272 小希的迷宫 并查集

    小希的迷宫 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  5. (转)解决Mac OS X上PhpStorm不能输入中文

    看到Netbeans上类似问题的解决办法: /Applications/netbeans/NetBeans 6.7.1/Content/Resource/netbeans/etc/netbeans.c ...

  6. linux和android博客链接

    1.Tracy Mcgrady的专栏冰山一角:linux和Android底层开发,主要是mtk系列点击打开链接 2.郁闷Wednesday:嵌入式linux 单片机 android,点击打开链接 3. ...

  7. getFragmentManager()和getSupportFragmentManager()

    在Android开发中,少不了Fragment的运用.目前在实际运用中,有v-4包下支持的Fragment以及app包下的Fragment,这两个包下的FragmentManager获取方式有点区别, ...

  8. 组合suan

    /// 求从数组a[1..n]中任选m个元素的所有组合. /// a[1..n]表示候选集,n为候选集大小,n>=m>0. /// b[1..M]用来存储当前组合中的元素(这里存储的是元素 ...

  9. Codeforces 722C. Destroying Array

    C. Destroying Array time limit per test 1 second memory limit per test 256 megabytes input standard ...

  10. CSUOJ_1002

    /* * Title : A+B(III) * Data : 2016/11/09 * Author : Andrew */ #include <iostream> #include &l ...