Return the public IP of this host
()
| 18 | |
| 19 | |
| 20 | def get_my_ip(): |
| 21 | """ Return the public IP of this host """ |
| 22 | urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) |
| 23 | http = urllib3.PoolManager(cert_reqs='CERT_NONE') |
| 24 | try: |
| 25 | external_ip = http.request('GET', 'https://ifconfig.me/ip').data |
| 26 | except Exception: |
| 27 | try: |
| 28 | external_ip = http.request('GET', 'https://ident.me').data |
| 29 | except Exception: |
| 30 | external_ip = '127.0.0.1' |
| 31 | |
| 32 | if isinstance(external_ip, bytes): |
| 33 | external_ip = external_ip.decode('utf-8') |
| 34 | |
| 35 | ip = ipaddress.ip_address(external_ip) |
| 36 | if isinstance(ip, ipaddress.IPv4Address): |
| 37 | return IP_ADDRESS_STRING.format(external_ip, 32) |
| 38 | elif isinstance(ip, ipaddress.IPv6Address): |
| 39 | return IP_ADDRESS_STRING.format(external_ip, 128) |
| 40 | |
| 41 | return IP_ADDRESS_STRING.format(external_ip, 32) |
| 42 | |
| 43 | |
| 44 | def _create_server(data): |