Python - Requests 模拟 DWR框架的请求

本文记录了使用Python的Requests库模拟DWR框架的请求过程,重点在于如何获取并处理DWRSESSION,以及解决请求过程中遇到的错误问题。
摘要由CSDN通过智能技术生成

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 = &#
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值