Skip to main content

A simple IP lookup tool written in Python.

Project description

SpyIP

wakatime

A simple IP lookup tool written in Python.

SpyIP uses ip-api API to trace IP addresses. SpyIP is type annotated, unit tested, PEP8 compliant, documented and optimized for performance.

Features

  • Trace your own IP address

  • Trace your own DNS

  • Trace IP address by query

  • Trace batch IP address

  • Type annotated

  • Unit tested

  • PEP8 compliant

  • Documented

  • Optimized

Installation

pip install spyip

Usage

from spyip import trace_me, trace_dns, trace_ip, trace_ip_batch


me = trace_me() # trace your own IP
print(me.json(indent=4)) # print as JSON with indent


dns = trace_dns() # trace your own DNS
print(dns.json()) # print as JSON


# trace by IP address (facebook.com)
ip = trace_ip(query="31.13.64.35")
print(ip.json()) # print as JSON

# set timeout
print(
    trace_ip(
        query="31.13.64.35",
        timeout=5,
    ).json()
)


# batch trace by IP address (facebook.com, google.com, github.com, microsoft.com, ...)
batch = trace_ip_batch(
    query_list=[
        '31.13.64.35', # facebook.com
        '142.250.193.206', # google.com
        '20.205.243.166', # github.com
        '20.236.44.162', # microsoft.com
    ],
)
print(batch) # print a list of IPResponse objects. (see below)

Localization

Localized city, regionName and country can be translated to the following languages by passing lang argument to trace_me, trace_ip and trace_ip_batch functions. Default language is en (English). Here is the list of supported languages:

lang (ISO 639) description
en English (default)
de Deutsch (German)
es Español (Spanish)
pt-BR Português - Brasil (Portuguese)
fr Français (French)
ja 日本語 (Japanese)
zh-CN 中国 (Chinese)
ru Русский (Russian)

Response objects

Response objects are pydantic base models. In SpyIP we have 2 response objects respectively:

  1. IPResponse: for single IP address query
    class IPResponse(BaseModel):
        """
        Example response from API:
    
        {
            "status": "success",
            "continent": "Asia",
            "continentCode": "AS",
            "country": "India",
            "countryCode": "IN",
            "region": "DL",
            "regionName": "National Capital Territory of Delhi",
            "city": "New Delhi",
            "district": "",
            "zip": "110001",
            "lat": 28.6139,
            "lon": 77.209,
            "timezone": "Asia/Kolkata",
            "offset": 19800,
            "currency": "INR",
            "isp": "Google LLC",
            "org": "Google LLC",
            "as": "AS15169 Google LLC",
            "asname": "GOOGLE",
            "mobile": false,
            "proxy": false,
            "hosting": true,
            "query": "142.250.193.206",
        }
        """
    
        status: str = Field(..., description='Status of the request.')
        continent: str = Field(..., description='Continent name.')
        continentCode: str = Field(..., description='Continent code.')
        country: str = Field(..., description='Country name.')
        countryCode: str = Field(..., description='Country code.')
        region: str = Field(..., description='Region code.')
        regionName: str = Field(..., description='Region name.')
        city: str = Field(..., description='City name.')
        district: str = Field(..., description='District name.')
        zip_: str = Field(..., description='Zip code.')
        lat: float = Field(..., description='Latitude.')
        lon: float = Field(..., description='Longitude.')
        timezone: str = Field(..., description='Timezone.')
        offset: int = Field(..., description='Offset.')
        currency: str = Field(..., description='Currency.')
        isp: str = Field(..., description='ISP name.')
        org: str = Field(..., description='Organization name.')
        as_: str = Field(..., description='AS number and name.')
        asname: str = Field(..., description='AS name.')
        mobile: bool = Field(..., description='Mobile status.')
        proxy: bool = Field(..., description='Proxy status.')
        hosting: bool = Field(..., description='Hosting status.')
        query: str = Field(..., description='IP address.')
    
  2. DNSResponse: for DNS query
    class DNSResponse(BaseModel):
        """
        Example response from API:
        "dns": {
            "ip": "74.125.73.83",
            "geo": "United States - Google"
        }
        """
    
        ip: str = Field(..., description='IP address.')
        geo: str = Field(..., description='Geo location.')
    

In batch query, trace_ip_batch returns a list of IPResponse objects. You can just iterate over the list and use as you need.

Exceptions

SpyIP has 3 custom exceptions:

  • TooManyRequests - raised when you exceed the API rate limit.
  • ConnectionTimeout - raised when connection times out.
  • StatusError - raised when API returns an error status.

Tests

Test cases are located in tests directory. You can run tests with the following command:

python -m unittest discover -s tests

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. If you have any idea, suggestion or question, feel free to open an issue. Please make sure to update tests as appropriate.

License

This project is licensed under the terms of the MIT license.

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

spyip-0.1.0.tar.gz (7.1 kB view hashes)

Uploaded Source

Built Distribution

spyip-0.1.0-py3-none-any.whl (8.8 kB view hashes)

Uploaded Python 3

Supported by

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