def __init__(self, uri, status_code, *args): HTTPException.__init__(self, uri, status_code, args) self.uri = uri if isinstance(status_code, int): self.status = status_code else: self.status = -1 self.message = str(self.status) + ": Problem accessing " + uri + "."
def uploadFile(self, fileName, url, fieldName='file1', params=[], verb='POST'): """ Upload a file with curl streaming it directly from disk """ ckey, cert = self.getKeyCert() capath = self.getCAPath() import pycurl c = pycurl.Curl() if verb == 'POST': c.setopt(c.POST, 1) elif verb == 'PUT': c.setopt(pycurl.CUSTOMREQUEST, 'PUT') else: raise HTTPException("Verb %s not sopported for upload." % verb) c.setopt(c.URL, url) fullParams = [(fieldName, (c.FORM_FILE, fileName))] fullParams.extend(params) c.setopt(c.HTTPPOST, fullParams) bbuf = StringIO.StringIO() hbuf = StringIO.StringIO() c.setopt(pycurl.WRITEFUNCTION, bbuf.write) c.setopt(pycurl.HEADERFUNCTION, hbuf.write) if capath: c.setopt(pycurl.CAPATH, capath) c.setopt(pycurl.SSL_VERIFYPEER, True) else: c.setopt(pycurl.SSL_VERIFYPEER, False) if ckey: c.setopt(pycurl.SSLKEY, ckey) if cert: c.setopt(pycurl.SSLCERT, cert) c.perform() hres = hbuf.getvalue() bres = bbuf.getvalue() rh = ResponseHeader(hres) c.close() if rh.status < 200 or rh.status >= 300: exc = HTTPException(bres) setattr(exc, 'req_data', fullParams) setattr(exc, 'url', url) setattr(exc, 'result', bres) setattr(exc, 'status', rh.status) setattr(exc, 'reason', rh.reason) setattr(exc, 'headers', rh.header) raise exc return bres
def lambda_handler(event, context): message = json.loads(event['Records'][0]['Sns']['Message']) print json.dumps(message, indent=4, sort_keys=True) #Create Cluster Snapshot for EDW Solution redshift_client = boto3.client('redshift') snapshotId=str(int(time.time())) #unique id for snapshotName as EpcoHTime clusterIdKeyName = 'clusterIdentifier' try: clusterIdentifier = message[clusterIdKeyName] except KeyError: print "Need to provide a '{}' key value to take a snapshot of Redshift Cluster".format(clusterIdKeyName) response = redshift_client.create_cluster_snapshot( SnapshotIdentifier='manual-{}-{}'.format(clusterIdentifier,snapshotId), ClusterIdentifier=clusterIdentifier ) if response['ResponseMetadata']['HTTPStatusCode']!=200: raise HTTPException( "Lambda failed to take snapshot from Redshift Cluster {} with response: {}".format( clusterIdentifier, response) )
def test_later_exceptions(self): log.debug("test_later_read_exceptions") client = stub_etcd.Client() client.add_read_result(key="/bloop", value="value") client.add_read_exception(stub_etcd.EtcdException()) client.add_read_result(key="/bloop", value="value") client.add_read_exception(ReadTimeoutError("pool", "url", "message")) client.add_read_result(key="/bloop", value="value") client.add_read_exception(SocketTimeout()) client.add_read_result(key="/bloop", value="value") client.add_read_exception(ConnectTimeoutError()) client.add_read_result(key="/bloop", value="value") client.add_read_exception(HTTPError()) client.add_read_result(key="/bloop", value="value") client.add_read_exception(HTTPException()) client.add_read_result(key="/bloop", value="value") client.add_read_exception(stub_etcd.EtcdClusterIdChanged()) client.add_read_result(key="/bloop", value="value") client.add_read_exception(stub_etcd.EtcdEventIndexCleared()) elector = election.Elector(client, "test_basic", "/bloop", interval=5, ttl=15) self._wait_and_stop(client, elector)
def connect(*args, **ckwargs): if 'give_content_type' in kwargs: if len(args) >= 7 and 'Content-Type' in args[6]: kwargs['give_content_type'](args[6]['Content-Type']) else: kwargs['give_content_type']('') if 'give_connect' in kwargs: kwargs['give_connect'](*args, **ckwargs) status = code_iter.next() if isinstance(status, tuple): status, expect_status = status else: expect_status = status etag = etag_iter.next() headers = headers_iter.next() timestamp = timestamps_iter.next() if status <= 0: raise HTTPException() if body_iter is None: body = static_body or '' else: body = body_iter.next() return FakeConn(status, etag, body=body, timestamp=timestamp, expect_status=expect_status, headers=headers)
def request(self, url, data=None, flavor=None, command='command'): '''Sends a request and returns the data from the response. url as in the XML file of that UMC module. command may be anything that UMCP understands, especially: * command (default) * get (and url could be 'ucr' then) * set (and url would be '' and data could be {'locale':'de_DE'}) * upload (url could be 'udm/license/import') ''' if data is None: data = {} try: if command in ('command', 'upload'): response = self.client.umc_command(url, data, flavor) elif command == 'get': response = self.client.umc_get(url) elif command == 'set': response = self.client.umc_set(data) except Forbidden: raise NotImplementedError('command forbidden: %s' % url) except HTTPError as exc: if self._error_handler: self._error_handler(str(exc)) raise HTTPException(str(exc)) return response.result
def test_http_call_failed(self, mock_conn): client = PrestoClient('any_host', 'any_user', 8080) mock_conn.side_effect = HTTPException('Error') self.assertFalse(client.execute_query('any_sql')) mock_conn.side_effect = socket.error('Error') self.assertFalse(client.execute_query('any_sql'))
def connect(*args, **ckwargs): if kwargs.get('slow_connect', False): sleep(0.1) if 'give_content_type' in kwargs: if len(args) >= 7 and 'Content-Type' in args[6]: kwargs['give_content_type'](args[6]['Content-Type']) else: kwargs['give_content_type']('') if 'give_connect' in kwargs: kwargs['give_connect'](*args, **ckwargs) status = code_iter.next() etag = etag_iter.next() headers = headers_iter.next() timestamp = timestamps_iter.next() if status <= 0: raise HTTPException() if body_iter is None: body = static_body or '' else: body = body_iter.next() return FakeConn(status, etag, body=body, timestamp=timestamp, headers=headers)
def test_retry_vs_no_retry(self): def error(*args, **kwargs): t.append('e') raise e hb = self.new_heartbeat_task() hb._urllib2.urlopen = error hb._retry_gap_secs = 1 hb.set_interval_secs(100) hb._heartbeat_max_retries = 2 # http non-5xx t = [] e = HTTPError(None, 400, None, None, None) hb.tick() self.assertEquals(t, ['e']) # http 5xx t = [] e = HTTPError(None, 500, None, None, None) hb.tick() self.assertEquals(t, ['e', 'e']) # urlerror t = [] e = URLError("foo") hb.tick() self.assertEquals(t, ['e', 'e']) # httpexception t = [] e = HTTPException() hb.tick() self.assertEquals(t, ['e', 'e'])
def request(self, connection, url, headers, data=None, timeout=0): result = response = None try: if data: connection.request('POST', url, data, headers) else: connection.request('GET', url, headers=headers) response = self.timeout_response(connection, timeout) if not response: return None if response.status == httplib.UNAUTHORIZED: say_line('Wrong username or password for %s', self.server().name) self.authorization_failed = True raise NotAuthorized() r = self.max_redirects while response.status == httplib.TEMPORARY_REDIRECT: response.read() url = response.getheader('Location', '') if r == 0 or url == '': raise HTTPException('Too much or bad redirects') connection.request('GET', url, headers=headers) response = self.timeout_response(connection, timeout) r -= 1 self.long_poll_url = response.getheader('X-Long-Polling', '') self.switch.update_time = bool(response.getheader('X-Roll-NTime', '')) hostList = response.getheader('X-Host-List', '') self.stratum_header = response.getheader('x-stratum', '') if (not self.options.nsf) and hostList: self.switch.add_servers(loads(hostList)) result = loads(response.read()) if result['error']: say_line('server error: %s', result['error']['message']) raise RPCError(result['error']['message']) return (connection, result) finally: if not result or not response or (response.version == 10 and response.getheader('connection', '') != 'keep-alive') or response.getheader('connection', '') == 'close': connection.close() connection = None
def request(self, url, data=None, flavor=None, command='command'): # type: (str, Any, Optional[str], str) -> str """ Sends a request and returns the data from the response. :param str url: The partial |URL| of the |UMC| function to invoke. See the |XML| file of the |UMC| module. :param data: The data to send. :param flavor: Some |UMC| modules support flavors, e.g. the |UDM| module for sub-types. :param str command: may be anything that |UMCP| understands, especially: * `command` (default) * `get` (and `url` could be `ucr` then) * `set` (and `url` would be `` and `data` could be `{'locale':'de_DE'}`) * `upload` (`url` could be `udm/license/import`) :returns: the result of the response. :rtype: str """ if data is None: data = {} try: if command in ('command', 'upload'): response = self.client.umc_command(url, data, flavor) elif command == 'get': response = self.client.umc_get(url) elif command == 'set': response = self.client.umc_set(data) except Forbidden: raise NotImplementedError('command forbidden: %s' % url) except HTTPError as exc: if self._error_handler: self._error_handler(str(exc)) raise HTTPException(str(exc)) return response.result
def request(self, url, params, headers=None, verb='GET', verbose=0, ckey=None, cert=None, capath=None, doseq=True, decode=False, cainfo=None): """Fetch data for given set of parameters""" curl = pycurl.Curl() bbuf, hbuf = self.set_opts(curl, url, params, headers, ckey, cert, capath, verbose, verb, doseq, cainfo) curl.perform() header = self.parse_header(hbuf.getvalue()) if header.status < 300: data = self.parse_body(bbuf.getvalue(), decode) else: data = bbuf.getvalue() exc = HTTPException() setattr(exc, 'req_data', params) setattr(exc, 'req_headers', headers) setattr(exc, 'url', url) setattr(exc, 'result', data) setattr(exc, 'status', header.status) setattr(exc, 'reason', header.reason) setattr(exc, 'headers', header.header) raise exc bbuf.flush() hbuf.flush() return header, data
def test_http_call_failed(self, mock_conn, mock_presto_config): client = PrestoClient('any_host', 'any_user') mock_conn.side_effect = HTTPException("Error") self.assertFalse(client.run_sql("any_sql")) mock_conn.side_effect = socket.error("Error") self.assertFalse(client.run_sql("any_sql"))
def connect(*args, **ckwargs): if kwargs.get('slow_connect', False): eventlet.sleep(0.1) if 'give_content_type' in kwargs: if len(args) >= 7 and 'Content-Type' in args[6]: kwargs['give_content_type'](args[6]['Content-Type']) else: kwargs['give_content_type']('') i, status = conn_id_and_code_iter.next() if 'give_connect' in kwargs: give_conn_fn = kwargs['give_connect'] argspec = inspect.getargspec(give_conn_fn) if argspec.keywords or 'connection_id' in argspec.args: ckwargs['connection_id'] = i give_conn_fn(*args, **ckwargs) etag = etag_iter.next() headers = headers_iter.next() expect_headers = expect_headers_iter.next() timestamp = timestamps_iter.next() if status <= 0: raise HTTPException() if body_iter is None: body = static_body or '' else: body = body_iter.next() return FakeConn(status, etag, body=body, timestamp=timestamp, headers=headers, expect_headers=expect_headers, connection_id=i, give_send=kwargs.get('give_send'))
def get(self, url="", query={}): """ Perform the GET request and return the parsed results """ qs = urllib.urlencode(query) if qs: qs = "?%s" % qs url = "%s%s%s" % (self.base_url, url, qs) log.debug("GET %s" % (url)) self.__connection.connect() request = self.__connection.request("GET", url, None, self.__headers) response = self.__connection.getresponse() data = response.read() self.__connection.close() log.debug("GET %s status %d" % (url, response.status)) result = {} # Check the return status if response.status == 200: log.debug("%s" % data) parser = DetailsToDict() parseString(data, parser) return parser.data elif response.status == 204: raise EmptyResponseWarning( "%d %s @ https://%s%s" % (response.status, response.reason, self.host, url)) elif response.status == 404: log.debug("%s returned 404 status" % url) raise HTTPException( "%d %s @ https://%s%s" % (response.status, response.reason, self.host, url)) elif response.status >= 400: _result = simplejson.loads(data) log.debug("OUTPUT %s" % _result) raise HTTPException( "%d %s @ https://%s%s" % (response.status, response.reason, self.host, url)) return result
def test_http_call_failed(self, mock_conn, mock_port): mock_port.return_value = 8080 client = PrestoClient('any_host', 'any_user') mock_conn.side_effect = HTTPException("Error") self.assertFalse(client.execute_query("any_sql")) mock_conn.side_effect = socket.error("Error") self.assertFalse(client.execute_query("any_sql"))
def test_that_send_poll_contact_to_prioritizer_api_when_url_exists( self, post_mock): poll = Poll(pk=123, question="My important question", default_response="Thanks") settings.BLACKLIST_POLL_DATA_URL = "http://fake.com" post_mock.return_value = HTTPException() self.assertRaises(Exception, add_poll_recipients_to_blacklist(poll))
def auth(self, username, password, auth_type=None): '''Tries to authenticate against the host and preserves the cookie. Has to be done only once (but keep in mind that the session probably expires after 10 minutes of inactivity)''' try: self.client.umc_auth(username, password, auth_type=auth_type) except HTTPError as exc: raise HTTPException(str(exc))
def test_that_send_poll_information_to_prioritizer_api_throws_http_exception( self, post_mock): poll = Poll(pk=123, question="My important question", default_response="Thanks") settings.BLACKLIST_POLL_DATA_URL = "www.server.com" post_mock.return_value = HTTPException() self.assertRaises(Exception, add_poll_to_blacklist(poll))
def getexpect(self): if self.expect_status == -2: raise HTTPException() if self.expect_status == -3: return FakeConn(507) if self.expect_status == -4: return FakeConn(201) return FakeConn(100)
def send_request(self, message, tokens): request = urllib2.Request(GcmUrl) request.add_header('Authorization', 'key=%s' % GCM.authkey) request.add_header('Content-Type', 'application/json') json_request = { 'data': { 'message': message }, 'registration_ids': [], 'priority': 'high' } for token in tokens: json_request['registration_ids'].append(token.gcm_token) request.add_data(json.dumps(json_request)) response_body = '' try: response = urllib2.urlopen(request) response_body = response.read() logging.debug("GCM Message sent, response: %s" % response_body) return json.loads(response_body) except HTTPError as e: if 500 <= e.code < 600: raise Exception("NOMAIL %s, retrying whole task" % e.code) # retry else: logging.error( "Unable to send GCM message! Response code: %s, response body: %s " % (e.code, response_body)) return None # do not retry except HTTPException as e: logging.warn("HTTPException: Unable to send GCM message! %s" % traceback.format_exc()) raise HTTPException("NOMAIL %s " % e) # retry except socket.error as e: logging.warn("socket.error: Unable to send GCM message! %s" % traceback.format_exc()) raise HTTPException("NOMAIL %s " % e) # retry except: logging.error("Unable to send GCM message! %s" % traceback.format_exc()) return None
def test_acquire_credentials_connection_failure(self, si_method_mock): si = MagicMock(name="si") session_manager_property = PropertyMock( side_effect=HTTPException("hubba")) type(si.content).sessionManager = session_manager_property si_method_mock.return_value = si vim_client = VimClient(auto_sync=False) self.assertRaises(AcquireCredentialsException, vim_client._acquire_local_credentials)
def update(self, url, updates, resource_name=""): """ Make a PUT request to save updates """ url = "%s%s.xml" % (self.base_url, url) log.debug("PUT %s" % (url)) self.__connection.connect() put_headers = {"Content-Type": "application/xml"} put_headers.update(self.__headers) log.debug("Call headers %s" % pformat(put_headers)) payload = toXML(resource_name, updates) log.debug("Updating %s -> %s" % (resource_name, payload)) request = self.__connection.request("PUT", url, payload, put_headers) response = self.__connection.getresponse() data = response.read() self.__connection.close() log.debug("PUT %s status %d" % (url, response.status)) log.debug("OUTPUT: %s" % data) result = {} if response.status == 200: result = True elif response.status == 204: raise EmptyResponseWarning( "%d %s @ https://%s%s" % (response.status, response.reason, self.host, url)) elif response.status == 404: log.debug("%s returned 404 status" % url) raise HTTPException( "%d %s @ https://%s%s" % (response.status, response.reason, self.host, url)) elif response.status >= 400: _result = simplejson.loads(data) log.debug("OUTPUT %s" % _result) raise HTTPException( "%d %s @ https://%s%s" % (response.status, response.reason, self.host, url)) return result
def create(self, url, properties, name=""): resource_name = url if not name else name url = "%s%s.xml" % (self.base_url, url) log.debug("POST %s" % (url)) log.debug("Creating %s" % pformat(properties)) self.__connection.connect() put_headers = {"Content-Type": "application/xml"} put_headers.update(self.__headers) payload = toXML(resource_name, properties) log.debug("Creating %s -> %s" % (resource_name, payload)) request = self.__connection.request("POST", url, payload, put_headers) response = self.__connection.getresponse() data = response.read() self.__connection.close() log.debug("POST %s status %d" % (url, response.status)) log.debug("OUTPUT: %s" % data) result = {} if response.status == 201: parser = DetailsToDict() parseString(data, parser) return parser.data elif response.status == 204: raise EmptyResponseWarning( "%d %s @ https://%s%s" % (response.status, response.reason, self.host, url)) elif response.status == 404: log.debug("%s returned 404 status" % url) raise HTTPException( "%d %s @ https://%s%s" % (response.status, response.reason, self.host, url)) elif response.status >= 400: _result = simplejson.loads(data) log.debug("OUTPUT %s" % _result) raise HTTPException( "%d %s @ https://%s%s" % (response.status, response.reason, self.host, url))
def get_posted_image(environ): path = environ['PATH_INFO'] if environ['REQUEST_METHOD'] != 'POST' or path != '/': raise HTTPException('405 Method Not Allowed') content_length = int(environ.get('CONTENT_LENGTH', '-1')) if content_length == -1: raise HTTPException('411 Length Required') elif content_length > PRNET_MAX_IMAGE_SIZE: raise HTTPException('413 Payload Too Large') forms, files = parse_form_data(environ) if len(files) != 1 or 'image' not in files: raise HTTPException("400 Bad Request") img = files['image'] imgfile_path = tempdir.join(img.filename) img.save_as(imgfile_path) return imgfile_path
def test_acquire_credentials_connection_failure(self, password_mock, si_method_mock): si = MagicMock(name="si") session_manager_property = \ PropertyMock(side_effect=HTTPException("hubba")) type(si.content).sessionManager = session_manager_property si_method_mock.return_value = si self.assertRaises(AcquireCredentialsException, VimClient.acquire_credentials)
def request32(self, method32, *args, **kwargs): """Send a request to the 32-bit server. Parameters ---------- method32 : :class:`str` The name of the method to call in the :class:`~.server32.Server32` subclass. *args The arguments that the `method32` method in the :class:`~.server32.Server32` subclass requires. **kwargs The keyword arguments that the `method32` method in the :class:`~.server32.Server32` subclass requires. Returns ------- The response from the 32-bit server. Raises ------ :class:`~http.client.HTTPException` If there was an error processing the request on the 32-bit server. """ if not self._is_active: raise HTTPException('The 32-bit server is not active') if method32 == 'SHUTDOWN_SERVER32': self.request('GET', '/SHUTDOWN_SERVER32') return request = '/{}:{}:{}'.format(method32, self._pickle_protocol, self._pickle_temp_file) with open(self._pickle_temp_file, 'wb') as f: pickle.dump(args, f, protocol=self._pickle_protocol) pickle.dump(kwargs, f, protocol=self._pickle_protocol) self.request('GET', request) response = self.getresponse() if response.status == 200: # everything is OK with open(self._pickle_temp_file, 'rb') as f: result = pickle.load(f) return result raise HTTPException(response.read().decode())
def update(self, url, updates): """ Make a PUT request to save updates """ url = "%s%s" % (self.base_url, url) log.debug("PUT %s" % (url)) self.__connection.connect() put_headers = {"Content-Type": "application/json"} put_headers.update(self.__headers) request = self.__connection.request("PUT", url, simplejson.dumps(updates), put_headers) response = self.__connection.getresponse() data = response.read() self.__connection.close() log.debug("PUT %s status %d" % (url, response.status)) log.debug("OUTPUT: %s" % data) result = {} if response.status == 200: result = simplejson.loads(data) elif response.status == 204: raise EmptyResponseWarning( "%d %s @ https://%s%s" % (response.status, response.reason, self.host, url)) elif response.status == 404: log.debug("%s returned 404 status" % url) raise HTTPException( "%d %s @ https://%s%s" % (response.status, response.reason, self.host, url)) elif response.status >= 400: _result = simplejson.loads(data) log.debug("OUTPUT %s" % _result) raise HTTPException( "%d %s @ https://%s%s" % (response.status, response.reason, self.host, url)) return result
def patch_request(self, *args, **kwargs): """Override _request to set socket timeout to some appropriate value.""" exception = HTTPException('Unable to get response') for _ in range(3): try: return old_request(self, *args, **kwargs) except (socket.error, HTTPException, IOError, OSError, MaxRetryError) as exc: exception = exc self._conn = urllib3.PoolManager(timeout=self._timeout) raise exception
def post_multipart(host, port, selector, fields, files): content_type, body = encode_multipart_formdata(fields, files) h = HTTPConnection(host, port) h.putrequest('POST', selector) h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) h.endheaders() if _python2: h.send(body) else: h.send(body.encode('utf-8')) if _python2: errcode, errmsg, headers = h.getreply() if errcode != 200: raise HTTPException("%s: %s" % (errcode, errmsg)) return h.file.read() else: res = h.getresponse() if res.status != 200: raise HTTPException("%s: %s" % (res.status, res.reason)) return res.read()
def auth(self, username, password, auth_type=None): # type: (str, str, Optional[str]) -> None """ Tries to authenticate against the host and preserves the cookie. Has to be done only once (but keep in mind that the session probably expires after 10 minutes of inactivity) :param str username: The user name. :param str password: The user password. :param str auth_type: The authentication type, e.g. `???`. """ try: self.client.umc_auth(username, password, auth_type=auth_type) except HTTPError as exc: raise HTTPException(str(exc))
def __init__(self): HTTPException.__init__(self, 408, "Timeout") self.strerror = "Timeout"