Python - Requests 模拟 DWR框架的请求
Max.Bai
2016-07-19
记录一次压测DWR系统的请求脚本。
难点获得DWRSESSION的值,并且解决请求页面报错问题
1. 获得DWRSESSION
2. 打开关键请求参数里面的page对应的页面一次
3. 模拟关键请求
样例脚本1:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import re
import random
import sys
import time
poitems = [['26621742','POPOTEST10002','POTEST10002','ITEMTEST15041','%E6%B5%8B%E8%AF%95%E5%95%86%E5%93%8115041'],
['26621743','POPOTEST10002','POTEST10002','ITEMTEST16137','%E6%B5%8B%E8%AF%95%E5%95%86%E5%93%8116137'],
['26621744','POPOTEST10002','POTEST10002','ITEMTEST19983','%E6%B5%8B%E8%AF%95%E5%95%86%E5%93%8119983'],
['26621745','POPOTEST10002','POTEST10002','ITEMTEST10941','%E6%B5%8B%E8%AF%95%E5%95%86%E5%93%8110941'],
['26621746','POPOTEST10002','POTEST10002','ITEMTEST17630','%E6%B5%8B%E8%AF%95%E5%95%86%E5%93%8117630'],
['26621747','POPOTEST10002','POTEST10002','ITEMTEST14313','%E6%B5%8B%E8%AF%95%E5%95%86%E5%93%8114313'],
['26621748','POPOTEST10002','POTEST10002','ITEMTEST17901','%E6%B5%8B%E8%AF%95%E5%95%86%E5%93%8117901'],
['26621749','POPOTEST10002','POTEST10002','ITEMTEST13596','%E6%B5%8B%E8%AF%95%E5%95%86%E5%93%8113596'],
['26621750','POPOTEST10002','POTEST10002','ITEMTEST14834','%E6%B5%8B%E8%AF%95%E5%95%86%E5%93%8114834'],
['26621751','POPOTEST10002','POTEST10002','ITEMTEST17821','%E6%B5%8B%E8%AF%95%E5%95%86%E5%93%8117821']]
class Transaction(object):
def __init__(self):
self.custom_timers={} # restart timer
self.ssrequest = requests.session()
################## login ##################
# get user
url = 'http://192.168.149.84:5000/getuser'
r = requests.get(url)
res = eval(r.text)
username = res[0]
password = res[1]
self.box = res[2]
# print username, password, self.box
url = "http://test2.max_blog.com:8080/cas/login?service=http%3A%2F%2Ftest2.max_blog.com%3A8080%2Fmax_blog_bas%2Flogout.jsp"
headers = {
'content-type': 'application/x-www-form-urlencoded',
'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36'
}
r = self.ssrequest.get(url, headers=headers)
#get lt value
ltvalue = ""
me = re.search(r'name=\"lt\" value=\"(\S{76})\"', r.text)
if me:
# print me.group(0), '\n',me.group(1)
ltvalue = me.group(1)
else:
print 'not match ltvalue'
jsid = self.ssrequest.cookies["JSESSIONID"]
url = "http://test2.max_blog.com:8080/cas/login;jsessionid={jsid}?service=http%3A%2F%2Ftest2.max_blog.com%3A8080%2Fmax_blog_bas%2Flogout.jsp".format(jsid=jsid)
private = {
'username': username,
'password': password,
'roleID':'2c9081551b95c187011b95cb29090001',
'warehouseID': '4',
'lt':ltvalue,
'_eventId':'submit'
}
r = self.ssrequest.post(url, data=private, headers=headers)
# get dwrsession
url2 = &#