kboghe / NordVPN-switcher

Rotate between different NordVPN servers with ease. Works both on Linux and Windows without any required changes to your code!
https://pypi.org/project/nordvpn-switcher/
189 stars 51 forks source link

urllib #26

Open Mariusz89B opened 3 years ago

Mariusz89B commented 3 years ago

Some issue when using urllib on py3, why not use requests library instead? Also there is problem with the website https://ident.me/, when connection fails service will not be kept running, it need to choose second option if first one fails.

Could this maybe be a solution?

import sys

if sys.version_info[0] > 2:
    from requests.exceptions import HTTPError, ConnectionError, Timeout, RequestException
else:
    from requests import HTTPError, ConnectionError, Timeout, RequestException

def get_ip():
    headers = set_headers(user_agent_rotator)
    ip_check_websites = ['http://ip4only.me/api/',"https://ident.me/"]
    ip_check_websites =ip_check_websites = ['http://ip4only.me/api/',"https://ident.me/"]
    website_pick = random.choice(ip_check_websites)
    ip = None

    try:
        request_currentip = requests.get(website_pick, headers=headers)
        ip = request_currentip.text
        if website_pick == 'http://ip4only.me/api/':
            ip = re.search("IPv4,(.*?),Remaining", ip).group(1)

    except HTTPError as e:
        rotate_VPN()

    except ConnectionError as e:
        rotate_VPN()

    except Timeout as e:
        rotate_VPN()

    except RequestException as e:
        rotate_VPN()

    except:
        rotate_VPN()

    return ip