def headrequest(self, path): """ Send an HTTP head request to the validator. Return the result code. """ url = urlparse.urljoin(self._base_url, path) LOGGER.debug('get content from url <%s>', url) try: request = urllib2.Request(url) request.get_method = lambda: 'HEAD' opener = urllib2.build_opener(self._proxy_handler) response = opener.open(request, timeout=30) except urllib2.HTTPError as err: # in this case it isn't really an error since we are just looking # for the status code return err.code except urllib2.URLError as err: LOGGER.warn('operation failed: %s', err.reason) raise MessageException('operation failed: {0}'.format(err.reason)) except: LOGGER.warn('no response from server') raise MessageException('no response from server') return response.code
def postmsg(self, msgtype, info): """ Post a transaction message to the validator, parse the returning CBOR and return the corresponding dictionary. """ data = dict2cbor(info) datalen = len(data) url = self._base_url + msgtype LOGGER.debug('post transaction to %s with DATALEN=%d, DATA=<%s>', url, datalen, data) try: request = urllib2.Request(url, data, { 'Content-Type': 'application/cbor', 'Content-Length': datalen }) opener = urllib2.build_opener(self._proxy_handler) response = opener.open(request, timeout=10) except urllib2.HTTPError as err: LOGGER.warn('operation failed with response: %s', err.code) raise MessageException( 'operation failed with response: {0}'.format(err.code)) except urllib2.URLError as err: LOGGER.warn('operation failed: %s', err.reason) raise MessageException('operation failed: {0}'.format(err.reason)) except: LOGGER.warn('no response from server') raise MessageException('no response from server') content = response.read() headers = response.info() response.close() encoding = headers.get('Content-Type') if encoding == 'application/json': value = json2dict(content) elif encoding == 'application/cbor': value = cbor2dict(content) else: LOGGER.info('server responds with message %s of type %s', content, encoding) return None LOGGER.debug(pretty_print_dict(value)) return value
def test_sawtooth_stats(self): try: keys = 10 rounds = 2 txn_intv = 0 print("Testing transaction load.") test = IntKeyLoadTest() urls = self.urls self.assertEqual(5, len(urls)) test.setup(self.urls, keys) test.run(keys, rounds, txn_intv) test.validate() try: stats_config_dict = self.stats_config_dict() run_stats(self.urls[0], config_opts=None, config_dict=stats_config_dict) except MessageException as e: raise MessageException('cant run stats print: {0}'.format(e)) finally: print("No Validators need to be stopped")
def _geturl(self, url, verbose=True, timeout=30): """ Send an HTTP get request to the validator. If the resulting content is in JSON or CBOR form, parse it & return the corresponding dictionary. """ if verbose: LOGGER.debug('get content from url <%s>', url) try: request = urllib2.Request(url, headers=self.GET_HEADER) opener = urllib2.build_opener(self.proxy_handler) response = opener.open(request, timeout=timeout) except urllib2.HTTPError as err: if verbose: LOGGER.error( 'peer operation on url %s failed ' 'with response: %d', url, err.code) raise MessageException('operation failed ' 'with response: {0}'.format(err.code)) except urllib2.URLError as err: if verbose: LOGGER.error('peer operation on url %s failed: %s', url, err.reason) raise MessageException('operation failed: {0}'.format(err.reason)) except: if verbose: LOGGER.error('no response from peer server for url %s; %s', url, sys.exc_info()[0]) raise MessageException('no response from server') content = response.read() headers = response.info() response.close() encoding = headers.get('Content-Type') if encoding == 'application/json': return json2dict(content) elif encoding == 'application/cbor': return cbor2dict(content) else: return content
def test_sawtooth_stats_twisted_thread(self): try: self.ssth.test_run(self.urls, config_opts=None, config_dict=None) except MessageException as e: raise MessageException('stats error: {0}'.format(e)) finally: print "No Validators need to be stopped"
def getmsg(self, path, timeout=10): """ Send an HTTP get request to the validator. If the resulting content is in JSON form, parse it & return the corresponding dictionary. """ url = urlparse.urljoin(self._base_url, path) LOGGER.debug('get content from url <%s>', url) try: request = urllib2.Request(url) opener = urllib2.build_opener(self._proxy_handler) response = opener.open(request, timeout=timeout) except urllib2.HTTPError as err: LOGGER.warn('operation failed with response: %s', err.code) self._print_error_information_from_server(err) raise MessageException( 'operation failed with response: {0}'.format(err.code)) except urllib2.URLError as err: LOGGER.warn('operation failed: %s', err.reason) raise MessageException('operation failed: {0}'.format(err.reason)) except: LOGGER.warn('no response from server') raise MessageException('no response from server') content = response.read() headers = response.info() response.close() encoding = headers.get('Content-Type') if encoding == 'application/json': return _json2dict(content.decode()) elif encoding == 'application/cbor': return _cbor2dict(content) else: return content
def postmsg(self, msgtype_name, info): """ Post a transaction message to the validator, parse the returning CBOR and return the corresponding dictionary. """ data = _dict2cbor(info) datalen = len(data) url = urlparse.urljoin(self._base_url, msgtype_name) LOGGER.debug('post transaction to %s with DATALEN=%d, DATA=<%s>', url, datalen, data) try: request = urllib2.Request(url, data, { 'Content-Type': 'application/cbor', 'Content-Length': datalen }) if self._cookie: request.add_header('cookie', self._cookie) opener = urllib2.build_opener(self._proxy_handler) response = opener.open(request, timeout=10) if not self._cookie: self._cookie = response.headers.get('Set-Cookie') except urllib2.HTTPError as err: content = err.read() if content is not None: headers = err.info() encoding = headers.get('Content-Type') if encoding == 'application/json': value = _json2dict(content) elif encoding == 'application/cbor': value = _cbor2dict(content) else: LOGGER.warn('operation failed with response: %s', err.code) raise MessageException( 'operation failed with response: {0}'.format(err.code)) LOGGER.warn('operation failed with response: %s %s', err.code, str(value)) if "errorType" in value: if value['errorType'] == "InvalidTransactionError": raise InvalidTransactionError( value['error'] if 'error' in value else value) else: raise MessageException(str(value)) else: raise MessageException( 'operation failed with response: {0}'.format(err.code)) except urllib2.URLError as err: LOGGER.warn('operation failed: %s', err.reason) raise MessageException('operation failed: {0}'.format(err.reason)) except: LOGGER.warn('no response from server') raise MessageException('no response from server') content = response.read() headers = response.info() response.close() encoding = headers.get('Content-Type') if encoding == 'application/json': value = _json2dict(content) elif encoding == 'application/cbor': value = _cbor2dict(content) else: LOGGER.info('server responds with message %s of type %s', content, encoding) return None LOGGER.debug(_pretty_print_dict(value)) return value
def _posturl(self, url, info, encoding='application/cbor'): """ Post a transaction message to the validator, parse the returning CBOR and return the corresponding dictionary. """ if encoding == 'application/json': data = dict2json(info) elif encoding == 'application/cbor': data = dict2cbor(info) else: LOGGER.error('unknown request encoding %s', encoding) return None datalen = len(data) LOGGER.debug('post transaction to %s with DATALEN=%d, DATA=<%s>', url, datalen, data) try: request = urllib2.Request(url, data, { 'Content-Type': 'application/cbor', 'Content-Length': datalen }) opener = urllib2.build_opener(self.proxy_handler) response = opener.open(request, timeout=10) except urllib2.HTTPError as err: LOGGER.error('peer operation on url %s failed with response: %d', url, err.code) raise MessageException( 'operation failed with response: {0}'.format(err.code)) except urllib2.URLError as err: LOGGER.error('peer operation on url %s failed: %s', url, err.reason) raise MessageException('operation failed: {0}'.format(err.reason)) except NameError as err: LOGGER.error('name error %s', err) raise MessageException('operation failed: {0}'.format(url)) except: LOGGER.error('no response from peer server for url %s; %s', url, sys.exc_info()[0]) raise MessageException('no response from server') content = response.read() headers = response.info() response.close() encoding = headers.get('Content-Type') if encoding == 'application/json': value = json2dict(content) elif encoding == 'application/cbor': value = cbor2dict(content) else: LOGGER.info('server responds with message %s of unknown type %s', content, encoding) value = OrderedDict() return value