The problem on Py2 was this at line 168 well the syntax if ip := service(): is not valid in Python 2 (and the syntax of the walrus := operator was introduced only in Python 3.8).
so..
Python
def get_external_ip():
"""Get external IP using multiple fallback services"""
import requests
from subprocess import Popen, PIPE
services = [
lambda: Popen(['curl', '-s', 'ifconfig.me'], stdout=PIPE).communicate()[0].decode('utf-8').strip(),
lambda: requests.get('https://v4.ident.me', timeout=5).text.strip(),
lambda: requests.get('https://api.ipify.org', timeout=5).text.strip(),
lambda: requests.get('https://api.myip.com', timeout=5).json().get("ip", "").strip(),
lambda: requests.get('https://checkip.amazonaws.com', timeout=5).text.strip(),
]
for service in services:
try:
ip = service()
if ip:
return ip
except Exception:
continue
return None
Display More
