http_utils.py 2.8 KB

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