http_utils.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # -*- coding:UTF-8 -*-
  2. import requests
  3. import json
  4. from V2 import contants
  5. from V2.print_log import printlog
  6. def notify_cut_state(gcp_code, progress, msg):
  7. url = contants.get_notify_url()
  8. printlog(msg)
  9. data = {"gcp_code": gcp_code, "msg": msg, "progress": progress}
  10. content = http_request(url, data, False, 'get', False)
  11. printlog(str(content))
  12. def http_request(url, data, is_https=False, method='post', is_json_type=False):
  13. try:
  14. headers = dict()
  15. if is_json_type is True:
  16. content_type = 'application/json; charset=UTF-8'
  17. else:
  18. content_type = 'application/x-www-form-urlencoded; charset=UTF-8'
  19. if is_https is True:
  20. url = 'https://%s' % url
  21. else:
  22. url = 'http://%s' % url
  23. headers['Content-Type'] = content_type
  24. post_data = set_params(data) if is_json_type is False else json.dumps(data)
  25. if method == 'post':
  26. result = requests.post(url, data=post_data, headers=headers, verify=False)
  27. else:
  28. result = requests.get(url + '?' + post_data, headers=headers, verify=False)
  29. content = result.text
  30. if not content:
  31. return False
  32. return content
  33. except Exception as e:
  34. return False
  35. def set_params(params={}):
  36. """
  37. 生成参数
  38. """
  39. import urllib
  40. if not isinstance(params, dict):
  41. raise Exception("You must pass in a dictionary!")
  42. params_list = []
  43. for k, v in params.items():
  44. if isinstance(v, list) and v:
  45. if isinstance(v[0], dict):
  46. params_list.append((k, json.dumps(v)))
  47. else:
  48. params_list.extend([(k, x) for x in v])
  49. elif isinstance(v, dict):
  50. params_list.append((k, json.dumps(v)))
  51. else:
  52. params_list.append((k, v))
  53. return urllib.parse.urlencode(params_list)