def request(self, method, url, body, headers):
# Calculate the absolute URI, which fetch requires
netloc = self.host
if self.port:
netloc = '%s:%s' % (self.host, self.port)
absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
try:
response = fetch(absolute_uri, payload=body, method=method,
headers=headers, allow_truncated=False, follow_redirects=False,
deadline=self.timeout,
validate_certificate=self.validate_certificate)
self.response = ResponseDict(response.headers)
self.response['status'] = str(response.status_code)
self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
self.response.status = response.status_code
setattr(self.response, 'read', lambda : response.content)
# Make sure the exceptions raised match the exceptions expected.
except InvalidURLError:
raise socket.gaierror('')
except (DownloadError, ResponseTooLargeError, SSLCertificateError):
raise httplib.HTTPException()
python类HTTPException()的实例源码
def post_to_thingspeak(payload):
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/plain"}
not_connected = 1
while (not_connected):
try:
conn = httplib.HTTPConnection("api.thingspeak.com:80")
conn.connect()
not_connected = 0
except (httplib.HTTPException, socket.error) as ex:
print "Error: %s" % ex
time.sleep(10) # sleep 10 seconds
conn.request("POST", "/update", payload, headers)
response = conn.getresponse()
print( response.status, response.reason, payload, time.strftime("%c"))
data = response.read()
conn.close()
def post_to_mcs(payload):
headers = {"Content-type": "application/json", "deviceKey": deviceKey}
not_connected = 1
while (not_connected):
try:
conn = httplib.HTTPConnection("api.mediatek.com:80")
conn.connect()
not_connected = 0
except (httplib.HTTPException, socket.error) as ex:
print "Error: %s" % ex
time.sleep(10) # sleep 10 seconds
conn.request("POST", "/mcs/v2/devices/" + deviceId + "/datapoints", json.dumps(payload), headers)
response = conn.getresponse()
print( response.status, response.reason, json.dumps(payload), time.strftime("%c"))
data = response.read()
conn.close()
def request(self, method, url, body, headers):
# Calculate the absolute URI, which fetch requires
netloc = self.host
if self.port:
netloc = '%s:%s' % (self.host, self.port)
absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
try:
response = fetch(absolute_uri, payload=body, method=method,
headers=headers, allow_truncated=False, follow_redirects=False,
deadline=self.timeout,
validate_certificate=self.validate_certificate)
self.response = ResponseDict(response.headers)
self.response['status'] = str(response.status_code)
self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
self.response.status = response.status_code
setattr(self.response, 'read', lambda : response.content)
# Make sure the exceptions raised match the exceptions expected.
except InvalidURLError:
raise socket.gaierror('')
except (DownloadError, ResponseTooLargeError, SSLCertificateError):
raise httplib.HTTPException()
def request(self, method, url, body, headers):
# Calculate the absolute URI, which fetch requires
netloc = self.host
if self.port:
netloc = '%s:%s' % (self.host, self.port)
absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
try:
response = fetch(absolute_uri, payload=body, method=method,
headers=headers, allow_truncated=False, follow_redirects=False,
deadline=self.timeout,
validate_certificate=self.validate_certificate)
self.response = ResponseDict(response.headers)
self.response['status'] = str(response.status_code)
self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
self.response.status = response.status_code
setattr(self.response, 'read', lambda : response.content)
# Make sure the exceptions raised match the exceptions expected.
except InvalidURLError:
raise socket.gaierror('')
except (DownloadError, ResponseTooLargeError, SSLCertificateError):
raise httplib.HTTPException()
def postInfo(self, requestParams):
logging.info("About to phone home to [%s].", self.url)
req = urllib2.Request(self.url)
req.add_header('Content-Type', 'application/json')
resp = None
try:
resp = urllib2.urlopen(req, json.dumps(requestParams), timeout = 30, **self.kwargs)
resp = resp.read()
except urllib2.HTTPError, e:
logging.error("HTTPError: %s", str(e.code))
except urllib2.URLError, e:
logging.error("URLError: %s", str(e.reason))
except httplib.HTTPException, e:
logging.error("HTTPException: %s", str(e))
except Exception, e:
logging.exception("Unexpected error: %s", str(e))
return resp
def request(self, method, url, body, headers):
# Calculate the absolute URI, which fetch requires
netloc = self.host
if self.port:
netloc = '%s:%s' % (self.host, self.port)
absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
try:
response = fetch(absolute_uri, payload=body, method=method,
headers=headers, allow_truncated=False, follow_redirects=False,
deadline=self.timeout,
validate_certificate=self.validate_certificate)
self.response = ResponseDict(response.headers)
self.response['status'] = response.status_code
setattr(self.response, 'read', lambda : response.content)
# Make sure the exceptions raised match the exceptions expected.
except InvalidURLError:
raise socket.gaierror('')
except (DownloadError, ResponseTooLargeError, SSLCertificateError):
raise httplib.HTTPException()
def _get_content(self, url):
http_request = urllib2.Request(url=url)
api_key = self.config.get('api_key')
if api_key:
http_request.add_header('Authorization', api_key)
try:
http_response = urllib2.urlopen(http_request)
except urllib2.HTTPError, e:
if e.getcode() == 404:
raise ContentNotFoundError('HTTP error: %s' % e.code)
else:
raise ContentFetchError('HTTP error: %s' % e.code)
except urllib2.URLError, e:
raise ContentFetchError('URL error: %s' % e.reason)
except httplib.HTTPException, e:
raise ContentFetchError('HTTP Exception: %s' % e)
except socket.error, e:
raise ContentFetchError('HTTP socket error: %s' % e)
except Exception, e:
raise ContentFetchError('HTTP general exception: %s' % e)
return http_response.read()
def send_http(self, url):
o = urlparse(url)
r = 0
try:
conn = httplib.HTTPConnection(o[1], timeout=self.timeout)
if o[4]:
conn.request('GET', o[2] + o[3] + '?' + o[4], headers=self.headers)
else:
conn.request('GET', o[2] + o[3], headers=self.headers)
r = conn.getresponse()
logger.info('%s %s' % (url, r.status))
time.sleep(self.delay)
except (httplib.HTTPException, socket.timeout, socket.gaierror, Exception), e:
logger.error('url %s is unreachable, Exception %s %s' % (url, e.__class__.__name__, e))
print 'url %s is unreachable, Exception %s %s' % (url.encode('utf-8'), e.__class__.__name__, e)
pass
return r
def _conn_request(self, conn, request_uri, method, body, headers):
for i in range(2):
try:
conn.request(method, request_uri, body, headers)
response = conn.getresponse()
except socket.gaierror:
conn.close()
raise ServerNotFoundError("Unable to find the server at %s" % conn.host)
except httplib.HTTPException, e:
if i == 0:
conn.close()
conn.connect()
continue
else:
raise
else:
content = response.read()
response = Response(response)
if method != "HEAD":
content = _decompressContent(response, content)
break;
return (response, content)
def send(verb, endpoint, body):
__API_LISTENER = __IP_ADDR + ":" + __PORT
iprint("sending to: " + __API_LISTENER)
try:
conn = httplib.HTTPConnection(__API_LISTENER)
if len(body) != 0:
conn.request(
verb,
endpoint,
body,
{"Content-Type": "application/json"}
)
else :
conn.request(verb, endpoint)
response = conn.getresponse()
body = response.read()
return response.status, response.reason, body
except (httplib.HTTPException, socket.error) as ex:
print ("Error: %s" % ex)
quit()
def get_infos(params, adr):
data = urllib.urlencode(params)
req = urllib2.Request(adr, data)
try:
response = urllib2.urlopen(req)
except urllib2.HTTPError, e:
print e
return False
except urllib2.URLError, e:
print e
return False
except httplib.HTTPException, e:
print e
return False
read_resp = response.read()
if len(read_resp) > 0:
return json.loads(read_resp)
else:
return False
def request(self, method, url, body, headers):
# Calculate the absolute URI, which fetch requires
netloc = self.host
if self.port:
netloc = '%s:%s' % (self.host, self.port)
absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
try:
response = fetch(absolute_uri, payload=body, method=method,
headers=headers, allow_truncated=False, follow_redirects=False,
deadline=self.timeout,
validate_certificate=self.validate_certificate)
self.response = ResponseDict(response.headers)
self.response['status'] = str(response.status_code)
self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
self.response.status = response.status_code
setattr(self.response, 'read', lambda : response.content)
# Make sure the exceptions raised match the exceptions expected.
except InvalidURLError:
raise socket.gaierror('')
except (DownloadError, ResponseTooLargeError, SSLCertificateError):
raise httplib.HTTPException()
def _conn_request(self, conn, request_uri, method, body, headers):
for i in range(2):
try:
conn.request(method, request_uri, body, headers)
except socket.gaierror:
conn.close()
raise ServerNotFoundError("Unable to find the server at %s" % conn.host)
except socket.error, e:
if not hasattr(e, 'errno'): # I don't know what this is so lets raise it if it happens
raise
elif e.errno == errno.ECONNREFUSED: # Connection refused
raise
# Just because the server closed the connection doesn't apparently mean
# that the server didn't send a response.
pass
except httplib.HTTPException:
# Just because the server closed the connection doesn't apparently mean
# that the server didn't send a response.
pass
try:
response = conn.getresponse()
except (socket.error, httplib.HTTPException):
if i == 0:
conn.close()
conn.connect()
continue
else:
raise
else:
content = ""
if method == "HEAD":
response.close()
else:
content = response.read()
response = Response(response)
if method != "HEAD":
content = _decompressContent(response, content)
break
return (response, content)
def request(self, method, url, body, headers):
# Calculate the absolute URI, which fetch requires
netloc = self.host
if self.port:
netloc = '%s:%s' % (self.host, self.port)
absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
try:
response = fetch(absolute_uri, payload=body, method=method,
headers=headers, allow_truncated=False, follow_redirects=False,
deadline=self.timeout,
validate_certificate=self.validate_certificate)
self.response = ResponseDict(response.headers)
self.response['status'] = str(response.status_code)
self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
self.response.status = response.status_code
setattr(self.response, 'read', lambda : response.content)
# Make sure the exceptions raised match the exceptions expected.
except InvalidURLError:
raise socket.gaierror('')
except (DownloadError, ResponseTooLargeError, SSLCertificateError):
raise httplib.HTTPException()
def send(uri, body, config):
logger = config['logger']
headers = {
'Authorization': 'Bearer ' + config.get('auth_token', ''),
'Content-Type': 'application/json'
}
try:
start = time.time()
http_client.request('POST', uri, body=json.dumps(body), headers=headers)
r = http_client.getresponse()
if r.status != 200:
logger.error('error posting server to server call ' + r.reason)
return False
logger.debug('Server call took ' + str(time.time() - start) + 'ms')
response_body = r.read()
return json.loads(response_body)
except httplib.HTTPException:
init(config)
return False
def translate_text(query, source_lang_code, target_lang_code):
"""returns translated text or text indicating a translation/network error
Takes a text to be translated, source language and target language code
2 letter ISO code found in language_list.py
"""
try:
translations = TRANSLATION_SERVICE.translations().list(
source=source_lang_code,
target=target_lang_code,
q=query
).execute()
translation = translations['translations'][0]
if 'detectedSourceLanguage' in translation.keys():
source_lang_code = translation['detectedSourceLanguage']
resp = random.choice(_TRANSLATE_RESULT).format(
text=translation['translatedText'],
fromLang=language_code_dict[source_lang_code],
toLang=language_code_dict[target_lang_code])
except (HTTPError, URLError, HTTPException):
resp = random.choice(_TRANSLATE_NETWORK_ERROR)
except Exception:
resp = random.choice(_TRANSLATE_ERROR)
return resp
def _start_snapshot_request(self):
"""
Issues the HTTP request to etcd to load the snapshot but only
loads it as far as the headers.
:return: tuple of response and snapshot's etcd index.
:raises HTTPException
:raises HTTPError
:raises socket.error
:raises DriverShutdown if the etcd cluster ID changes.
"""
_log.info("Loading snapshot headers...")
resp = self._etcd_request(self._resync_http_pool,
VERSION_DIR,
recursive=True,
timeout=120,
preload_content=False)
snapshot_index = int(resp.getheader("x-etcd-index", 1))
if not self._cluster_id:
_log.error("Snapshot response did not contain cluster ID, "
"resyncing to avoid inconsistency")
raise ResyncRequired()
_log.info("Got snapshot headers, snapshot index is %s; starting "
"watcher...", snapshot_index)
return resp, snapshot_index
def request(self, method, url, body, headers):
# Calculate the absolute URI, which fetch requires
netloc = self.host
if self.port:
netloc = '%s:%s' % (self.host, self.port)
absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
try:
response = fetch(absolute_uri, payload=body, method=method,
headers=headers, allow_truncated=False, follow_redirects=False,
deadline=self.timeout,
validate_certificate=self.validate_certificate)
self.response = ResponseDict(response.headers)
self.response['status'] = str(response.status_code)
self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
self.response.status = response.status_code
setattr(self.response, 'read', lambda : response.content)
# Make sure the exceptions raised match the exceptions expected.
except InvalidURLError:
raise socket.gaierror('')
except (DownloadError, ResponseTooLargeError, SSLCertificateError):
raise httplib.HTTPException()
def send(verb, endpoint, body):
__API_LISTENER = __IP_ADDR + ":" + __PORT
iprint("sending to: " + __API_LISTENER)
try:
conn = httplib.HTTPConnection(__API_LISTENER)
if len(body) != 0:
conn.request(
verb,
endpoint,
body,
{"Content-Type": "application/json"}
)
else :
conn.request(verb, endpoint)
response = conn.getresponse()
body = response.read()
return response.status, response.reason, body
except (httplib.HTTPException, socket.error) as ex:
print ("Error: %s" % ex)
quit()
def test_download_failed_HTTPException(self, mock_urlopen):
mock_urlopen.side_effect = httplib.HTTPException()
fake_request = urllib2.Request('http://fakeurl.com')
self.assertRaises(
self.glance.RetryableError,
self.glance._download_tarball_and_verify,
fake_request, 'fake_staging_path')
def connection(self, url, scheme=HTTPS_SCHEME):
site = scheme + '://' + url
req = urllib2.Request(site)
req.add_header('User-Agent', self.settings['http']['user_agent'])
req.add_header('Origin', self.settings['http']['origin'])
try:
response = urllib2.urlopen(req, timeout=3)
except socket.error as error:
return str(error), -1, ''
except urllib2.URLError as error:
return str(error.reason), -2, ''
except httplib.HTTPException as error:
return str(error), -3, ''
else:
return response.geturl(), response.getcode(), response.info().items()
def detect_wp(html, dominio):
soup = BeautifulSoup(html, "html.parser")
try:
#Buscamos generator
gen = soup.findAll(attrs={"name":"generator"})
if "Wordpress" in str(gen):
return True
else: #Buscamos wp-content en el html
if html.find("wp-content")>0:
return True
else:#Buscamos links con xmlrpc.php
links = soup.findAll("link")
for l in links:
if "xmlrpc.php" in str(l):
return True
else:#Buscamos el readme.html
try:
url = "http://" + dominio + "/readme.html"
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
for h1 in soup.find_all('h1', {'id':"logo"}):
h1 = remove_tags(str(h1)) #PARSER
if h1:
return True
except urllib2.HTTPError, e:
continue
except urllib2.URLError, e:
continue
except httplib.HTTPException, e:
continue
except:
return False
def request(self, method, url, body, headers):
# Calculate the absolute URI, which fetch requires
netloc = self.host
if self.port:
netloc = '%s:%s' % (self.host, self.port)
absolute_uri = '%s://%s%s' % (self.scheme, netloc, url)
try:
try: # 'body' can be a stream.
body = body.read()
except AttributeError:
pass
response = fetch(absolute_uri, payload=body, method=method,
headers=headers, allow_truncated=False, follow_redirects=False,
deadline=self.timeout,
validate_certificate=self.validate_certificate)
self.response = ResponseDict(response.headers)
self.response['status'] = str(response.status_code)
self.response['reason'] = httplib.responses.get(response.status_code, 'Ok')
self.response.status = response.status_code
setattr(self.response, 'read', lambda : response.content)
# Make sure the exceptions raised match the exceptions expected.
except InvalidURLError:
raise socket.gaierror('')
except (DownloadError, ResponseTooLargeError, SSLCertificateError):
raise httplib.HTTPException()
def getresponse(self):
if self.response:
return self.response
else:
raise httplib.HTTPException()
def _TestServerConnection(self):
# Wait for server to start
server_msg = ''
for timeout in xrange(1, 5):
client_error = None
try:
with contextlib.closing(httplib.HTTPConnection(
'127.0.0.1', self.port, timeout=timeout)) as http:
http.set_debuglevel(timeout > 3)
http.request('HEAD', '/')
r = http.getresponse()
r.read()
if (r.status == 200 and r.reason == 'OK' and
r.getheader('Server') == self.server_tag):
return (None, server_msg)
client_error = ('Bad response: %s %s version %s\n ' %
(r.status, r.reason, r.version) +
'\n '.join([': '.join(h) for h in r.getheaders()]))
except (httplib.HTTPException, socket.error) as client_error:
pass # Probably too quick connecting: try again
# Check for server startup error messages
ix = self.process.expect([pexpect.TIMEOUT, pexpect.EOF, '.+'],
timeout=timeout)
if ix == 2: # stdout spew from the server
server_msg += self.process.match.group(0) # pylint: disable=no-member
elif ix == 1: # EOF -- server has quit so giveup.
client_error = client_error or 'Server exited'
break
return (client_error or 'Timeout', server_msg)
def dotnetpad(lang, code, timeout=30):
"Posts a provided snippet of code in a provided langugage to dotnetpad.net"
code = code.encode('utf8')
params = urllib.urlencode({'language': lang, 'code': code})
headers = {"Content-type": "application/x-www-form-urlencoded",
"Accept": "text/plain"}
try:
conn = httplib.HTTPConnection("dotnetpad.net", 80, timeout=timeout)
conn.request("POST", "/Skybot", params, headers)
response = conn.getresponse()
except httplib.HTTPException:
conn.close()
return 'error: dotnetpad is broken somehow'
except socket.error:
return 'error: unable to connect to dotnetpad'
try:
result = json.loads(response.read())
except ValueError:
conn.close()
return 'error: dotnetpad is broken somehow'
conn.close()
if result['Errors']:
return 'First error: %s' % (result['Errors'][0]['ErrorText'])
elif result['Output']:
return result['Output'].lstrip()
else:
return 'No output'
def getresponse(self):
if self.response:
return self.response
else:
raise httplib.HTTPException()
def getresponse(self):
if self.response:
return self.response
else:
raise httplib.HTTPException()
def getresponse(self):
if self.response:
return self.response
else:
raise httplib.HTTPException()