How to get external IPv6 address in Python

Also see:How to get external IPv4 address in Python

You can use the free IPify service together with requests to get your current external IPv6 address. Note that this will only work if IPv6 is enabled on the host running the script, and it has a valid IPv6 configuration. Most notably, on Docker containers it will typically only work in network_mode: host:

#!/usr/bin/env python3
import requests

def get_current_ipv6():
    """Get the current external IPv6 address or return None if no connection to the IPify service is possible"""
    try:
        return requests.get("https://api6.ipify.org", timeout=5).text
    except requests.exceptions.ConnectionError as ex:
        return None

# Usage example
print(get_current_ipv6()) # Prints e.g. 2a01:4f9:c010:278::1