Created
January 9, 2019 06:22
-
-
Save tekton/3d714222926078c42dd1c4b9e8e1b533 to your computer and use it in GitHub Desktop.
concurrent url requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import concurrent.futures | |
import urllib.request | |
import argparse | |
import json | |
import time | |
def make_call(data): | |
headers = data["headers"] | |
uri = data["uri"] | |
body = data["body"] | |
request = urllib.request.Request(uri, data=json.dumps(body).encode('utf8'), headers=headers) | |
start = time.time() | |
try: | |
with urllib.request.urlopen(request) as response: | |
elapsed = time.time() - start | |
code = response.getcode() | |
_body = response.read() | |
try: | |
j_body = json.loads(_body) | |
if "content" not in j_body: | |
print("ERROR BODY in call {}".format(data["num"])) | |
print(code, j_body) | |
elif code != 200: | |
print("ERROR CODE in call {}".format(data["num"])) | |
print(code, j_body) | |
except Exception as e: | |
print("Unable to parse body {}".format(data["num"])) | |
print(code, _body) | |
except Exception as e: | |
elapsed = time.time() - start | |
print(data["num"], elapsed, e) | |
return data["num"] | |
def helper_brain(data): | |
proc_list = [] | |
for x in range(0, data["count"]): | |
proc_list.append({"headers": data["headers"], "uri": data["uri"], "body": data["body"], "num": x}) | |
with concurrent.futures.ProcessPoolExecutor(max_workers=data["pool"]) as executor: | |
for x in executor.map(make_call, proc_list): | |
print(x) | |
def get_file_contents(file): | |
data = None | |
with open(file) as infile: | |
data = json.load(infile) | |
return data | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser() | |
parser.add_argument("--file", help="File to use for request settings", required=True) | |
args = parser.parse_args() | |
data = get_file_contents(args.file) | |
helper_brain(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment