def handle(self, flow, args): if not flow.request.pretty_host(hostheader=True).endswith(self._host): return r = str(flow.request.path).replace("/", "-") rq_path = r[1:] if args.verbosity >= 1: t = os.getcwd() + "/api_calls.txt" # if not os.access(p, os.F_OK): # os.mkdir(p) # data_path = p + str(flow.request.path) + "_" + time.strftime("%m%d%Y-%H%M%S") + ".json" t_file = open(t, 'a') print flow.request.path print >> t_file, flow.request.path # # print >> test_file, json.dumps(data, indent=4, sort_keys=True) t_file.close() # print flow.request.path # print if self.should_ignore(flow.request.path): return with decoded(flow.response): handlers = self.get_handlers(flow.request.path) if handlers: data = json_decode(flow.response.content) if args.verbosity >= 2: p = os.getcwd() + "/data_dump/" + rq_path + "/" if not os.access(p, os.F_OK): os.mkdir(p) data_path = p + rq_path + "_" + time.strftime("%m%d%Y-%H%M%S") + ".json" test_file = open(data_path, 'w') print >> test_file, json.dumps(data, indent=4, sort_keys=False) test_file.close() # test_file = open(data_path, 'w') # print >> test_file, json.dumps(data, indent=4, sort_keys=False) # test_file.close() # # print dump_json(data) for func in handlers: if func in self._wants_flow: func(data, flow) else: func(data) else: if args.verbosity >= 3: data = json_decode(flow.response.content) p = os.getcwd() + "/data_dump/" + rq_path + "/" if not os.access(p, os.F_OK): os.mkdir(p) data_path = p + rq_path + "_" + time.strftime("%m%d%Y-%H%M%S") + ".json" test_file = open(data_path, 'w') # print >> test_file, "//" + str(flow.request.path) print >> test_file, json.dumps(data, indent=4, sort_keys=False) test_file.close()
def enter_dungeon(data, flow): global args start_time = time.time() dungeon_request = flow.request.content headers = flow.request.headers enter_url = flow.request.url leave_url = enter_url.replace("enter_dungeon", "leave_dungeon") name = data.get("enemy", dict(name="???", memory_factor="0")).get("name") resp = None while args.find not in name: if time.time() - start_time > 28: ## times out after 30 seconds print "Took too long! Entering the dungeon so you don't get kicked out." return print "Opponent is {0}, retrying...".format(name) resp = requests.post(leave_url, headers=headers, data=dungeon_request) if resp.status_code != requests.codes.ok: resp.raise_for_status() resp = requests.post(enter_url, headers=headers, data=dungeon_request) if resp.status_code != requests.codes.ok: resp.raise_for_status() data = json_decode(resp.content) name = data.get("enemy", dict(name="???", memory_factor="0")).get("name") print "Found {0}! Entering the dungeon now...".format(name) if resp is not None: flow.response.content = resp.content
def handle(self, flow, args): if not flow.request.pretty_host(hostheader=True).endswith(self._host): return if args.verbosity >= 1: print flow.request.path if self.should_ignore(flow.request.path): return with decoded(flow.response): handlers = self.get_handlers(flow.request.path) if handlers: data = json_decode(flow.response.content) if args.verbosity >= 2: print dump_json(data) self.call_handlers(flow.request.path, data, flow) else: if args.verbosity >= 3: data = json_decode(flow.response.content) print dump_json(data)
def handle(self, flow, args): request = flow.request response = flow.response pretty_host = request.pretty_host(hostheader=True) if not pretty_host.endswith(self._host): print("\nSkipping unknown host: {}".format(pretty_host)) print( "{}".format( request._assemble_first_line() ) ) return # Show request. print( "\n{}".format( request._assemble_first_line() ) ) content = request.content if content: print(content) # For full request, including headers, use line below instead: # print( request.assemble() ) # if self.should_ignore(request.path): # return with decoded(response): data = json_decode(response.content) if data: if args.verbosity == 1: print_json(data) elif args.verbosity == 2: path_as_filename = ('-').join(request.path.strip('/').split('/')) need_unique_filename = True count = 0 while need_unique_filename: # Create a random 8-digit string. uniqueID = ''.join(random.choice(string.digits) for i in range(8)) unique_filename = path_as_filename + '-' + uniqueID + '.txt' # If proposed filename already exists, then try again. Else, save the data. if os.path.isfile(unique_filename): print( "\nUnexpected: File {} already exists. Making new ID...".format(unique_filename) ) count += 1 if count >= 3: print("\nVery unexpected: Found a duplicate file 3 times in a row. Break time.") break else: save_request_and_response(request, data, unique_filename) need_unique_filename = False # if verbosity = 3 and its path has been registered (via csv file), then ignore it # elif args.verbosity == 3: print("\nWarning: Verbosity 3 not implemented yet.")