Skip to content

Instantly share code, notes, and snippets.

@pirkla
Last active August 4, 2019 11:41
Show Gist options
  • Save pirkla/d0b2790050b638b6c16022a13b650713 to your computer and use it in GitHub Desktop.
Save pirkla/d0b2790050b638b6c16022a13b650713 to your computer and use it in GitHub Desktop.
A sample python script using argParse
#!/usr/bin/env python3
import asyncio
import aiohttp
from aiohttp import ClientSession
import argparse
import re
class StoreDictKeyPair(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
self.myDict = {}
super(StoreDictKeyPair, self).__init__(option_strings, dest, nargs=nargs, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
k,v = values.split(":")
self.myDict.update({k:v})
setattr(namespace, self.dest, self.myDict)
parser = argparse.ArgumentParser(description="[state intent]\nExample command: %(prog)s https://url.here/endpoint",
usage='use "%(prog)s --help" for more information',
formatter_class=argparse.RawTextHelpFormatter)
parser.add_argument("-u", "--user", help= "credentials in the form user:password",type=str,metavar= "")
parser.add_argument("-H", "--header",help= "additional header for request", action=StoreDictKeyPair ,default = {},metavar= "",dest='header')
parser.add_argument("-d", "--data", help="data to send with request",type=str,metavar= '')
parser.add_argument("-X","--call-type",help="request type to use", type=str,default="GET",metavar= '',choices=["GET","POST","PUT","DELETE"])
parser.add_argument("url",help="url to use",type=str)
args = parser.parse_args()
def parseURLList(url):
urlList = []
subString = re.search(r"\[(.*?)\]|\{(.*?)\}", url)
if subString == None:
urlList.append(url)
return urlList
elif subString.group(0).startswith('['):
parseStep = re.split(":",subString.group(0).strip('[]'))
nth = 1
if len(parseStep) > 1:
nth = int(parseStep[1])
parseRange = re.split("-",parseStep[0])
start = int(parseRange[0])
end = int(parseRange[1])
for x in range(start,end,nth):
modifiedUrl = re.sub(r"\[(.*?)\]",str(x),url,1)
urlList += parseURLList(modifiedUrl) or []
elif subString.group(0).startswith('{'):
parseElems = re.split(",",subString.group(0).strip('{}'))
for x in parseElems:
modifiedUrl = re.sub(r"\{(.*?)\}",x,url,1)
urlList += parseURLList(modifiedUrl) or []
return urlList
print(args)
print(parseURLList(args.url))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment