51Testing软件测试论坛

标题: Locust 脚本-HttpLocust [打印本页]

作者: 歪小Y_02    时间: 2019-3-22 16:43
标题: Locust 脚本-HttpLocust
HttpLocust类比Locust类多了client特性,而Locust内部其实使用了python的requests模块发送HTTP请求,所以HTTP响应类的特性及方法也十分类似
  1. from locust import HttpLocust, TaskSet, task, events
  2. import requests

  3. class UserBehavior(TaskSet):

  4.         def login(self):
  5.                 a = requests.post("https://xxx.xxxxxxx.com/?c=login&m=login",{"username":"test1","password":"123456"})
  6.                 ck = a.cookies
  7.                 return ck

  8.         @task
  9.         def profile1(self):
  10.                 b = self.client.get("/",cookies=self.login())
  11.                 print(dir(b))
复制代码
  1. ['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__doc__',
  2. '__enter__', '__exit__', '__format__', '__getattribute__', '__getstate__',
  3. '__hash__', '__init__', '__iter__', '__module__', '__new__', '__nonzero__',
  4. '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__',
  5. '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content',
  6. '_content_consumed', '_next', 'apparent_encoding', 'close', 'connection',
  7. 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history',
  8. 'is_permanent_redirect', 'is_redirect', 'iter_content', 'iter_lines',
  9. 'json', 'links', 'next', 'ok', 'raise_for_status', 'raw', 'reason',
  10. 'request', 'status_code', 'text', 'url']



  11. 常用方法

  12. b.cookies   
  13. b.json()
  14. b.status_code
  15. b.text
  16. b.history
  17. b.reason
  18. b.encoding
  19. b.headers
复制代码
使用cookie,和requests类时一样,当做参数传给get、post即可

作者: qqq911    时间: 2019-4-12 10:47
感谢分享
作者: Miss_love    时间: 2020-12-25 16:57
感谢分享




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2