【问题标题】:How can I translate this requests.post into a locust request?如何将此 requests.post 转换为 locust 请求?
【发布时间】:2020-06-16 21:12:27
【问题描述】:

我有一个用于调用 API 并返回数据的 Python 函数的模板。我想使用 locust 对该 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

我怎样才能把它翻译成蝗虫类?

【问题讨论】:

标签: python api locust


【解决方案1】:

我是这样做的:

# 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”。如果您不熟悉 requests 库,可能会让人感到困惑。 希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 2018-01-30
    • 2017-10-31
    • 2016-04-12
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 2016-11-25
    相关资源
    最近更新 更多