python的强大之处在于提供了很多的标准库,这些标准库可以直接调用,本节部分,重点学习和总结在
接口测试中Python的Http请求的库的学习。
首先来看httplib,官方的解释为:本模块定义了类实现客户端的 HTTP 和 HTTPS 协议。它通常不能直接
使用 — — 模块 urllib 用于处理使用 HTTP 和 HTTPS 的 Url。使用httplib来做一个简单的对百度的请求,看
这样的一个实现过程,见实现的代码:
#!/usr/bin/env python #coding:utf-8 import httplib def getBaidu(): '''对百度发送一个GET请求''' http_client=httplib.HTTPConnection('baidu.com',80,timeout=20) http_client.request('GET','') r=http_client.getresponse() print r.status print r.read() getBaidu()
这里我们详细的来看r提供了那些方法,见输出的结果:
#!/usr/bin/env python #coding:utf-8 import httplib def getBaidu(): '''对百度发送一个GET请求''' http_client=httplib.HTTPConnection('baidu.com',80,timeout=20) http_client.request('GET','') r=http_client.getresponse() print dir(r) getBaidu()
C:\Python27\python.exe D:/git/Python/bookDay/api/http/httplipTest.py ['__doc__', '__init__', '__module__', '_check_close', '_method', '_read_chunked', '_read_status', '_safe_read', 'begin', 'chunk_left', 'chunked', 'close', 'debuglevel', 'fileno', 'fp', 'getheader', 'getheaders', 'isclosed', 'length', 'msg', 'read', 'reason', 'status', 'strict', 'version', 'will_close'] Process finished with exit code 0
使用dir(r)方法可以看到调用的方法,如我们想看到请求百度这样的一个GET请求,到底返回的status code是多少,
是否OK,响应内容是什么,headers是什么,见实现的代码和输出的内容:
#!/usr/bin/env python #coding:utf-8 import httplib def getBaidu(): '''对百度发送一个GET请求''' http_client=httplib.HTTPConnection('www.baidu.com',80,timeout=20) http_client.request('GET','/') r=http_client.getresponse() print u'状态的状态码:\n',r.status print u'是否请求Ok:\n',r.reason print u'header是多少:\n',r.getheaders() print u'Response消息结构:\n',r.msg print u'响应内容:\n',r.read() getBaidu()
见输出的结果内容:
C:\Python27\python.exe D:/git/Python/bookDay/api/http/httplipTest.py 状态的状态码: 200 是否请求Ok: OK header是多少: [('content-length', '14613'), ('set-cookie', 'BAIDUID=5BE300935709382ADC6AFA01D08E1959:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, BIDUPSID=5BE300935709382ADC6AFA01D08E1959; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com, PSTM=1481724972; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com'), ('accept-ranges', 'bytes'), ('vary', 'Accept-Encoding'), ('server', 'BWS/1.1'), ('last-modified', 'Mon, 12 Dec 2016 06:09:00 GMT'), ('connection', 'Keep-Alive'), ('x-ua-compatible', 'IE=Edge,chrome=1'), ('pragma', 'no-cache'), ('cache-control', 'no-cache'), ('date', 'Wed, 14 Dec 2016 14:16:12 GMT'), ('p3p', 'CP=" OTI DSP COR IVA OUR IND COM "'), ('content-type', 'text/html')] Response消息结构: Date: Wed, 14 Dec 2016 14:16:12 GMT Content-Type: text/html Content-Length: 14613 Last-Modified: Mon, 12 Dec 2016 06:09:00 GMT Connection: Keep-Alive Vary: Accept-Encoding Set-Cookie: BAIDUID=5BE300935709382ADC6AFA01D08E1959:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com Set-Cookie: BIDUPSID=5BE300935709382ADC6AFA01D08E1959; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com Set-Cookie: PSTM=1481724972; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com P3P: CP=" OTI DSP COR IVA OUR IND COM " Server: BWS/1.1 X-UA-Compatible: IE=Edge,chrome=1 Pragma: no-cache Cache-control: no-cache Accept-Ranges: bytes 响应内容: "content-type" content="text/html;charset=utf-8"> "X-UA-Compatible" content="IE=Edge"> "dns-prefetch" href="//s1.bdstatic.com"/> "dns-prefetch" href="//t1.baidu.com"/> "dns-prefetch" href="//t2.baidu.com"/> "dns-prefetch" href="//t3.baidu.com"/> "dns-prefetch" href="//t10.baidu.com"/> "dns-prefetch" href="//t11.baidu.com"/> "dns-prefetch" href="//t12.baidu.com"/> "dns-prefetch" href="//b1.bdstatic.com"/>百度一下,你就知道 "http://s1.bdstatic.com/r/www/cache/static/home/css/index.css" rel="stylesheet" type="text/css" /> "#0000cc">"wrapper" style="display:none;">Process finished with exit code 0"u">"//www.baidu.com/gaoji/preferences.html" οnmοusedοwn="return user_c({'fm':'set','tab':'setting','login':'0'})">搜索设置|"btop" href="/" οnmοusedοwn="return user_c({'fm':'set','tab':'index','login':'0'})">百度首页|"lb" href="https://passport.baidu.com/v2/?login&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" οnclick="return false;" οnmοusedοwn="return user_c({'fm':'set','tab':'login'})">登录"https://passport.baidu.com/v2/?reg®Type=1&tpl=mn&u=http%3A%2F%2Fwww.baidu.com%2F" οnmοusedοwn="return user_c({'fm':'set','tab':'reg'})" target="_blank" class="reg">注册"head">©2014 Baidu 使用百度前必读 京ICP证030173号
下来来看urllib2的库,看官方的解释:urllib2 定义了很多函数和类,这些函数和类能够帮助我们在复杂的情况下获取URLS内容。
复杂情况— 基本的和深入的验证, 重定向, cookies 等等.和如上一样,我们使用urllib2来实现对百度的请求,见GET请求的代码:
#!/usr/bin/env python #coding:utf-8 import urllib import urllib2 def get_baidu(): r=urllib2.urlopen('http://www.baidu.com') print u'Response code:\n',r.getcode(),r.msg print u'headers:\n',r.headers get_baidu()
见输出结果的内容:
C:\Python27\python.exe D:/git/Python/bookDay/api/http/httplipTest.py Response code: 200 OK headers: Date: Wed, 14 Dec 2016 14:52:28 GMT Content-Type: text/html; charset=utf-8 Vary: Accept-Encoding Set-Cookie: BAIDUID=55980CB92E6F9F88BF0FF2B73E8607D4:FG=1; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com Set-Cookie: BIDUPSID=55980CB92E6F9F88BF0FF2B73E8607D4; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com Set-Cookie: PSTM=1481727148; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com Set-Cookie: BDSVRTM=0; path=/ Set-Cookie: BD_HOME=0; path=/ Set-Cookie: H_PS_PSSID=1429_21422_21120_21553_21408_20927; path=/; domain=.baidu.com P3P: CP=" OTI DSP COR IVA OUR IND COM " Cache-Control: private Cxy_all: baidu+435ac15289339958b4bcff45cd341d25 Expires: Wed, 14 Dec 2016 14:51:52 GMT X-Powered-By: HPHP Server: BWS/1.1 X-UA-Compatible: IE=Edge,chrome=1 BDPAGETYPE: 1 BDQID: 0xfc9210c000017b10 BDUSERID: 0 Transfer-Encoding: chunked Proxy-Connection: Close
下面使用urllib2实现一个POST的请求过程,见实现的代码:
#!/usr/bin/env python #coding:utf-8 import urllib import urllib2 def get_baidu(): r=urllib2.urlopen('http://www.baidu.com') print u'Response code:\n',r.getcode(),r.msg print u'headers:\n',r.headers def post_cun(): params=urllib.urlencode({'cityId':'438'}) r=urllib2.urlopen('http://m.cyw.com/index.php?m=api&c=cookie&a=setcity',params) print r.getcode(),r.msg print r.read() post_cun()
见输出结果的json内容:
C:\Python27\python.exe D:/git/Python/bookDay/api/http/httplipTest.py 200 OK {"status":true,"homeUrl":"\/xian"}
转载地址:http://www.cnblogs.com/weke/articles/6181483.html