Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def send(self,url):
http = httplib2.Http(timeout=30)
response = {}
content = {}
headers = {'Content-type': 'application/json;charset=utf8'}
try:
# print('url is :', url)
response, content = http.request(url, 'GET')
content = content.decode('utf-8')
except httplib2.ServerNotFoundError as e:
content["code"] = "Error"
content["message"] = str(e)
log.log().logger.error(e)
except Exception as e:
content["code"] = "Error"
content["message"] = str(e)
log.log().logger.error(e)
return response, content
def test_googleapiclient_connection_error(self):
error = httplib2.ServerNotFoundError('Test error')
firebase_error = _utils.handle_googleapiclient_error(error)
assert isinstance(firebase_error, exceptions.UnavailableError)
assert str(firebase_error) == 'Failed to establish a connection: Test error'
assert firebase_error.cause is error
assert firebase_error.http_response is None
shell=True,
stderr=subprocess.STDOUT)
success = False
for line in out:
if "You are now logged in as" in out:
success = True
if not success:
print "unable to authenticate:"
exit(1)
else:
break
try:
self.dataproc_service = discovery.build('dataproc', 'v1', credentials=credentials)
self.gce_service = discovery.build('compute', 'v1', credentials=credentials)
except httplib2.ServerNotFoundError as error:
print 'There was a Google API error when trying to connect to a server:\n %s' % (str(error))
exit(1)
self.parse_args()
master_node, zone = self.query_cluster()
status = self.get_master_status(master_node, zone)
if status != 'RUNNING':
print 'Master node not running, unable to connect'
exit(1)
port = random.randint(5000, 10000)
self.proxy = SshProxy(project_id=self.project_id, zone=zone, master_node=master_node,
def getValidOauth2TxtCredentials(force_refresh=False):
"""Gets OAuth2 credentials which are guaranteed to be fresh and valid."""
credentials = getOauth2TxtStorageCredentials()
if (credentials and credentials.expired) or force_refresh:
retries = 3
for n in range(1, retries+1):
try:
credentials.refresh(google_auth_httplib2.Request(_createHttpObj()))
writeCredentials(credentials)
break
except google.auth.exceptions.RefreshError as e:
systemErrorExit(18, str(e))
except (google.auth.exceptions.TransportError, httplib2.ServerNotFoundError, RuntimeError) as e:
if n != retries:
waitOnFailure(n, retries, str(e))
continue
systemErrorExit(4, str(e))
elif credentials is None or not credentials.valid:
requestOAuthAccess()
credentials = getOauth2TxtStorageCredentials()
return credentials
_print_retry_error(exception, verbose)
return True
if isinstance(exception, google.auth.exceptions.RefreshError):
_print_retry_error(exception, verbose)
return True
# For a given installation, this could be a permanent error, but has only
# been observed as transient.
if isinstance(exception, SSLError):
_print_retry_error(exception, verbose)
return True
# This has been observed as a transient error:
# ServerNotFoundError: Unable to find the server at genomics.googleapis.com
if isinstance(exception, ServerNotFoundError):
_print_retry_error(exception, verbose)
return True
# Observed to be thrown transiently from auth libraries which use httplib2
# Use the one from six because httlib no longer exists in Python3
# https://docs.python.org/2/library/httplib.html
if isinstance(exception, six.moves.http_client.ResponseNotReady):
_print_retry_error(exception, verbose)
return True
return False
def main():
flags = parse_cmdline()
logger = configure_logs(flags.logfile)
pageTokenFile = PageTokenFile(flags.ptokenfile)
for i in range(RETRY_NUM):
try:
service = build_service(flags)
pageToken = pageTokenFile.get()
deletionList, pageTokenBefore, pageTokenAfter = \
get_deletion_list(service, pageToken, flags)
pageTokenFile.save(pageTokenBefore)
listEmpty = delete_old_files(service, deletionList, flags)
except client.HttpAccessTokenRefreshError:
print('Authentication error')
except httplib2.ServerNotFoundError as e:
print('Error:', e)
except TimeoutError:
print('Timeout: Google backend error.')
print('Retries unsuccessful. Abort action.')
return
else:
break
time.sleep(RETRY_INTERVAL)
else:
print("Retries unsuccessful. Abort action.")
return
if listEmpty:
pageTokenFile.save(pageTokenAfter)
header = "posts.json?login=" + self.username + "&api_key=" + \
self.password + "&limit=" + str(self.number_per_page) + "&"
# make request
if self.verbose:
print("Making request: " + self.urlbase + header + "tags=" + self.tags + \
"&page=" + str(self.page_num))
res, content = connection.request(
self.urlbase + header + "tags=" + self.tags + "&page=" + str(self.page_num))
if len(content) >= 0 and res.status == 200:
if self.verbose:
print("Response recieved")
return json.loads(content.decode())
else:
return res.status
except (httplib2.ServerNotFoundError):
print("Could not contact server at danbooru.donmai.us")
print("Retrying in 30 seconds")
time.sleep(30)
self.getResultsJSON()
return None
def credentials_valid(user_settings):
try:
connection = get_dataverse_connection(user_settings=user_settings)
if connection.swordConnection.history[1]['payload']['response']['status'] != 200:
return {
'status' : 'failure',
'message' : 'Invalid credentials',
}
return {
'status' : 'success',
}
except ServerNotFoundError as error:
return {
'status' : 'failure',
'message' : error.message,
}
# do something else here while the request is being downloaded and decoded...
# if you want to do the decoding of json and some processing in the background
# see callback.py
pprint(content)
response, content = http.request('http://some.bad.address.that.does.not.exist/')
# do something else where the request is processing, the exception will happen
# in the worker.
# when you go to access the result the exception will be raised
try:
response.status
except ServerNotFoundError:
print "caught the expected exception"
token = oauth.Token(self._accessToken, self._accessSecret)
oauth_request = oauth.Request.from_consumer_and_token(consumer, token, 'GET', resource_url, params)
oauth_request.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token)
headers = {}
headers.update(oauth_request.to_header())
headers['user-agent'] = 'python-latitude'
headers['content-type'] = 'application/json; charset=UTF-8'
if params:
resource_url = "%s?%s" % (resource_url, urllib.urlencode(params))
h = httplib2.Http()
try:
response, content = h.request(resource_url, method = 'GET', headers = headers)
except httplib2.ServerNotFoundError, e:
raise self.ServerNotFoundError(str(e))
if response['status'] != '200':
raise self.HttpError(response)
contentJson = json.loads(content)
if contentJson.has_key('error'):
raise self.ApiError(contentJson)
return contentJson