def campaign(args): if args.version: print(bombard.__name__, bombard.version()) with open( os.path.join(os.path.dirname(bombard.__file__), 'LICENSE.txt'), 'r') as license: print(license.readline()) return if args.quiet: level = logging.WARNING elif args.verbose: level = logging.DEBUG else: level = logging.INFO setup_logging(level=level, log_file_name=args.log) if args.init: init(args) return campaign_file_name = get_campaign_file_name(args) if os.path.isdir(campaign_file_name): show_folder(campaign_file_name) elif not (os.path.isfile(campaign_file_name) or args.init): print(red(f'\nCannot find campaign file "{args.file_name}"\n')) else: start_campaign(args, yaml.load(open(campaign_file_name, 'r')))
def status_coloured(self, status: Union[str, int]) -> str: """ Paint ok status as green and overload as red using terminal control codes. If status has special string value (EXCEPTION_STATUS) paint it red. """ if status in self.ok: return green(str(status)) if status in self.overload or status == EXCEPTION_STATUS: return dark_red(str(status)) return red(str(status))
def get_campaign_file_name(args: Any) -> str: if args.example is not None: if args.file_name != CAMPAIGN_FILE_NAME: print( red(f'--example option found - ignoring campaign file name "{args.file_name}".' )) args.file_name = EXAMPLES_PREFIX + args.example if not args.file_name.endswith(".yaml"): args.file_name += ".yaml" if args.examples: if args.file_name != CAMPAIGN_FILE_NAME: print( red(f'--examples option found - ignoring campaign file name "{args.file_name}".' )) if args.example is not None: print( red("Please do not use --example and --examples options simultaneously." )) args.file_name = EXAMPLES_PREFIX return expand_relative_file_name(args.file_name)
def worker(self, thread_id: int, job: Dict[str, Any]) -> None: """ Thread callable. Strike ammo from queue. """ try: # setup logging ASAP and as safe as possible if isinstance(job, dict): request = job.get("request", {}) ammo_id = job.get("id", "") ammo_name = request.get("name", "") else: request = {} ammo_id = None ammo_name = None request_logging.sending(thread_id, ammo_id, ammo_name) pretty_url = "" # we use it in `except` try: job = apply_supply_dict(job, dict(self.supply, **job["supply"])) url = request.get("url", "") method = request["method"] if "method" in request else "GET" body = json.dumps( request["body"]) if "body" in request else None headers = self.get_headers(request, body is not None) pretty_url = self.beautify_url(url, method, body) log.debug( # pylint: disable=logging-not-lazy f"Bomb to drop:\n{pretty_url}" + ("\n{body}" if body is not None else "")) if self.args.quiet and ammo_id in self.show_request: print( f"{self.show_request[ammo_id].format(id=ammo_id):>15}\r", end="") log.info(pretty_url) start_ns = time_ns() status: Union[str, int] if self.args.dry: status, resp = list(self.ok)[0], json.dumps( request.get("dry")) else: status, resp = http_request(url, method, headers, body, self.args.timeout) request_logging.receiving() self.process_resp(job, status, resp, time_ns() - start_ns, len(resp)) self.resp_count += 1 if self.args.quiet and self.resp_count in self.show_response: print( f"{self.show_response[self.resp_count].format(id=self.resp_count):>15}\r", end="", ) log.info( self.status_coloured(status) + f" ({pretty_sz(len(resp))}) " + pretty_url + " " + (red(resp) if status == EXCEPTION_STATUS else "")) except Exception as e: log.info( # pylint: disable=logging-not-lazy pretty_url + " " + red(str(e)), exc_info=True) finally: request_logging.main_thread()
def pretty_ns(self, elapsed_ns: int, paint: bool = True): result = pretty_ns(elapsed_ns, self.time_units) if elapsed_ns > self.time_threshold_ns and paint: return red(result) else: return result