Mar-26-2018, 11:39 AM
Thanks Larz60+,
Well I guess to fetch a network-page its good. My url starts a download (csv file), so I cant fetch it as you do.
So maybe I'm doing it the right way.
Would be fun to hear what others think about the solution.
Well I guess to fetch a network-page its good. My url starts a download (csv file), so I cant fetch it as you do.
So maybe I'm doing it the right way.
Would be fun to hear what others think about the solution.
(Mar-24-2018, 08:53 PM)Larz60+ Wrote: I wrote this a while back and it works for me:
GetUrl.py
import requests import socket class GetUrl: def __init__(self): self.ok_status = 200 self.response = None def check_availability(self): self.internet_available = False if socket.gethostbyname(socket.gethostname()) != '127.0.0.1': self.internet_available = True return self.internet_available def fetch_url(self, url, bin=False): self.response = None if self.check_availability(): try: if bin: self.response = requests.get(url, stream=True, allow_redirects=False, timeout=3) self.response.raise_for_status() else: self.response = requests.get(url, allow_redirects=False, timeout=3) self.response.raise_for_status() except requests.exceptions.HTTPError as errh: print("Http Error:", errh) except requests.exceptions.ConnectionError as errc: print("Error Connecting:", errc) except requests.exceptions.Timeout as errt: print("Timeout Error:", errt) except requests.exceptions.RequestException as err: print("OOps: Something Else", err) else: print("Please check network connection and try again") return self.response def testit(): gu = GetUrl() url = 'https://www.google.com/' page = gu.fetch_url(url) if page is not None: if page.status_code == gu.ok_status: print(page.text) else: print("Problem downloading page") if __name__ == '__main__': testit()So to use in another program: