Skip to content

Commit 00ac424

Browse files
committed
First commit.
1 parent cadb4fa commit 00ac424

8 files changed

Lines changed: 279 additions & 0 deletions

File tree

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 IP2WHOIS.com
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# IP2WHOIS Python SDK
2+
3+
This Python module enables user to easily implement the checking of WHOIS information for a particular domain into their solution using the API from [https://www.ip2whois.com](https://www.ip2whois.com/). It is a WHOIS lookup api that helps users to obtain domain information, WHOIS record, by using a domain name. The WHOIS API returns a comprehensive WHOIS data such as creation date, updated date, expiration date, domain age, the contact information of the registrant, mailing address, phone number, email address, nameservers the domain is using and much more. IP2WHOIS supports the query for [1113 TLDs and 634 ccTLDs](https://www.ip2whois.com/tld-cctld-supported).
4+
5+
This module requires API key to function. You may sign up for a free API key at https://www.ip2whois.com/register.
6+
7+
# Installation
8+
9+
To install this module type the following:
10+
11+
```bash
12+
pip install IP2WHOIS
13+
```
14+
15+
# Usage Example
16+
17+
### Lookup Domain Information
18+
19+
#### Object Properties
20+
21+
| Property Name | Property Type | Description |
22+
| ------------- | ------------- | ------------ |
23+
| domain | string | Domain name. |
24+
25+
```python
26+
import ip2whois
27+
28+
# Configures IP2WHOIS API key
29+
ip2whois_init = ip2whois.Api('YOUR_API_KEY')
30+
31+
# Lookup domain information
32+
parameter = {'domain': 'example.com'}
33+
results = ip2whois_init.lookup(parameter)
34+
print(results)
35+
```
36+
37+
### Convert Normal Text to Punycode
38+
39+
#### Object Properties
40+
41+
| Property Name | Property Type | Description |
42+
| ------------- | ------------- | ------------ |
43+
| domain | string | Domain name. |
44+
45+
```python
46+
import ip2whois
47+
48+
# Configures IP2WHOIS API key
49+
ip2whois_init = ip2whois.Api('YOUR_API_KEY')
50+
51+
52+
# Convert normal text to punycode
53+
parameter = {'domain': 'xn--tst-qla.de'}
54+
result = ip2whois_init.getPunycode(parameter)
55+
print(result)
56+
```
57+
58+
### Convert Punycode to Normal Text
59+
60+
#### Object Properties
61+
62+
| Property Name | Property Type | Description |
63+
| ------------- | ------------- | ------------ |
64+
| domain | string | Domain name. |
65+
66+
```python
67+
import ip2whois
68+
69+
# Configures IP2WHOIS API key
70+
ip2whois_init = ip2whois.Api('YOUR_API_KEY')
71+
72+
73+
# Convert punycode to normal text
74+
parameter = {'domain': 'täst.de'}
75+
result = ip2whois_init.getNormalText(parameter)
76+
print(result)
77+
```
78+
79+
# LICENCE
80+
81+
See the LICENSE file.

example.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import ip2whois
2+
3+
# Configures IP2WHOIS API key
4+
ip2whois_init = ip2whois.Api('YOUR_API_KEY')
5+
6+
# Lookup domain information
7+
parameter = {'domain': 'example.com'}
8+
results = ip2whois_init.lookup(parameter)
9+
print(results)
10+
11+
# Convert normal text to punycode
12+
parameter = {'domain': 'xn--tst-qla.de'}
13+
result = ip2whois_init.getPunycode(parameter)
14+
print(result)
15+
16+
# Convert punycode to normal text
17+
parameter = {'domain': 'täst.de'}
18+
result = ip2whois_init.getNormalText(parameter)
19+
print(result)

setup.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import setuptools
2+
3+
with open("README.md", "r") as fh:
4+
long_description = fh.read()
5+
6+
setuptools.setup(
7+
name="IP2WHOIS",
8+
version="1.0.0",
9+
author="IP2WHOIS",
10+
author_email="support@ip2whois.com",
11+
description="IP2WHOIS Python SDK to help user to check WHOIS information for a particular domain.",
12+
long_description=long_description,
13+
long_description_content_type="text/markdown",
14+
url="https://github.com/ip2whois/ip2whois-python",
15+
license='MIT',
16+
keywords='WHOIS',
17+
project_urls={
18+
'Official Website': 'https://www.ip2location.com',
19+
},
20+
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*',
21+
# packages=setuptools.find_packages(),
22+
package_dir={"": "src"},
23+
packages=setuptools.find_packages(where="src"),
24+
tests_require=['pytest>=3.0.6'],
25+
classifiers=[
26+
"Development Status :: 5 - Production/Stable",
27+
"Intended Audience :: Developers",
28+
"Topic :: Software Development :: Libraries :: Python Modules",
29+
'Programming Language :: Python :: 2.7',
30+
"Programming Language :: Python :: 3.5",
31+
"Programming Language :: Python :: 3.6",
32+
"Programming Language :: Python :: 3.7",
33+
"Programming Language :: Python :: 3.8",
34+
"Programming Language :: Python :: 3.9",
35+
"License :: OSI Approved :: MIT License",
36+
"Operating System :: OS Independent",
37+
],
38+
)

src/ip2whois/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from ip2whois.api import Api

src/ip2whois/api.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import json
2+
import sys
3+
4+
# Compability support for Python 2.7
5+
if sys.version < '3':
6+
import urllib, httplib
7+
def urlencode(x):
8+
return urllib.urlencode(x)
9+
def httprequest(x, usessl):
10+
try:
11+
if (usessl is True):
12+
conn = httplib.HTTPSConnection("api.ip2whois.com")
13+
else:
14+
conn = httplib.HTTPConnection("api.ip2whois.com")
15+
conn.request("GET", "/v2?" + x)
16+
res = conn.getresponse()
17+
return json.loads(res.read())
18+
except:
19+
return None
20+
else:
21+
import urllib.parse, http.client
22+
def urlencode(x):
23+
return urllib.parse.urlencode(x)
24+
def httprequest(x, usessl):
25+
try:
26+
if (usessl is True):
27+
conn = http.client.HTTPSConnection("api.ip2whois.com")
28+
else:
29+
conn = http.client.HTTPConnection("api.ip2whois.com")
30+
conn.request("GET", "/v2?" + x)
31+
res = conn.getresponse()
32+
return json.loads(res.read())
33+
except:
34+
return None
35+
36+
class Api:
37+
def __init__(self, apikey, usessl=True):
38+
self.apikey = apikey
39+
self.usessl = usessl
40+
41+
def lookup(self, params = {}):
42+
43+
try:
44+
parameters = urlencode((("key", self.apikey), ("format", 'json'), ("domain", params['domain'] if "domain" in params else '')))
45+
response = httprequest(parameters, self.usessl)
46+
return response
47+
except:
48+
return None
49+
50+
def getPunycode(self, params = {}):
51+
domain = params['domain'] if "domain" in params else ''
52+
# return domain.encode('ascii').decode('idna')
53+
if sys.version < '3':
54+
converted_result = unicode(domain).encode('ascii').decode('idna')
55+
return converted_result.encode('utf-8')
56+
else:
57+
return domain.encode('ascii').decode('idna')
58+
59+
def getNormalText(self, params = {}):
60+
domain = params['domain'] if "domain" in params else ''
61+
# return domain.encode('idna').decode('ascii')
62+
if sys.version < '3':
63+
converted_result = domain.encode('idna').decode('ascii')
64+
return converted_result.encode('utf-8')
65+
else:
66+
return domain.encode('idna').decode('ascii')

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import pytest
2+
3+
@pytest.fixture(scope = 'module')
4+
def global_data():
5+
return {'apikey': "YOUR_API_KEY"}

tests/test_api.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import pytest
4+
import json
5+
6+
import ip2whois
7+
8+
def test_lookup(global_data):
9+
ip2whois_init = ip2whois.Api(global_data["apikey"])
10+
parameter = {'domain': 'example.c'}
11+
results = ip2whois_init.lookup(parameter)
12+
# assert results['error_message'] == 'API key not found.'
13+
if (global_data["apikey"] == 'YOUR_API_KEY'):
14+
assert results['error']['error_message'] == 'API key not found.'
15+
else:
16+
assert results['error']['error_message'] == 'Invalid domain.'
17+
18+
# def test_api_key_exist(global_data, capsys):
19+
# if (global_data["apikey"] == 'YOUR_API_KEY'):
20+
# with capsys.disabled():
21+
# print ("You could enter a IP2WHOIS API Key in tests/conftest.py for real web service calling test.")
22+
# print ("You could sign up for a free API key at https://www.ip2whois.com/register if you do not have one.")
23+
# assert global_data["apikey"] == "YOUR_API_KEY"
24+
# else:
25+
# assert global_data["apikey"] != "YOUR_API_KEY"
26+
27+
def test_function_exist(global_data):
28+
ip2whois_init = ip2whois.Api(global_data["apikey"])
29+
errors = []
30+
functions_list = ['lookup', 'getPunycode', 'getNormalText']
31+
for x in range(len(functions_list)):
32+
# assert hasattr(mbv, functions_list[x]) == True, "Function did not exist."
33+
if (hasattr(ip2whois_init, functions_list[x]) is False):
34+
errors.append("Function " + functions_list[x] + " did not exist.")
35+
# assert no error message has been registered, else print messages
36+
assert not errors, "errors occured:\n{}".format("\n".join(errors))
37+
38+
def test_get_puny_code(global_data):
39+
ip2whois_init = ip2whois.Api(global_data["apikey"])
40+
parameter = {'domain': 'xn--tst-qla.de'}
41+
result = ip2whois_init.getPunycode(parameter)
42+
assert result == "täst.de"
43+
44+
def test_get_normal_text(global_data):
45+
ip2whois_init = ip2whois.Api(global_data["apikey"])
46+
parameter = {'domain': 'täst.de'}
47+
result = ip2whois_init.getNormalText(parameter)
48+
assert result == "xn--tst-qla.de"

0 commit comments

Comments
 (0)