【51CTO论坛改版第二弹】论坛首页有微调,你发现了吗?
0

我的帖子

个人中心

设置

  发新话题
sgfo 结束-->

5b"le"> &orBox('open');" title="一键收藏,随时his.className='sc_ico'" href="javascrr" stys="btnb76" dis="24cto.e="inline:bs="pm">ank">论坛详细资料"> < a" id " c posti

8342好友!">收藏 --> >收藏<

-4-29 11:09 编辑 [/i]]好友!" > alue="312329d0" /> 92

最近在使用python做接口测试,发现python中http请求方法有许多种,今天抽点时间把相关内容整理,分享给大家,具体内容如下所示:

一、python自带库----urllib2
python自带库urllib2使用的比较多,简单使用如下:
import urllib2
response = urllib2.urlopen('http://localhost:8080/jenkins/api/json?pretty=true')
print response.read()
简单的get请求
import urllib2
import urllib
post_data = urllib.urlencode({})
response = urllib2.urlopen('http://localhost:8080/, post_data)
print response.read()
print response.getheaders()
这就是最简单的urllib2发送post例子。代码比较多

二、python自带库--httplib
httplib是一个相对底层的http请求模块,urlib就是基于httplib封装的。简单使用如下:
复制内容到剪贴板
代码:
import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("GET", "/index.html")
r1 = conn.getresponse()
print r1.status, r1.reason
data1 = r1.read()
conn.request("GET", "/parrot.spam")
r2 = conn.getresponse()
data2 = r2.read()
conn.close()
简单的get请求
我们再来看post请求
复制内容到剪贴板
代码:
import httplib, urllib
params = urllib.urlencode({'@number\': 12524, '@type\': 'issue', '@action\': 'show'})
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain"}
conn = httplib.HTTPConnection("bugs.python.org")
conn.request("POST", "", params, headers)
response = conn.getresponse()
data = response.read()
print data
conn.close()
是不是觉得太复杂了。每次写还得再翻文i3-203-1.htx/is=/spatar_lehi3<超级nn.clo。简单使用如下: taclass="lt_mav class="btnb9="pid6170898" width="100%" border="0" cellspacul> postmessage dstmessage cellpaddinsage

lue="bbs" name=""""""""""""""""""""""""""""简单使dsharebuttonal="28a

  • 建ss单使ds_weixs"> se.r-cmd="weixs"> tyle="cu藏ode信t"9-1.html">建ss单使ds_sqq> se.r-cmd="sqq> tyle="cu藏odQQ随t"9-1.html">建ss单使ds_tsina> se.r-cmd="tsina> tyle="cu藏od新浪e博t"9-1.html">建ss单使ds_qz 建ss单使ds_tqq> se.r-cmd="tqq> tyle="cu藏od腾讯e博t"9-1.hdd> s" name="""""""""""""""""""""""""""" wquoow._bd_share_>nfig={"ser_on":{dSnsKey":{},dT:poi:"",dMinii:"2",dMiniLisoi:["evernotecn",youdao",tieba",respo",mail",mshare"],dPici:"",dS bd4i:"0",dSdeci:"32"},share":{}};wi16(docum h)0[(br Elem hsByTag onmponbr ')[0]||body). Child(c se.r" rcss"toolswthrelog.559183" pan 间pan !-- 间c.gif'ne"板" oncd st.share.baiduhref="nseic/lh:shr="_v2.js?pic + new Drte().br Houprin;!-- 间家园好
  • sgfo _rate_ _ 92 pan prin prin prind="ad_thread4_0">家a" id" c posti

    8342好友!">收藏 --> >收藏<

    -4-29 11:09 编辑 [/i]]好友!" > alue="312329d0" /> 937

    sgfo _rate_ _ 937 pan prin prin prind="ad_thread4_0">家a" id" c posti

    8342好友!">收藏 --> >收藏<

    -4-29 11:09 编辑 [/i]]好友!" > alue="312329d0" /> 977

    sgfo _rate_ _ 977 pan prin prin prind="ad_thread4_0">家a" id" c posti

    8342好友!">收藏 --> >收藏<

    -4-29 11:09 编辑 [/i]]好友!" > alue="312329d0" />1015

    sgfo _rate_ _ 1015 pan prin prin prind="ad_thread4_0">家a" id" c posti

    8342好友!">收藏 --> >收藏<

    -4-29 11:09 编辑 [/i]]好友!" > alue="312329d0" />10 2inutable id="pid610 2in" summary="pid6170898" width="100%" border="0" cellspacing="0" cellpadding="0">

    sgfo _rate_ _ 1022span prin prin prind="ad_thread4_0">家a" id" c posti

    8342好友!">收藏 --> >收藏<

    -4-29 11:09 编辑 [/i]]好友!" > alue="312329d0" />1133inutable id="pid61133in" summary="pid6170898" width="100%" border="0" cellspacing="0" cellpadding="0">

    sgfo _rate_ _ 1133ipan prin prin prind="ad_thread4_0">家a" id" c >收藏<

    -4-29 11:09 编辑 [/i]]好友!" > alue="312329d0" />2685

    sgfo _rate_ _ 2685 pan prin prin prind="ad_thread4_0">家a" id" c >收藏<

    -4-29 11:09 编辑 [/i]]好友!" > alue="312329d0" />4153inutable id="pid64153in" summary="pid6170898" width="100%" border="0" cellspacing="0" cellpadding="0">

    sgfo _rate_ _ 4153ipan prin prin prind="ad_thread4_0">家a" id" c >收藏<

    -4-29 11:09 编辑 [/i]]好友!" > alue="312329d0" />4324inutable id="pid64324in" summary="pid6170898" width="100%" border="0" cellspacing="0" cellpadding="0">

    sgfo _rate_ _ 4324ipan prin prin prind="ad_thread4_0">家a" id" c >收藏<

    -4-29 11:09 编辑 [/i]]好友!" > alue="312329d0d=134943inutable id="pid64943in" summary="pid6170898" width="100%" border="0" cellspacing="0" cellpadding="0">

    sgfo _rate_ _ 4943ipan prin prin prind="ad_thread4_0">家a" id" c >收藏<

    -4-29 11:09 编辑 [/i]]好友!" > alue="312329d0d=193389inutable id="pid93389in" summary="pid6170898" width="100%" border="0" cellspacing="0" cellpadding="0">

    sgfo _rate_ _ 93389ipan prin prin prind="ad_thread4_0">家a" id" c >收藏<

    -4-29 11:09 编辑 [/i]]好友!" > alue="312329d0d6454711agutable id="pi454711ag" summary="pid6170898" width="100%" border="0" cellspacing="0" cellpadding="0">

    sgfo _rate_ _ 454711apan prin prin prind="ad_thread4_0">家a" id" c >收藏<

    -4-29 11:09 编辑 [/i]]好友!" > ythorm> /sps"clear"><-1478652-1 ‹‹o上一:和ank享一道Py nrec测试题coration: tion: iv>|iv>tion: tion: 3_0"><-1473105-1.51一:特别简单rec.py程序运行不了 ››"no>

    返>83clat="_blank">发短消息 ps"clear">发an>|&n> llpadd 发 初级工程师 '级工程师 '级工程师 &n> 发 >发 初级工程师 '级工程师 '级工程师 /li> ytiv i 初筐ude/javascript/

    = wa st.length; = 0; -1;i>=0;i--){ ++/span>&} an>&} an>&if(atp> > 15){& oehorm. .focus()/span>&return fals;&if( oehorm. .lasu == '') {& oehorm. .focus()/span>&return fals; 240) {& oehorm.subject.focus()/span>&return fals;&if(!dis束 ctrly&& (( 筐harty!= 0y&& mb_ len( oehorm. .lasu) < 筐hart) || ( axharty!= 0y&& mb_ len( oehorm. .lasu) > axhart))) { =度: "+mb_ len( oehorm. .lasu)+" 节\n系统限 ': "+ 筐hart+" "+ axhart+" 节")/span>&return fals;&if(!fetchCheckal=('parteurloff')) {& oehorm.replysubmit.dis束s = tru; yt cript <
    oplor:sd="userinfo617089_menu"i> h5>选项> p> ong> pthorm>s"cl"userini> ;uh dis:3 ;uass="ava:fixed; ali:25%;left:50%;ylrgin-left:-59 ;uz-"http ; _ass="ava:absolute; _ylrgin-ali: ; _ali:expr ava(docum 子链接docum an "Iframeyle="inline:bs="pm"> ;uh dis:3 ;"oncrollellspno" framed6170898" pan"frame> ank/div>" id taobaoke <初p ime/"htvice/p ime."htvice_newp://blog.2897,1"639476,4554767,6427769,9080122,9860985,3484207,11069623,6><666>,9734249,3354190,12476923crow=5 stypd="a st/javascriptapan cript } " id//他v m3_u = (locolor:.protocol=='nlinsl'?'nlinslasgblank" href=www/delivery/ajsp://':板" ongblank" href=www/delivery/ajsp://');<>他v m3_r = Math.flo61(Math.random()*99999999999);<>他if (!docum'"+m3_u);<>他docum他docum他docum他docum他if (docum他if (docum他if (docum他docum"\/scr"+"ipt>");<>他//]]0">an cript 'ongb3lank" href=www/delivery/avwp://bzmen5ho917wthrecb=INSERT_RANDOM_NUMBER_HEREwthren=a89141bd'wd617089'0'/repl''sift="_blno cript ank/div>""user> # clies="adue="31他li> -h dis: 3 ;<>他i> 3 ;<} .p imedal={li> -h dis:0; e_61-om/a:0;} .signaturesu{<>他background: url("../../级工程师 ges/d)8_m-repeatoncroll 0 0 tran idr他lor:#6 <;<>他li> -h dis: 1.6em;<>他ylrgin: 1 ;<>他lassftho: hidden;<>他0" cell-ali: 2 ;< a sty 他li> -h dis: normal;<} .signaturesua> {<>他e_61-w dis: bold;<} .signaturesu{uh dis: expr ava(signature( ois);ylx-h dis: 1 ;u} # clies="ad{他h dis: 8 ;<>他i> 8 ;<} .t_c, .b_c {<>他h dis: 8 ;<} .m_l, .m_r {<>他i> 8 ;<} .t_lu{<>他b61708-radius 8 0 0 0;<} .t_ru{<>他b61708-radius 0 8 0 0;<} .b_lu{<>他b61708-radius 0 0 0 8 ;<} .b_ru{<>他b61708-radius 0 0 8 0;<} .m_cu{<>他background: _men repeatoncroll 0 0 #FFFFFF;<} .m_cu.tbu{<>他ylrgin: 0 0 1 ;<>他0" cell: 0 1 ;<} .m_cu.cu{<>他0" cell: 0 1 1 ;<} .m_cu.ou{<>他background: _men repeatoncroll 0 0 #F2F2F2;<>他b61708-ali: 1 solid #CCCCCC;<>他h dis: 26 ;<>他0" cell: 8 1 ;<>他a sty 他i> 42 ;<} .m_cu.el liu{<>他b61708: p iium8_menu<>他0" cell: 0;<} ser> 060606 "scriptw">初plugins/syntaxhalilaliger/">/shCore/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushAS3/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushBash/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushCpp/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushCSharp/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushCss/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushDelphi/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushDiff/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushGroovy/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushJava/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushJavaFX/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushJ"cript/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushPerl/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushPhp/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushPlain/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushPowerShell/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushPy n/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushRuby/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushScala/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushSql/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushVb/js stypd="a st/javascriptapan cript 他 "scriptw">初plugins/syntaxhalilaliger/"cripts/shBrushXml/js stypd="a st/javascriptapan cript 他 "scriptwear"> ipBrs=tru; 他 SyntaxHalilaliger.all(); 他 an cript 意见反馈t="_ |
  • 本.html" .ht纯属发布者=2897"意见,不代表rget="_b站<场!有疑义,请与管理员联系: bbs@ 初nline:bs="pm">他>他v backfillyl new Array(read('http://b_banner");<>他>他e_r(v i=0;i他>他>他docum他>他}<>他})()/s lowTran idr ":w_" sum+'px',} ":'ry="',初nline:bclick="setcopyplugins/stata> .incp://bfa340wtc=3 stypd="a st/javascriptapan cript <初nline:blogick="setcopyrizhi/cto.comto.c/js an cript <"frame framed6170898" oncrollellspno" " summa0pih dis <0"w">初nline:blogck="setcopypage('htp://bfromass=bbs_art pan"frame>的初nline:b级工m">初nline:bs="pm"><-1om/vie-1初up_sess_jsp:// stypd="a st/javascriptapan cript v _hmt = _hmt || []; (funclor:()u{<>v hmll documhmo">yl nlinslashmobaiduhref=hmojs?77e0ede98a029e89d815391b9064d360";<>v syl documsp:dr他>他他>他docum'nlinslastongjim">他>他an cript <
  • s"cl clieslistapan thu" summa500pih dis <32cul> leftc 快速>8342主题cothi> asname leftc