I hope it's ok to ask for what may be basic help. I am a hobbyist and newish to Python, but used to code in COBOL many years ago. I'd like some help on how you would re-write the structure of my code.
I like to create code to protect my home network via my router and have written some code (below) that successfully reads and processes a file from a website. The file is a list of IP addresses of tor exit points. I can only download it once a day from the remote website so need to save it the first time so I can run the script more than once if I need to. The script has been running successfully for many months.
The code works but I recognise that is it completely inelegant. It drops in and out of if statement and doesn't save the file until the end. I'm hoping someone could give me a bit of help to structure the code in a more elegant pythonesque way. Should I be using Try statements to catch the != 200, but how big should the try statement be?
I realise this is only a snippet, but a response giving some structured pseudocode that I can copy would be very helpful.
TIA, Charles
I like to create code to protect my home network via my router and have written some code (below) that successfully reads and processes a file from a website. The file is a list of IP addresses of tor exit points. I can only download it once a day from the remote website so need to save it the first time so I can run the script more than once if I need to. The script has been running successfully for many months.
The code works but I recognise that is it completely inelegant. It drops in and out of if statement and doesn't save the file until the end. I'm hoping someone could give me a bit of help to structure the code in a more elegant pythonesque way. Should I be using Try statements to catch the != 200, but how big should the try statement be?
I realise this is only a snippet, but a response giving some structured pseudocode that I can copy would be very helpful.
TIA, Charles
############################################################################## # Get tor file once a day from the internet and save it for another run ############################################################################## print('>>tor') count = 0 AddressFile = datePrefix + '-torlist.txt' if os.path.isfile('html/' + AddressFile): url = 'http://192.168.xxx.xxx/' + AddressFile print('>>tor from file') else: url = 'https://www.xxxxxxxxx/torlist/?exit' print('>>tor from web') response = requests.get(url) if response.status_code != 200: print('Failed to get data:', response.status_code) else: wrapper = csv.reader(response.text.strip().split('\n'), delimiter=';') for record in wrapper: if record[0][0:1].isnumeric() and len(record[0]) != 39: OutText = record[0] + "/32" ip_list.append(IPNetwork(OutText)) count += 1 print('>>tor: ' + str(count)) if not os.path.isfile('html/' + AddressFile): f = open('html/' + AddressFile, "w") f.write(str(response.text)) f.close()