我有一个用于Python函数的模板,该模板调用API并返回数据。我想使用蝗虫对该API进行负载测试。
import requests
proxies = {"http" : None,
"https" : None}
verify = "/data/certs/abc123.crt"
def call_api(par1, par2, par3):
r = requests.post(url = 'https://ABCD123.XYZ.QWERTY:9010/public/api/v1/ABC_TEST/query',
json = {"par1" : par1, "par2" : par2, "par3" : par3}, verify = verify, proxies = proxies)
return r
如何将其翻译成蝗虫类?
答案 0 :(得分:0)
我这样做:
# prepare the headers to send to the remote host
headers = {"key":"value",
"key":"value"}
# prepare the data for the POST body
data = {"key":"value",
"key":"value"}```
with connection_object.client.post(url, headers=headers,
json=data, name=task_name, catch_response=True) as response:
# convert the response to JSON
msg = json.loads(response.content.decode('utf-8'))
if response.status_code == 200:
# we got a 200 OK
response.success()
else:
response.failure("failure text")
在此示例中,此代码在从UserBehavior类调用的单独函数中运行,因此,如果在类中的任务内执行此操作,则connection_object为“ self”。
在您的示例中,更改
r = requests.post(url = 'https://ABCD123.XYZ.QWERTY:9010/public/api/v1/ABC_TEST/query',
json = {"par1" : par1, "par2" : par2, "par3" : par3}, verify = verify, proxies = proxies)
到
class UserBehavior(SequentialTaskSet):
@task()
def task1(self):
r = self.client.post('https://ABCD123.XYZ.QWERTY:9010/public/api/v1/ABC_TEST/query',
json = {"par1" : par1, "par2" : par2, "par3" : par3},
verify = verify, proxies = proxies)
我确定您已经看到this,其中显示“ get”,但不显示“ post”。如果您不熟悉请求库,则可能会造成混淆。 希望有帮助!