POST-GET.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/python3
  2. # --*-- coding:utf-8 --*--
  3. import requests, json, os, time
  4. import pymysql
  5. import sys
  6. import time, datetime
  7. import logging
  8. Ids = []
  9. logging.basicConfig(filename='./app_time.log', level=logging.INFO)
  10. def auth_post():
  11. url_auth = "http://localhost:8000/auth"
  12. auth_body = {"username": "123", "password": "123"}
  13. auth_headers = {
  14. "Content-Type": "application/json, charset=UTF-8"
  15. }
  16. #json为dict时,如果不指定content-type,默认为application/json
  17. #json为str时,如果不指定content-type,默认为application/json
  18. auth_original = requests.request('post', url_auth, json=auth_body, headers=auth_headers)
  19. access_token_original = auth_original.json()
  20. access_token = access_token_original['access_token']
  21. return access_token
  22. def post_x-www-form():
  23. init_headers = {'content-type': 'application/x-www-form-urlencoded'}
  24. init_url = "http://192.1.1.1:8080/init"
  25. params = {"account_ids":Ids[0]}
  26. #data为dict时,如果不指定content-type,默认为application/x-www-form-urlencoded,相当于普通form表单提交的形式
  27. #data为str时,如果不指定content-type,默认为text/plain
  28. resp = requests.post(init_url, data=params, headers=init_headers)
  29. print(resp.text)
  30. def clear_post():
  31. url_clear = "http://localhost:8000/api/range"
  32. data_headers = {
  33. 'Content-Type': 'application/json',
  34. 'Authorization': 'Bearer ' + auth_post()}
  35. json_id = {'ID': Ids}
  36. data_original = requests.post(url_clear, json=json_id, headers=data_headers)
  37. # 发送GET请求,参数为10000,带上验证信息(Auth)。
  38. def get_resource():
  39. url_clear = "http://localhost:8000/api/source/10000?region=cn-northwest-1"
  40. data_headers = {
  41. 'Content-Type': 'application/json',
  42. 'Authorization': 'Bearer ' + auth_post()}
  43. json_id = {'ID': Ids}
  44. data_original = requests.get(url_clear, headers=data_headers)