def _request(self, method, url, params=None, data=None, headers=None, cookies=None, files=None, auth=None, timeout=None, allow_redirects=True, proxies=None, hooks=None, stream=None, verify=None, cert=None, json=None): if not headers: headers = {} url = quote(url, safe="%/:=&?~#+!$,;'@()*[]") handlers = [] import sys # starting with python 2.7.9 urllib verifies every https request if False == verify and sys.version_info >= (2, 7, 9): import ssl ssl_context = ssl.create_default_context() ssl_context.check_hostname = False ssl_context.verify_mode = ssl.CERT_NONE handlers.append(HTTPSHandler(context=ssl_context)) # handlers.append(HTTPCookieProcessor()) # handlers.append(ErrorHandler) if not allow_redirects: handlers.append(NoRedirectHandler) opener = build_opener(*handlers) if params: url = '{0}?{1}'.format(url, urlencode(params)) request = _request(url) if headers: for key in headers: request.add_header(key, headers[key]) if data or json: if self.plugin.get_dict_value(headers, 'content-type').startswith('application/x-www-form-urlencoded') and data: # transform a string into a map of values if isinstance(data, six.string_types): _data = data.split('&') data = {} for item in _data: name, value = item.split('=') data[name] = value request.data = urlencode(data) elif self.plugin.get_dict_value(headers, 'content-type').startswith('application/json') and data: request.data = dumps(data).encode('utf-8') elif json: request.data = dumps(json).encode('utf-8') else: if not isinstance(data, six.string_types): data = str(data) if isinstance(data, str): data = data.encode('utf-8') request.data = data elif method.upper() in ['POST', 'PUT']: request.data = 'null'.encode('utf-8') request.get_method = lambda: method result = Response() response = None try: response = opener.open(request, timeout=30) except HTTPError as e: # HTTPError implements addinfourl, so we can use the exception to construct a response if isinstance(e, addinfourl): response = e except Exception as e: result.text = e return result # process response result.headers.update(response.headers) result.status_code = response.getcode() if method.upper() == 'HEAD': return result elif response.headers.get('Content-Encoding', '').startswith('gzip'): buf = StringIO(response.read()) f = GzipFile(fileobj=buf) result.text = f.read() else: result.text = response.read() return result
def put(self, url, data=None, json=None, **kwargs): return _request('PUT', url, data=data, json=json, **kwargs)