我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用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 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 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 _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 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 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 _analysis_404(self): """ ??404?????????URL???? """ try: conn1 = httplib.HTTPConnection(self.site_parse[1], timeout=self.timeout) conn1.request('GET', self.site_parse[2]+'/../g0ogle/go0g1e/l.php', headers=self.headers) response = conn1.getresponse() self.not_found_page_length = response.getheader('Content-Length') except (httplib.HTTPException, socket.timeout, socket.gaierror, Exception), e: logger.error('url %s is unreachable, Exception %s %s' % (self.site, e.__class__.__name__, e)) print 'url %s is unreachable, Exception %s %s' % (self.site, e.__class__.__name__, e) sys.exit(1)
def open_url(connection, url, return_redirect_url=False): '''Tries to open url and return page's html''' if 'goodreads.com' in url: url = url[url.find('goodreads.com') + len('goodreads.com'):] try: connection.request('GET', url, headers=HEADERS) response = connection.getresponse() if response.status == 301 or response.status == 302: if return_redirect_url: return response.msg['location'] response = open_url(connection, response.msg['location']) else: response = response.read() except (HTTPException, socket.error): time.sleep(1) connection.close() connection.connect() connection.request('GET', url, headers=HEADERS) response = connection.getresponse() if response.status == 301 or response.status == 302: if return_redirect_url: return response.msg['location'] response = open_url(connection, response.msg['location']) else: response = response.read() if 'Page Not Found' in response: raise PageDoesNotExist('Page not found.') return response
def __init__(self, *args, **kw): unittest.TestCase.__init__(self, *args, **kw) try: self.open_mapping_file().close() # test it to report the error early except (IOError, HTTPException): self.skipTest("Could not retrieve "+self.mapfileurl)
def test_too_many_headers(self): headers = '\r\n'.join('Header%d: foo' % i for i in xrange(200)) + '\r\n' text = ('HTTP/1.1 200 OK\r\n' + headers) s = FakeSocket(text) r = httplib.HTTPResponse(s) self.assertRaises(httplib.HTTPException, r.begin)
def retry_if_network_error(exception): pick_da_useragent() do_retry = isinstance(exception, (requests.exceptions.RequestException, httplib.HTTPException, socket.error)) if do_retry: echo(Fore.YELLOW + 'Retrying... ' + repr(exception)) return do_retry
def _start_transaction(self, h, req): try: if req.has_data(): data = req.data if hasattr(req, 'selector'): h.putrequest(req.get_method() or 'POST', req.selector, skip_host=req.has_header("Host"), skip_accept_encoding=req.has_header("Accept-encoding")) else: h.putrequest(req.get_method() or 'POST', req.get_selector(), skip_host=req.has_header("Host"), skip_accept_encoding=req.has_header("Accept-encoding")) if not req.headers.has_key('Content-type'): h.putheader('Content-type', 'application/x-www-form-urlencoded') if not req.headers.has_key('Content-length'): h.putheader('Content-length', '%d' % len(data)) else: if hasattr(req, 'selector'): h.putrequest(req.get_method() or 'GET', req.selector, skip_host=req.has_header("Host"), skip_accept_encoding=req.has_header("Accept-encoding")) else: h.putrequest(req.get_method() or 'GET', req.get_selector(), skip_host=req.has_header("Host"), skip_accept_encoding=req.has_header("Accept-encoding")) except (socket.error, httplib.HTTPException), err: raise urllib2.URLError(err) if not req.headers.has_key('Connection'): req.headers['Connection'] = 'keep-alive' for args in self.parent.addheaders: if not req.headers.has_key(args[0]): h.putheader(*args) for k, v in req.headers.items(): h.putheader(k, v) h.endheaders() if req.has_data(): h.send(data)
def selenium_login(self): try: self.pydriver.get(self.config_dict[TABLEAU_SERVER_URL]) time.sleep(2) self.pydriver.page_source.encode('utf-8') logins = self.pydriver.find_elements(By.XPATH, '//input') if logins is None or len(logins) == 0: ACLogger().get_logger().error('selenium_login error 1') return False the_login = logins[0] password = logins[1] the_login.send_keys(self.config_dict[TABLEAU_SERVER_USERNAME]) password.send_keys(self.config_dict[TABLEAU_SERVER_PASSWORD]) buttons = self.pydriver.find_elements(By.XPATH, '//button') button = None for b in buttons: if str(b.text) == 'Sign In': button = b if button is None: ACLogger().get_logger().error('selenium_login error 2') return False button.click() time.sleep(2) if 'Workbooks' not in self.pydriver.title: ACLogger().get_logger().error('selenium_login error 3') return False except (WebDriverException, StaleElementReferenceException, HTTPException, ResponseNotReady) , e: ACLogger().get_logger().error('selenium_login error: %s' % str(e)) return False ACLogger().get_logger().info( 'Logged in %s successfully, next page title: %s' % (self.config_dict[TABLEAU_SERVER_URL], self.pydriver.title)) return True
def connect(self): """Opens a HTTP connection to the RPC server.""" logger.debug("Opening connection to %s:%s", self.url.hostname, self.url.port) try: self.connection = httplib.HTTPConnection(self.url.hostname, self.url.port) self.connection.connect() except (httplib.HTTPException, socket.error) as e: raise errors.InterfaceError('Unable to connect to the specified service', e)
def close(self): """Closes the HTTP connection to the RPC server.""" if self.connection is not None: logger.debug("Closing connection to %s:%s", self.url.hostname, self.url.port) try: self.connection.close() except httplib.HTTPException: logger.warning("Error while closing connection", exc_info=True) self.connection = None
def _post_request(self, body, headers): retry_count = self.max_retries while True: logger.debug("POST %s %r %r", self.url.path, body, headers) try: self.connection.request('POST', self.url.path, body=body, headers=headers) response = self.connection.getresponse() except httplib.HTTPException as e: if retry_count > 0: delay = math.exp(-retry_count) logger.debug("HTTP protocol error, will retry in %s seconds...", delay, exc_info=True) self.close() self.connect() time.sleep(delay) retry_count -= 1 continue raise errors.InterfaceError('RPC request failed', cause=e) else: if response.status == httplib.SERVICE_UNAVAILABLE: if retry_count > 0: delay = math.exp(-retry_count) logger.debug("Service unavailable, will retry in %s seconds...", delay, exc_info=True) time.sleep(delay) retry_count -= 1 continue return response
def get_status(host, path, file): try: conn = HTTPConnection(host) conn.request('HEAD', '/{0}/{1}'.format(path, file)) res = conn.getresponse() except (HTTPException, socket.timeout, socket.error): return 0 else: return res.status
def __init__(self, response): raw_headers = response.getheaders() headers = dict() for k, v in raw_headers: headers[k.lower()] = v self.request_id = headers.get('x-cas-requestid') self.status = response.status sys.stdout.write('====== debug: error: receive status: %s\n' % response.status) sys.stdout.write('====== debug: error: receive headers: %s\n' % headers) content = '' try: content = response.read() body = json.loads(content) sys.stdout.write('====== debug: error: receive body: %s\n' % body) self.code = body.get('code') self.type = body.get('type') self.message = body.get('message') msg = 'Expected 2xx, got ' msg += '(%d, code=%s, message=%s, type=%s, request_id=%s)' % \ (self.status, self.code, self.message, self.type, self.request_id) except (HTTPException, ValueError): msg = 'Expected 2xx, got (%d, content=%s, request_id=%s)' % \ (self.status, content, self.request_id) super(CASServerError, self).__init__(msg)