JSR223 Sampler added in Jmeter, import python script, realize variable transfer between jmeter and python. Finally, the Chinese characters passed in the script are displayed as garbled characters in the Debug Sampler. How to solve it?

The python script in JSR223 Sample added in Jmeter is as follows:

import hashlib

def get_app_sign(data_dict, secret):

params_list = sorted(data_dict.items(), key=lambda e: e[0], reverse=False)

params_str = "".join("{}{}".format(k, v) for k, v in params_list) + 's' + secret

s = hashlib.sha1()

s.update(params_str.encode())

return s.hexdigest().upper()

 

sid = "${sessionid}" #1. Call variables in jmeter

data_dict = {

"timestamp": 123,

"device": "app",

"session_id": sid #Because the sid in 1 = "${sessionid}", you can directly pass in the variables in jmeter here.

}

secret = "y"

result = get_app_sign(data_dict, secret)

results = data_dict["session_id"]

 

vars.put('jp',result) # Lead the result to the jmeter space with the jp variable name, then we can call it directly in jmeter: ${jp}

vars.put ('jps', results)

In addition, for those with variables, it is recommended to add Debug Sampler in the result tree

View:

 

 

 

See that jp, the variable we generated through the python script, can already be called in jmeter.

In addition, if Chinese characters are imported into Python, import them at the beginning of the python script:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

It is found that the variables we exported later are garbled in the Debug Sampler. How to operate, please give instructions....

 

Solution:

[code=python]
data_dict =  {
   "timestamp": 123,
    "device": "app",
    "session_id": sid,
    "okc": "呵呵"
 }
results1 = data_dict["okc"].decode('utf-8')
 vars.put('jps1',results1)

Pass the variables you want to pass in the py script through decode('utf-8'), such as results1 = data_dict["okc"].decode('utf-8'); then vars.put('jps1',results1 ) Throw it to the Debug Sampler, you can see:

Guess you like

Origin blog.csdn.net/qq_25162431/article/details/96482462