Skip to main content

DomainTools' Official Python API

Project description

domaintools

PyPI version CI Status Coverage Status License

DomainTools' Official Python API

domaintools Example

Installing the DomainTools' API

To install the API run

pip install domaintools_api --upgrade

Ideally, within a virtual environment.

Using the API

To start out create an instance of the API - passing in your credentials

from domaintools import API


api = API(USER_NAME, KEY)

Every API endpoint is then exposed as a method on the API object, with any parameters that should be passed into that endpoint being passed in as method arguments:

api.iris_enrich('domaintools.com')

You can get an overview of every endpoint that you can interact with using the builtin help function:

help(api)

If applicable, native Python looping can be used directly to loop through any results:

for result in api.iris_enrich('domaintools.com').response().get('results', {}):
    print(result['domain'])

You can also use a context manager to ensure processing on the results only occurs if the request is successfully made:

with api.iris_enrich('domaintools.com').response().get('results', {}) as results:
    print(results)

For API calls where a single item is expected to be returned, you can directly interact with the result:

profile = api.domain_profile('google.com')
title = profile['website_data']['title']

For any API call where a single type of data is expected you can directly cast to the desired type:

float(api.reputation('google.com')) == 0.0
int(api.reputation('google.com')) == 0

The entire structure returned from DomainTools can be retrieved by doing .data() while just the actionable response information can be retrieved by doing .response():

api.iris_enrich('domaintools.com').data() == {'response': { ... }}
api.iris_enrich('domaintools.com').response() == { ... }

You can directly get the html, xml, or json version of the response by calling .(html|xml|json)() These only work with non AsyncResults:

html = str(api.domain_search('google').json())
xml = str(api.domain_search('google').xml())
html = str(api.domain_search('google').html())

Besides the Iris Investigate method letting you lookup domains, it can also be used to search for domains sharing common attributes. For instance, you can pass it an IP to bring back all domains sharing that IP:

api.iris_investigate(ip='ENTER_IP_ADDRESS_HERE')

Other args you can use are below:

  • ip: Search for domains having this IP.
  • email: Search for domains with this email in their data.
  • email_domain: Search for domains where the email address uses this domain.
  • nameserver_host: Search for domains with this nameserver.
  • nameserver_domain: Search for domains with a nameserver that has this domain.
  • nameserver_ip: Search for domains with a nameserver on this IP.
  • registrar: Search for domains with this registrar.
  • registrant: Search for domains with this registrant name.
  • registrant_org: Search for domains with this registrant organization.
  • mailserver_host: Search for domains with this mailserver.
  • mailserver_domain: Search for domains with a mailserver that has this domain.
  • mailserver_ip: Search for domains with a mailserver on this IP.
  • redirect_domain: Search for domains which redirect to this domain.
  • ssl_hash: Search for domains which have an SSL certificate with this hash.
  • ssl_subject: Search for domains which have an SSL certificate with this subject string.
  • ssl_email: Search for domains which have an SSL certificate with this email in it.
  • ssl_org: Search for domains which have an SSL certificate with this organization in it.
  • google_analytics: Search for domains which have this Google Analytics code.
  • adsense: Search for domains which have this AdSense code.
  • tld: Filter by TLD. Must be combined with another parameter.
  • search_hash: Use search hash from Iris to bring back domains.

If any API call is unsuccesfull, one of the exceptions defined in domaintools.exceptions will be raised:

api.domain_profile('notvalid').data()


---------------------------------------------------------------------------
BadRequestException                       Traceback (most recent call last)
<ipython-input-3-f9e22e2cf09d> in <module>()
----> 1 api.domain_profile('google').data()

/home/tcrosley/projects/external/python_api/venv/lib/python3.5/site-packages/domaintools-0.0.1-py3.5.egg/domaintools/base_results.py in data(self)
     25                 self.api._request_session = Session()
     26             results = self.api._request_session.get(self.url, params=self.kwargs)
---> 27             self.status = results.status_code
     28             if self.kwargs.get('format', 'json') == 'json':
     29                 self._data = results.json()

/home/tcrosley/projects/external/python_api/venv/lib/python3.5/site-packages/domaintools-0.0.1-py3.5.egg/domaintools/base_results.py in status(self, code)
     44
     45         elif code == 400:
---> 46             raise BadRequestException()
     47         elif code == 403:
     48             raise NotAuthorizedException()

BadRequestException:

the exception will contain the status code and the reason for the exception:

try:
    api.domain_profile('notvalid').data()
except Exception as e:
    assert e.code == 400
    assert 'We could not understand your request' in e.reason['error']['message']

You can get the status code of a response outside of exception handling by doing .status:

api.domain_profile('google.com').status == 200

Using the API Asynchronously

domaintools Async Example

The DomainTools' API automatically supports async usage:

search_results = await api.iris_enrich('domaintools.com').response().get('results', {})

There is built-in support for async context managers:

async with api.iris_enrich('domaintools.com').response().get('results', {}) as search_results:
    # do things

And direct async for loops:

async for result in api.iris_enrich('domaintools.com').response().get('results', {}):
    print(result)

All async operations can safely be intermixed with non async ones - with optimal performance achieved if the async call is done first:

profile = api.domain_profile('google.com')
await profile
title = profile['website_data']['title']

Interacting with the API via the command line client

domaintools CLI Example

Immediately after installing domaintools_api with pip, a domaintools command line client will become available to you:

domaintools --help

To use - simply pass in the api_call you would like to make along with the parameters that it takes and your credentials:

domaintools iris_investigate --domains domaintools.com -u $TEST_USER -k $TEST_KEY

Optionally, you can specify the desired format (html, xml, json, or list) of the results:

domaintools domain_search google --max_length 10 -u $TEST_USER -k $TEST_KEY -f html

To avoid having to type in your API key repeatedly, you can specify them in ~/.dtapi separated by a new line:

API_USER
API_KEY

Python Version Support Policy

Please see the supported versions document for the DomainTools Python support policy.

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

domaintools_api-0.6.0.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

domaintools_api-0.6.0-py2.py3-none-any.whl (18.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file domaintools_api-0.6.0.tar.gz.

File metadata

  • Download URL: domaintools_api-0.6.0.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2

File hashes

Hashes for domaintools_api-0.6.0.tar.gz
Algorithm Hash digest
SHA256 6bc2bddb222bd9641c616e43642a4aba78ab0456cbbb89a4485dc39f5c359dd0
MD5 20f04793d7e16abf87cc9e77df09575c
BLAKE2b-256 e660ff7d6354f3209b51366165a43aff52b927eb5147898f78ae2edbf466da41

See more details on using hashes here.

File details

Details for the file domaintools_api-0.6.0-py2.py3-none-any.whl.

File metadata

  • Download URL: domaintools_api-0.6.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.2

File hashes

Hashes for domaintools_api-0.6.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 7fa4947b36ea30e7e808322a8314653c28c518661c8c50f897316475c0be7b5a
MD5 04ffe8e090a33e242e66789cbe8546e8
BLAKE2b-256 929d70024f3d784e95311a4cc280b5d5422d15a5f831e7ef0c67221acbd2f9ea

See more details on using hashes here.

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