首页 > 解决方案 > 在 Locust 上使用 OAuth 的 MaxRetryError

问题描述

我的蝗虫文件如下

from locust import HttpUser, TaskSet, task
import urllib.parse
from locust.env import Environment
import json

class RecommenderTasks(TaskSet):
    def on_start(self):
        self.login()
        
    def login(self):
        cold_start_url = "https://my-url.com"
        url=urllib.parse.urljoin(cold_start_url, "/oauth/token")
        payload_headers = {'grant_type': 'client_credentials',
                            'Authorization': 'Basic c2ItZGV2IXQ1Nzc6TU01U0poMUtGZ3lnR1c4dXJPcUdzREhrMTVNPQ=='}
        response = self.client.post(url, payload_headers)
        
        print(response.text)
        access_token = json.loads(response.text.encode('utf8'))['access_token']
        return access_token

    @task
    def test_recommender_multiple_platforms(self):
        self.client.get("/recommendations")

class RecommenderUser(HttpUser):
    tasks = [RecommenderTasks]
    min_wait = 1000
    max_wait = 2000

    host = "https://my-url.com"

if __name__ == '__main__':
    env = Environment(user_classes=[RecommenderUser])
    user = RecommenderUser(env)
    user.run()

我正在尝试使用我的 URL 进行 OAuth 并收到如下错误

MaxRetryError("HTTPSConnectionPool(host='my-url.com', port=443): url: /oauth/token 超过最大重试次数(由 NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x0000019122582C08>: Failed to建立新连接:[Errno 11001] getaddrinfo failed'))")

调用 POST 方法时我在这里做错了吗?

标签: pythonoauth-2.0oauthload-testinglocust

解决方案


它还没有到它尝试并未能 POST 登录的地步。它找不到您要攻击的主机。确保你可以从你试图运行 Locust 的机器上点击你的 url。

另外,作为旁注,您不需要为客户端请求提供解析的 URL,您只需传入一个字符串,如cold_start_url + '/oauth/token'.


推荐阅读