HttpUtils.py 2.3 KB

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