I'm currently working on a Capstone project where my team will be building a network traffic analyzer using Python. But before we can do all that I have to build the baseline for the Command Line Interface. My goal is to make an interface that takes in arguments to initialize, but can take different arguments later from the command line while the script is still running. And so I've encountered a problem. How do I send these argparse variables to another script and change them and send them back?
I've read several StackOverflow answers regarding multiprocessing and shared variables but they never made sense to me for my case.
This is essentially what I'm trying to do:
# parser object
parser = argparse.ArgumentParser(description="A lightweight command-line based Network Traffic Analyzer")
parser.add_argument("-des", "--destination", "-dest", type=str, nargs=1, metavar="destination_ip", default=None, help="Destination IP Address")
parser.add_argument("-s", "--source", type=str, nargs=1, metavar="source_ip", default=None, help="Source IP Address")
parser.add_argument("-pr", "--protocol", type=str, nargs=1, metavar="protocol_name", default=None, help="Type of protocol")
parser.add_argument("-p", "--port", type=str, nargs=1, metavar="port_num", default=None, help="Port Number")
parser.add_argument("-d", "--date", type=int, nargs=1, metavar="date", default=None, help="Date packet was made; Syntax = MMdd")
parser.add_argument("-t", "--time", type=str, nargs=1, metavar="time", default=None, help="Time packet was made; Syntax = HHmm")
args = parser.parse_args()
As you can see, I created arguments for 6 attributes that can be found in a network packet. For now I'm only trying to print off what I'm inputting as a test for future implementations. So I made this function
# Print function. All arguments are sent here and printed
def printargs():
try:
while True:
if args.destination != None:
print(f"Destination IP is {args.destination[0]}")
if args.source != None:
print(f"Source IP is {args.source[0]}")
if args.protocol != None:
print(f"Protocol type is {args.protocol[0]}")
if args.port != None:
print(f"Port number is {args.port[0]}")
if args.date != None:
print(f"Date of packet capture is {args.date[0]}")
if args.time != None:
print(f"Time of packet capture is {args.time[0]}")
clock.sleep(1)
print("\n")
except:
print("Exiting Program...")
sys.tracebacklimit = 0
So my main question: How can I make it so while this while loop is running, I can have a second script running simultaneously that I can send these variables to, and change them at will so that it prints the new output right away? Do I necessarily have to use Multiprocessing or is there a better/simpler way? Am I overthinking things?
If this is an unrealistic goal, please tell me. Give me any tips at all, because my Python skills are regrettably lacking.
I hope I included enough info to go off of. Thanks for the help!