2019 十一月 13 , 星期三
Home / 实用工具 / 压侧工具 ngrinder 学习(发送POST JSON请求)

压侧工具 ngrinder 学习(发送POST JSON请求)

# -*- coding:utf-8 -*-

# A simple example using the HTTP plugin that shows the retrieval of a
# single page via HTTP. 
#
# This script is automatically generated by ngrinder.
#
# @author admin
from net.grinder.script.Grinder import grinder
from net.grinder.script import Test
from net.grinder.plugin.http import HTTPRequest
from net.grinder.plugin.http import HTTPPluginControl
from HTTPClient import NVPair
#导入对JSON格式返回值的处理函数
from org.json import JSONObject
import sys
reload(sys)
sys.setdefaultencoding('utf-8') 

control = HTTPPluginControl.getConnectionDefaults()
# if you don't want that HTTPRequest follows the redirection, please modify the following option 0.
# control.followRedirects = 1
# if you want to increase the timeout, please modify the following option.
control.timeout = 6000

test1 = Test(1, "api.vd.cn")
request1 = HTTPRequest()

# Make any method call on request1 increase TPS
test1.record(request1)

class TestRunner:
	# initlialize a thread 
	def __init__(self):
		grinder.statistics.delayReports=True
		pass

	# test method		
	def __call__(self):
		headers = [
			NVPair("Content-Type","application/json")
		]
		json_data='{"is_card_photo":"1","wid":"200168668","BaseAppType":"android","BaseAppVersion":"4.4.0","SystemVersion":"4.4.2","shop_id":"11433415","_sign_":"DDFDDA37A35F2CF28591FC6D18245779","_token_":"53c571f37a7e2fe64cc4d24200fdd8ca","_wid_":"200168668","appIdentifier":"com.hs.yjseller"}'
                #submitdata = [NVPair("a","1"),NVPair("b","2"),NVPair("c","3")]
		result = request1.POST("http://your ip/user/getuseraddressall", json_data, headers)
		# You get the message body using the getText() method.
		# if result.getText().find("HELLO WORLD") != -1 :
		#    grinder.statistics.forLastTest.success = 1
		# else :
		#	 grinder.statistics.forLastTest.success = 0
			
		# if you want to print out log.. Don't use print keyword. Instead, use following.
		#grinder.logger.info(result.getText())
		
		if result.getStatusCode() == 200 :
			grinder.statistics.forLastTest.success = 1
		elif result.getStatusCode() in (301, 302) :
			grinder.logger.warn("Warning. The response may not be correct. The response code was %d." %  result.getStatusCode()) 
			grinder.statistics.forLastTest.success = 1
		else :
			grinder.statistics.forLastTest.success = 1

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>