12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- # -*- coding:UTF-8 -*-
- import requests
- import json
- import contants
- from print_log import printlog
- from urllib import parse
- def notify_cut_state(gcp_code, progress, msg):
- url = contants.get_notify_url()
- printlog(msg)
- data = {"gcp_code": gcp_code, "msg": msg, "progress": progress}
- content = http_request(url, data, False, 'get', False)
- printlog(str(content))
- def http_request(url, data, is_https=False, method='post', is_json_type=False):
- try:
- headers = dict()
- if is_json_type is True:
- content_type = 'application/json; charset=UTF-8'
- else:
- content_type = 'application/x-www-form-urlencoded; charset=UTF-8'
- if is_https is True:
- url = 'https://%s' % url
- else:
- url = 'http://%s' % url
- headers['Content-Type'] = content_type
- post_data = set_params(data) if is_json_type is False else json.dumps(data)
- if method == 'post':
- result = requests.post(url, data=post_data, headers=headers, verify=False)
- else:
- result = requests.get(url + '?' + post_data, headers=headers, verify=False)
- content = result.text
- if not content:
- return False
- return content
- except Exception as e:
- return False
- def set_params(params={}):
- """
- 生成参数
- """
- import urllib
- if not isinstance(params, dict):
- raise Exception("You must pass in a dictionary!")
- params_list = []
- for k, v in params.items():
- if isinstance(v, list) and v:
- if isinstance(v[0], dict):
- params_list.append((k, json.dumps(v)))
- else:
- params_list.extend([(k, x) for x in v])
- elif isinstance(v, dict):
- params_list.append((k, json.dumps(v)))
- else:
- params_list.append((k, v))
- return urllib.parse.urlencode(params_list)
|