web测试常用python代码——发包

#coding=utf-8
'''
Created on 2012-3-5

@author: xxx
'''

import cookielib
import urllib, urllib2
import zlib
import xml.etree.ElementTree as ET

self.root = 'http://127.0.0.1/login.action'
self.username = 'xxx'
self.password = '123'

//获得cookie中的数据
self.cj = cookielib.CookieJar()
self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
urllib2.install_opener(self.opener)

//调接口发包
params = urllib.urlencode({'username':self.username, 'password':self.password})
req = urllib2.Request(self.root, params)
stri = urllib2.urlopen(req).read()
stri = zlib.decompress(srti)

//解析xml(由string转化所得)    
treeroot = ET.fromstring(stri[stri.find('<'):])

result = treeroot.getiterator('state')[0].text

你可能感兴趣的:(Python)