Skip to content

Instantly share code, notes, and snippets.

@Tremus
Last active June 26, 2018 09:43
Show Gist options
  • Select an option

  • Save Tremus/e616729a920df34d371fc5ca18c1aef2 to your computer and use it in GitHub Desktop.

Select an option

Save Tremus/e616729a920df34d371fc5ca18c1aef2 to your computer and use it in GitHub Desktop.

Revisions

  1. Tremus renamed this gist Jun 26, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions guarantee_http_response.py → handle_bad_connection.py
    Original file line number Diff line number Diff line change
    @@ -17,7 +17,7 @@ def internet_connection(host="8.8.8.8", port=53, timeout=3):
    print(e)
    return False

    class guarantee_http_response(object):
    class handle_bad_connection(object):
    def __init__(self, func):
    self.func = func

    @@ -47,7 +47,7 @@ def wait_for_connection_and_retry(self, *args, **kwargs):

    # Use case
    class SuperMegaScraper(MegaScraper)
    @guarantee_http_response
    @handle_bad_connection
    def super_scrape(self):
    # ...
    pass
  2. Tremus created this gist Jun 26, 2018.
    53 changes: 53 additions & 0 deletions guarantee_http_response.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    from time import sleep
    import socket


    def internet_connection(host="8.8.8.8", port=53, timeout=3):
    """
    Host: 8.8.8.8 (google-public-dns-a.google.com)
    OpenPort: 53/tcp
    Service: domain (DNS/TCP)
    Source: https://stackoverflow.com/questions/3764291/checking-network-connection
    """
    try:
    socket.setdefaulttimeout(timeout)
    socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
    return True
    except Exception as e:
    print(e)
    return False

    class guarantee_http_response(object):
    def __init__(self, func):
    self.func = func

    def __get__(self, instance, owner):
    self.instance = instance
    return self.__call__

    def __call__(self, *args, **kwargs):
    """
    On a bad internet connection, your function may return None.
    Returning None however, may be an intended part of your function.
    When your response = None, check the internet connection.
    """
    response = self.func(self.instance, *args, **kwargs)
    if not response and not internet_connection():
    response = self.wait_for_connection_and_retry(*args, **kwargs)

    return response

    def wait_for_connection_and_retry(self, *args, **kwargs):
    print ('\nConnection error. Retrying...!\n')
    while not internet_connection():
    sleep(10)
    print ('\nSuccessully reconnected!\n')
    return self.func(self, *args, **kwargs)


    # Use case
    class SuperMegaScraper(MegaScraper)
    @guarantee_http_response
    def super_scrape(self):
    # ...
    pass