In my script I am using api's from another Python script. I am able to add latency but similar to add_latency there is another function to remove latency which is remove_latency. What I want to do is after add latency to interface I want to remove it when I do a cntrl c and invoke sigint.
def url(a, b, c):
url = f"http://{a}:5000/{c}?ms={b}"
return url
def main_func(url, endpoint_name):
response = requests.get(url)
if response.status_code // 100 == 2:
print('OK')
return True
else:
print(f"ERROR in {endpoint_name}. URL: {url}. Response code: {response.status_code}")
return False
def add_latency(mbxIP, latency):
return main_func(url(mbxIP, latency, 'add_latency'), add_latency)
def remove_latency(mbxIP, latency):
return main_func(url(mbxIP, latency, 'remove_latency'), remove_latency)
The above code is which I am using to add and remove latency. Below is the sighandler:
def gracefulExit(signal_number, frame):
print('Signal received....Exiting gracefully')
sys.exit(0)
def main():
signal.signal(signal.SIGINT, gracefulExit)
parser = ap.ArgumentParser()
parser.add_argument('--latency', help='Latency in ms')
parser.add_argument('--mbxip', help='MBX IP')
parser.add_argument('--addLatency', help='Add latency on interface', action='store_true')
parser.add_argument('--removeLatency', help='Remove latency from interface', action='store_true')
elif args.addLatency:
add_latency(args.mbxip, args.latency)
if signal.signal(signal.SIGINT, gracefulExit):
remove_latency(args.mbxip, args.latency)
elif args.removeLatency:
remove_latency(args.mbxip, args.latency)
if __name__ == '__main__':
try:
main()
except KeyboardInterrupt:
pass
I am not able to invoke a SIGINT and remove latency. What is happening is the latency is added and removed right away and the script is exited. I am running the script as ./script.py --addLatency --latency 25 --mbxip
What I want is once I have added the latency, the script would exit when SIGINT is invoked with control + c and would remove the latency before exiting. How can I make this happen?