Python - Requests 模拟 DWR框架的请求

来源:互联网 发布:网站源码小偷破解版 编辑:程序博客网 时间:2024/05/18 20:12

Python - Requests 模拟 DWR框架的请求

Max.Bai

2016-07-19


记录一次压测DWR系统的请求脚本。

难点获得DWRSESSION的值,并且解决请求页面报错问题

1. 获得DWRSESSION
2. 打开关键请求参数里面的page对应的页面一次
3. 模拟关键请求

样例脚本1:
# !/usr/bin/env python# -*- coding: utf-8 -*-import requestsimport reimport randomimport sysimport timepoitems = [['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 = "http://test2.max_blog.com:8080/max_blog_bas/dwr/call/plaincall/__System.generateId.dwr"        headers = {            'content-type': 'text/plain',            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36'            }        private = {            'callCount':'1',            'c0-scriptName':'__System',            'c0-methodName':'generateId',            'c0-id':'0',            'batchId':'0',            'instanceId':'0',            'page':'%2Fmax_blog_bas%2Findex.jsp',            'scriptSessionId':'',            'windowName':''        }        r = self.ssrequest.post(url2, data=private, headers=headers)        dwrsession = ""        me = re.search(r'.*\"(\S{27})\"', r.text)        if me:            # print me.group(0), '\n',me.group(1)            dwrsession = me.group(1)        else:            print 'not match dwrsession'                self.scriptSessionId = dwrsession+"/"+"aGWX1kl-b3J8E53ll"        # print self.scriptSessionId        # DWRSESSIONID=Z4hUoMEQPIOedQ*cvnf2mPcv$jl        requests.utils.add_dict_to_cookiejar(self.ssrequest.cookies, {'DWRSESSIONID':dwrsession})                # get dwrsession 2        url3 = "http://test2.max_blog.com:8080/max_blog_bas/dwr/call/plaincall/__System.pageLoaded.dwr"        headers = {            'content-type': 'text/plain',            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36'            }        private = {            'callCount':'1',            'windowName':'',            'c0-scriptName':'__System',            'c0-methodName':'pageLoaded',            'c0-id':'0',            'batchId':'1',            'instanceId':'0',            'page':'%2Fmax_blog_bas%2Findex.jsp',            'scriptSessionId': self.scriptSessionId        }        r = self.ssrequest.post(url3, data=private, headers=headers)                #### open iqc page        # iqc jsp        # /max_blog_inb/pages/inb/iqcReceivingScan.jsp?random=0.9528758095111698&tag=9bc2afd41913ce66f7c9dbcfacf21dc1        url3 = "http://test2.max_blog.com:8080/max_blog_inb/pages/inb/iqcReceivingScan.jsp?random=0.9528758095111698&tag=9bc2afd41913ce66f7c9dbcfacf21dc1"        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(url3, headers=headers)        def run(self):        # get po item        res = random.sample(poitems, 1)        recp_id = res[0][0]        po_code = res[0][1]        recp_no = res[0][2]        it_code = res[0][3]        it_name = res[0][4]        # print recp_id, po_code, recp_no, it_code, it_name                    # ssrequest = requests.session()                        ##########################   Bussniess    ##########################        res = ''        try:                                #######add scan result            headers = {                'content-type': 'text/plain',                'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36'                }                            url3 = "http://test2.max_blog.com:8080/max_blog_inb/dwr/call/plaincall/IqcReceivingScanService.addScanResult.dwr"            private = {                'callCount':'1',                'windowName':'c0-e1',                'c0-scriptName':'IqcReceivingScanService',                'c0-methodName':'addScanResult',                'c0-id':'0',                'c0-e2':'number:0',                'c0-e3':'string:%s' % it_code,                'c0-e4':'string:%s' % it_name,                'c0-e5':'string:',                'c0-e6':'string:%s' % self.box,                'c0-e7':'number:2',                'c0-e8':'number:1',                'c0-e9':'string:1',                'c0-e10':'string:EA',                'c0-e11':'string:%s' % po_code,                'c0-e12':'string:receiptContainer',                'c0-e13':'number:%s' % recp_id,                'c0-e14':'string:%s' % recp_no,                'c0-e15':'string:null',                'c0-e16':'string:ml',                'c0-e17':'string:TEST_COMPANY_1',                'c0-e18':'string:1900-01-01',                'c0-e19':'string:1900-01-01',                'c0-e1':'Object_Object:{iqcScanVoId:reference:c0-e2, itemCode:reference:c0-e3, itemDesc:reference:c0-e4, purchaseCaseNo:reference:c0-e5, containerCode:reference:c0-e6, scanQty:reference:c0-e7, perScanQty:reference:c0-e8, newPrice:reference:c0-e9, scanUom:reference:c0-e10, poNo:reference:c0-e11, flag:reference:c0-e12, receiptDetailId:reference:c0-e13, receiptOrderNo:reference:c0-e14, newItemSize:reference:c0-e15, itemSize:reference:c0-e16, companyCode:reference:c0-e17, manufacturedDate:reference:c0-e18, expirationDate:reference:c0-e19}',                'c0-param0':'array:[reference:c0-e1]',                'c0-param1':'boolean:true',                'c0-param2':'string:normal',                'batchId':'19',                'instanceId':'0',                'page':'%2Fmax_blog_inb%2Fpages%2Finb%2FiqcReceivingScan.jsp%3Frandom%3D0.9492080826312304%26tag%3Db22c0d8798dfdbdae2048ab76d64b292',                'scriptSessionId': self.scriptSessionId            }            start = time.time()            r = self.ssrequest.post(url3, data=private, headers=headers)            response_time = time.time()            self.custom_timers['response sent'] = response_time - start            time.sleep(0.01)            # print r.text        except Exception as e:            print str(e)            print str(sys.exc_info())            raise Exception('Error: {0}--{1}--{2}'.format(res.replace('\n',''), str(e), str(sys.exc_info())))if __name__ == '__main__':    trans = Transaction()    trans.run()        # set a timer to calculate the interface response time.    for timer in ('response sent',):        print '%s: %.5f secs'%(timer, trans.custom_timers[timer])     




样例脚本2:
# -*- coding: utf-8 -*-import requestsimport reimport random, time, sysdef tokenify(number):    tokenbuf = []    charmap = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ*$"    remainder = number    while remainder > 0:        tokenbuf.append(charmap[remainder & 0x3F])        remainder = remainder // 64    return ''.join(tokenbuf)domain = "http://test2.maxblog.com:8080"class Transaction(object):    def __init__(self):        self.custom_timers={}   # restart timer        self.ssrequest = requests.session()                # get user        url = 'http://192.168.149.84:5000/getuser'        r = requests.get(url)        res = eval(r.text)        self.username = res[0]        self.password = res[1]        ##########################   Lgoin    ##########################        # get dwr session        url2 = domain + "/max_blog_rf/dwr/call/plaincall/__System.generateId.dwr"        headers = {            'content-type': 'text/plain',            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36'            }        private = {            'callCount':'1',            'c0-scriptName':'__System',            'c0-methodName':'generateId',            'c0-id':'0',            'batchId':'0',            'instanceId':'0',            'page':'%2Fmax_blog_rf%2Flogin.jsp',            'scriptSessionId':'',            'windowName':''        }        r = self.ssrequest.post(url2, data=private, headers=headers)        # print r        dwrsession = ""        me = re.search(r'.*\"(\S{27})\"', r.text)        if me:            dwrsession = me.group(1)        else:            print 'not match dwrsession'        # scriptSessionId = dwrsession+"/O1tRakl-dPKysDfm5"        t = long(time.time()*1000)        r = random.randint(1000000000000000, 9999999999999999)        self.scriptSessionId = dwrsession+"/"+tokenify(t)+"-"+tokenify(r)        requests.utils.add_dict_to_cookiejar(self.ssrequest.cookies, {'DWRSESSIONID':dwrsession})        # login        url = domain + "/max_blog_rf/dwr/call/plaincall/RfLoginService.login.dwr"        headers = {            'content-type': 'text/plain',            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36'            }        private = {            'callCount':'1',            'windowName':'',            'c0-scriptName':'RfLoginService',            'c0-methodName':'login',            'c0-id':'0',            'c0-param0':'string:4',            'c0-param1':'string:%s' % self.username,            'c0-param2':'string:%s' % self.password,            'c0-param3':'boolean:false',            'batchId':'1',            'instanceId':'0',            'page':'%2Fmax_blog_rf%2Flogin.jsp',            'scriptSessionId': self.scriptSessionId        }        r = self.ssrequest.post(url, data=private, headers=headers)        # print r        # print r.text        # RF_SESSION_TAG=4%2Ctest100        requests.utils.add_dict_to_cookiejar(self.ssrequest.cookies, {'RF_SESSION_TAG':'4%2C{user}'.format(user=self.username)})        url = domain + "/max_blog_rf/pages/rf/pick/batchPicking.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)        # print r        t = long(time.time()*1000)        r = random.randint(1000000000000000, 9999999999999999)        self.scriptSessionId = dwrsession+"/"+tokenify(t)+"-"+tokenify(r)        # /max_blog_rf/dwr/call/plaincall/__System.pageLoaded.dwr        url3 = domain + "/max_blog_rf/dwr/call/plaincall/__System.pageLoaded.dwr"        headers = {            'content-type': 'text/plain',            'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36'            }        private = {            'callCount':'1',            'windowName':'',            'c0-scriptName':'__System',            'c0-methodName':'pageLoaded',            'c0-id':'0',            'batchId':'1',            'instanceId':'0',            'page':'%2Fmax_blog_rf%2Fpages%2Frf%2Fpick%2FbatchPicking.jsp',            'scriptSessionId': self.scriptSessionId        }        r = self.ssrequest.post(url3, data=private, headers=headers)        # print r    def run(self):        # get box        url = 'http://192.168.149.84:5000/getorder'        r = requests.get(url)        box = 'JH'+r.text                # get pc info   ['16060100005096','ITEMTEST90023','TT01010102']        url = 'http://192.168.149.84:5000/getupc/{user}'.format(user=self.username)        r = requests.get(url)        res = eval(r.text)        pc_no = res[0]        it_code = res[1]        gd_loc = res[2]                try:            ##########################   Bussniess    ##########################            url = domain + "/max_blog_rf/dwr/call/plaincall/BatchPickingService.txRequiredPick.dwr"            headers = {                'Content-Type': 'text/plain',                'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.125 Safari/537.36'                }            private = {                'callCount':'1',                'windowName':'',                'c0-scriptName':'BatchPickingService',                'c0-methodName':'txRequiredPick',                'c0-id':'0',                'c0-param0':'string:%s' % pc_no,                'c0-param1':'string:%s' % gd_loc,                'c0-param2':'string:%s' % box,                'c0-param3':'string:%s' % it_code,                'c0-param4':'number:1',                'c0-param5':'number:5',                'batchId':'3',                'instanceId':'0',                'page':'%2Fmax_blog_rf%2Fpages%2Frf%2Fpick%2FbatchPicking.jsp',                'scriptSessionId': self.scriptSessionId            }            start = time.time()            r = self.ssrequest.post(url, data=private, headers=headers)            response_time = time.time()            self.custom_timers['response sent'] = response_time - start            assert ('status:true' in r.text), 'Failed pick item:%s' % r.text.replace('\n','')            time.sleep(0.02)            # print r            # print r.text        except Exception as e:            # print str(e)            # print str(sys.exc_info())            raise Exception('Error: {0}--{1}'.format(str(e), str(sys.exc_info())))if __name__ == '__main__':    trans = Transaction()    trans.run()        # set a timer to calculate the interface response time.    for timer in ('response sent',):        print '%s: %.5f secs'%(timer, trans.custom_timers[timer])  



0 1
原创粉丝点击