This is a tool to get public ip of your machine
Project description
python-ifconfig-me
Project Overview
This is a simple Python library to detect the current public IP of your machine.
Getting Started
Prerequisites
Python version >= 3.9
Installation
pip install python-ifconfig-me
Usage
Basic usage - Use as a tool
Show help messages:
$ ifconfig-me -h
Show the current public ip:
$ ifconfig-me
Show statistics used to determine the public IP.
$ ifconfig-me --show-statistics
{
"ip": "xxx.xxx.xxx.xxx",
"statistics": [
{
"ipObject": {
"ip": "xxx.xxx.xxx.xxx"
},
"weight": 4,
"priority": 0,
"retrievers": [
{
"url": "https://ifconfig.me/ip",
"priority": 0
},
{
"url": "https://ipecho.net/plain",
"priority": 0
},
{
"url": "https://ipinfo.io/ip",
"priority": 0
},
{
"url": "https://httpbin.org/ip",
"priority": 0
}
]
},
{
"ipObject": {
"ip": "xxx.xxx.xxx.xxx\n"
},
"weight": 3,
"priority": 0,
"retrievers": [
{
"url": "https://checkip.amazonaws.com",
"priority": 0
},
{
"url": "https://icanhazip.com",
"priority": 0
},
{
"url": "https://ifconfig.co/ip",
"priority": 0
}
]
}
]
}
xxx.xxx.xxx.xxx
Force to return IPv4
$ ifconfig-me --ipv4
Force to return IPv6
$ ifconfig-me --ipv6
Prefer ipv4 over ipv6. By default, if an IPv4 address and IPv6 address are both detected and have the same weight (i.e. the same number of services detected them), the IPv4 address is returned. This option forces the IPv6 address to be returned in this case.
Note: This option only takes effect when both an IPv4 address and an IPv6 address have the same weight.
$ ifconfig-me --prefer-ipv6
Use --logLevel to set the log level. The default log level is ERROR.
Advanced usage - Use as a library
getPublicIPAsync and getPulicIP
There are two versions of the getPublicIP function: synchronous and asynchronous.
Async version:
import asyncio
from python_ifconfig_me import getPublicIPAsync
asyncio.run(getPublicIPAsync())
Sync version:
from python_ifconfig_me import getPublicIP
public = getPublicIP()
The sync version is just a wrapper of the async version. If possible, use the async version because it is more efficient.
import asyncio
from python_ifconfig_me import getPublicIPAsync, GetPublicIPOptions
options = GetPublicIPOptions(
return_statistics=True
)
asyncio.run(getPublicIPAsync(options))
Use retrievers
You can pass retrievers to the getPublicIPAsync/getPublicIP function. A retriever follows the IPRetriever protocol. You can implement your own retriever by inheriting the IPRetriever class.
import asyncio
from python_ifconfig_me import getPublicIPAsync, GetPublicIPOptions
from python_ifconfig_me.core.ipretriever.simpleTextIPRetriever import SimpleTextIPRetriever
options = GetPublicIPOptions(
return_statistics=True
)
retrievers = [
SimpleTextIPRetriever("https://ifconfig.me/ip"),
]
asyncio.run(getPublicIPAsync(options, retrievers))
If you want to add your retriever to the default retrievers, you can use the DEFAULT_IP_RETRIEVERS variable.
import asyncio
from python_ifconfig_me import getPublicIPAsync, GetPublicIPOptions
from python_ifconfig_me.core.ipretriever.simpleTextIPRetriever import SimpleTextIPRetriever
from python_ifconfig_me.core.ipretriever import DEFAULT_IP_RETRIEVERS
options = GetPublicIPOptions(
return_statistics=True
)
retrievers = DEFAULT_IP_RETRIEVERS + [
SimpleTextIPRetriever("https://ifconfig.me/ip"),
]
asyncio.run(getPublicIPAsync(options, retrievers))
How this project works
The idea behind this library is pretty simple: majority voting among multiple third-party public ip detection services.
As of now, the following services are configured to be used for detention:
- https://checkip.amazonaws.com
- https://icanhazip.com
- https://ifconfig.co/ip
- https://ifconfig.me/ip
- https://ipecho.net/plain
- https://ipinfo.io/ip
- https://httpbin.org/ip
- https://api.ipify.org
LICENSE
The project is licensed under the GPL license. For more information, please refer to the LICENSE file.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file python_ifconfig_me-0.0.2.tar.gz.
File metadata
- Download URL: python_ifconfig_me-0.0.2.tar.gz
- Upload date:
- Size: 20.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d4bc3291d0ab366afd28cde4602f71dd011f8d38fb6d50163ee2baa17b06fc9a
|
|
| MD5 |
876ae5fe4dac3aa63fd320ebe61fc61d
|
|
| BLAKE2b-256 |
9abf6752ec12bb4e7e4b7d6574b257dd9829dcfe388b0241252a92eb982a2435
|
File details
Details for the file python_ifconfig_me-0.0.2-py3-none-any.whl.
File metadata
- Download URL: python_ifconfig_me-0.0.2-py3-none-any.whl
- Upload date:
- Size: 23.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ca2e7d9e7433027c3c8c19221980483768f181f8caa8eedb463425dc87fdd07
|
|
| MD5 |
1d255e74105030e5913c1de4e2ce7e46
|
|
| BLAKE2b-256 |
92f50d3a7aa4ebfcf5e0bae53ca944b72ce4d420179456b676f44446c5ee30b8
|