Skip to main content

Fast, fault-tolerant public IP address retrieval from Python or CLI.

Project description

MyOrigin

Fast, fault-tolerant public IP address retrieval from Python or CLI.

The primary goal of this project is to find the public IP address (sometimes called an external IP address) reliably. This means it works with or without UPnP, dual NAT, DNS manipulation by the ISP, a VPN, or any single public web API or STUN server. It simultaneously queries multiple external sources at random from a long list, recovers gracefully from failed queries, and does not return an IP address unless all the responses agree.

Installation

pip install myorigin

Command line usage

$ myorigin -v
08:54:32.904 INFO requests (need 2 matches):
08:54:33.552 INFO     http://zx2c4.com/ip → 88.123.8.180 (640 ms; 33 of 34 succeeded)
08:54:33.743 INFO     https://myip.dnsomatic.com/ → 429 Too Many Requests (30 of 35 succeeded)
08:54:34.573 INFO     http://ip.websupport.sk/ → 88.123.8.180 (814 ms; 34 of 34 succeeded)
08:54:34.584 INFO IP found: 88.123.8.180 (2 successes, 1 failures)
88.123.8.180
$ 
$ myorigin --help
usage: myorigin [-h] [-t TIMEOUT] [--minimum-match MINIMUM_MATCH]
                [--overkill OVERKILL] [--majority-ratio MAJORITY_RATIO]
                [--max-failures MAX_FAILURES]
                [--max-connections MAX_CONNECTIONS] [--dbfile DBFILE]
                [--show-api-providers] [-4] [-6] [-l LOGFILE] [-q] [-v]

options:
  -h, --help                         show this help message and exit
  -t TIMEOUT, --timeout TIMEOUT      approximate timeout for http and https
                                     requests in milliseconds (default: 12000)
  --minimum-match MINIMUM_MATCH      an IP address is considered valid after
                                     this number of idential responses
                                     (default: 2)
  --overkill OVERKILL                number of initial requests to make beyond
                                     minimum-match (default: 0)
  --majority-ratio MAJORITY_RATIO    minimum ratio needed to overrule a
                                     conflicting response; must be an integer;
                                     a value of 2 means 2:1, or that 6
                                     responses of 8.7.8.7 are needed to
                                     overrule 3 responses of 7.8.4.4 (default:
                                     3)
  --max-failures MAX_FAILURES        maximum number of failed requests allowed
                                     (default: 10)
  --max-connections MAX_CONNECTIONS  maximum number of simultaneous network
                                     connections allowed (default: 10)
  --dbfile DBFILE                    path for database file ('-' for memory-
                                     only; default:
                                     ~/.config/myorigin/data.sqlite)
  --show-api-providers               display the database of IP address API
                                     providers in a human-readable form and
                                     exit
  -4, --ipv4                         use IPv4 only; note this or --ipv6 is
                                     highly recommended if both IPv4 and IPv6
                                     are available, in order to avoid wasteful
                                     network traffic and unpredictable results
                                     (sometimes --minimum-match IPv4 addresses
                                     will be received first, and sometimes
                                     IPv6 will win)
  -6, --ipv6                         use IPv6 only
  -l LOGFILE, --logfile LOGFILE      path for log file (default: write to
                                     STDERR)
  -q, --quiet                        silence warning messages
  -v, --verbose                      increase verbosity
$ 

Library import usage

>>> import myorigin
>>> args = myorigin.MyoriginArgs()
>>> args.minimum_match = 4
>>> myorigin.my_ip(args)
'88.123.8.180'
>>> 
>>> args.exception_level = 2
>>> args.ip_version = 6  # but there is no IPv6 on this LAN
>>> try:
...     myorigin.my_ip(args)
... except myorigin.NetworkError as e:
...     print(f"got error: {e}")
... 
got error: 10 requests failed; giving up
>>> 

Features

  • retrieves your IP address from any of 135 IP address providers
  • confirms the correct IP address by checking muliple providers (default 2)
  • recovers from failures by making additional requests of other providers
  • keeps a record of past successes and prioritizes the fastest and most reliable providers from your location
  • makes simultaneous IP address requests
  • supports http, https, IPv4, IPv6

Limitations

  • has only been tested on Ubuntu Linux, though, being 100% Python, this should work on Windows, macOS, other Linux and BSDs, etc.
  • has not been field-tested

Similiar projects

  • Go External IP: "a Golang library to get your external ip from multiple services"
  • gip: "a command-line tool to get global IP address"; written in Rust
  • Discovering public IP programmatically: Stack Overflow discussion (16 answers)
  • PyNAT: "Discover external IP addresses and NAT topologies using STUN"
  • pubip: "get public IP address"; written in Go

Did you find a mistake or have a suggestion? With a GitHub account, it's easy to suggest changes.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

myorigin-0.4.0.tar.gz (35.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

myorigin-0.4.0-py3-none-any.whl (34.7 kB view details)

Uploaded Python 3

File details

Details for the file myorigin-0.4.0.tar.gz.

File metadata

  • Download URL: myorigin-0.4.0.tar.gz
  • Upload date:
  • Size: 35.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.4

File hashes

Hashes for myorigin-0.4.0.tar.gz
Algorithm Hash digest
SHA256 4e15e66624a89d4f4d79b321080e46f7d9defafb2c4df052072b556058f43bf5
MD5 133ffad21cfa6ea41eaf5fb2a696f03b
BLAKE2b-256 3d2ba5e638706c04604be44c436a4c83acfde097d329e4a45d55cadadab99d90

See more details on using hashes here.

File details

Details for the file myorigin-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: myorigin-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 34.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 colorama/0.4.4 importlib-metadata/4.6.4 keyring/23.5.0 pkginfo/1.8.2 readme-renderer/34.0 requests-toolbelt/0.9.1 requests/2.25.1 rfc3986/1.5.0 tqdm/4.57.0 urllib3/1.26.5 CPython/3.10.4

File hashes

Hashes for myorigin-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e632a845b7973403a98c78ff68da03c852e8d3e632560d04aea10932d4ae6fdd
MD5 391dc861c8f0604933bd2c8b7ee32942
BLAKE2b-256 8ecca495b52c10dfb37a02f3f26739175ef60efbb13ba4a8274d225420fd489a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page