Skip to content

Instantly share code, notes, and snippets.

@tekton
Created January 9, 2019 06:22
Show Gist options
  • Save tekton/3d714222926078c42dd1c4b9e8e1b533 to your computer and use it in GitHub Desktop.
Save tekton/3d714222926078c42dd1c4b9e8e1b533 to your computer and use it in GitHub Desktop.
concurrent url requests
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