http_utils.py 2.4 KB

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