AttributeError:' str'对象没有属性' load'

时间:2018-04-10 09:39:56

标签: python python-3.x

我正在尝试使用LocustIO进行负载测试。为了进行POST调用,我将json params传递给它并返回值。我创建了一个预期的响应值,因为它是预期返回的值。现在我一直在尝试比较预期值和响应值,我一直在收到这个错误:

s...
[2018-04-10 10:32:23,351] DESKTOP-MJ573AA/INFO/stdout: Response status code:
[2018-04-10 10:32:23,351] DESKTOP-MJ573AA/INFO/stdout:
[2018-04-10 10:32:23,352] DESKTOP-MJ573AA/INFO/stdout: 201
[2018-04-10 10:32:23,352] DESKTOP-MJ573AA/INFO/stdout:
[2018-04-10 10:32:23,352] DESKTOP-MJ573AA/INFO/stdout: Response status code:
[2018-04-10 10:32:23,352] DESKTOP-MJ573AA/INFO/stdout:
[2018-04-10 10:32:23,352] DESKTOP-MJ573AA/INFO/stdout: b'[\n  {\n    "userId": 1,\n    "id": 1,\n    "title": "delectus aut autem",\n    "completed": false\n  }\n]'
[2018-04-10 10:32:23,352] DESKTOP-MJ573AA/INFO/stdout:
[2018-04-10 10:32:23,354] DESKTOP-MJ573AA/ERROR/stderr: Traceback (most recent call last):
  File "c:\users\kadeoya\appdata\local\programs\python\python36\lib\site-packages\locust\core.py", line 271, in run
    self.execute_next_task()
  File "c:\users\kadeoya\appdata\local\programs\python\python36\lib\site-packages\locust\core.py", line 297, in execute_next_task
    self.execute_task(task["callable"], *task["args"], **task["kwargs"])
  File "c:\users\kadeoya\appdata\local\programs\python\python36\lib\site-packages\locust\core.py", line 309, in execute_task
    task(self, *args, **kwargs)
  File "C:\Programs\Others\Locust\locustfile.py", line 37, in send
    a = json.loads(responseValue)
AttributeError: 'str' object has no attribute 'loads'

我已经在SO上尝试了所有建议的选项,似乎没有一个可行。对不起,这只是我学习python的第3天。一个例子 - Python 'str' object has no attribute 'read' 代码:

from locust import HttpLocust, TaskSet, task
from slumber import API 
import json, requests

json = """
[{
  userId: 1
}]
"""

jsonsecond = """
[{
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  }]
"""

responseValue = """
[{
    "userId": 1,
    "id": 1,
    "title": "delectus aut autem",
    "completed": false
  }]
"""

json_response = dict()
class MyTaskSet(TaskSet):
  @task(1)
  def send(self):
    response = self.client.post("/todos", jsonsecond, headers= { 'Content-Type': 'application/json', 'Accept': 'application/json', 'Authorization': 'Token eyJhbGciOiJSUzI1NiIsImtpZCI6ImRiYzQ3M2U0OWViODkyNGM4YTdjMDkxNmZhZmFjODgyIiwidHlwIjoiSldUIn0.eyJuYmYiOjE1MTE4ODk5NzIsImV4cCI6MTUxNDQ4MTk3MiwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwIiwiYXVkIjpbImh0dHA6Ly9sb2NhbGhvc3Q6NTAwMC9yZXNvdXJjZXMiLCJhcGkxIl0sImNsaWVudF9pZCI6ImNsaWVudCIsInNjb3BlIjpbImFwaTEiXX0.Qku54rtPbPyyEubJekXwKlbmKXEI3hABPVYNZzpAv69Dii8Ud42ucDyZ90gjyZ3yF7v5XiK9s_ZuKmiL2IxJF4zDc4dal78T7B9gnwmSatyWPRBOKjJw8N_qcuaJlnOsvVVAZ2JuMqZion7GX8W8ykbG3xjB-EAPpyYCoP7YEI8qqMQ6PLKpLi_RwYNt3ATF6nzOQIIV4McfroNPuRKfHAszgLqPMPw2bWqcgFzSEeQTTzU2lQieQNr3vOsUVmW6DX5QpdpwYSW4fNeEByVgcCrvPx-ZnPm85HR4nthEAV1Y1yoxJUzgl1wOu9k2JadRdjnF_8UVWriO96zKb2PDDg' } )
    print("Response status code:", response.status_code)
    print("Response status code:", response.content)

    a = json.loads(responseValue)
    b = json.loads(response.content)
    if sorted(a.items()) == sorted(b.items()):
      print('good. application success')

class MyLocust(HttpLocust):
    task_set = MyTaskSet
    min_wait = 5000
    max_wait = 15000
    # host = "http://www.somaku.com"
    host = "https://jsonplaceholder.typicode.com"
    stop_timeout = 200

1 个答案:

答案 0 :(得分:2)

在课程后命名变量。

在这种情况下,请替换:

json = """
[{
  userId: 1
}]
"""

以下内容:

json_str = """
[{
  userId: 1
}]
"""

然后您的代码应按预期运行。