Skip to content

Commit

Permalink
bug/4
Browse files Browse the repository at this point in the history
  • Loading branch information
secynic committed Sep 12, 2013
1 parent 65c65e9 commit cd1e681
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========

0.1.4 (2013-09-12)
------------------

- Added validity checks for the asn_registry value due to a bug in the Team Cymru ASN lookup over night.
- Added timeout argument to IPWhois(). This is the default timeout in seconds for socket connections.
- Fixed decoding issue in IPWhois.get_whois().

0.1.3 (2013-09-11)
------------------

Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ipwhois

ipwhois is a simple package for retrieving and parsing whois data for IPv4 and IPv6 addresses. This code was quickly thrown together to demonstrate functionality, and is by no means optimized or fully featured.

This version requires Python 3.3 (for the ipaddress library) and dnspython3. Other Python version support is planned.
This version requires Python 3.3+ (for the ipaddress library) and dnspython3. Other Python version support is planned.

Usage Examples
==============
Expand Down
36 changes: 24 additions & 12 deletions ipwhois/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

__version__ = '0.1.3'
__version__ = '0.1.4'

import ipaddress, socket, urllib.request, dns.resolver, re
from xml.dom.minidom import parseString
Expand Down Expand Up @@ -109,6 +109,9 @@
def get_countries():
"""
The function to generate a dictionary containing ISO_3166-1 country codes to names.
Returns:
Dictionary: A dictionary with the country codes as the keys and the country names as the values.
"""

#Initialize the countries dictionary.
Expand Down Expand Up @@ -300,11 +303,14 @@ class IPWhois():
address: An IPv4 or IPv6 address in string format.
"""

def __init__(self, address):
def __init__(self, address, timeout = 5):

#IPv4Address or IPv6Address, use ipaddress package exception handling.
self.address = ipaddress.ip_address(address)

#Default timeout for socket connections.
self.timeout = timeout

#IP address in string format for use in queries.
self.address_str = self.address.__str__()

Expand Down Expand Up @@ -379,13 +385,18 @@ def get_asn_dns(self):

#Parse out the ASN information.
temp = str(data[0]).split("|")

ret = {}

ret['asn_registry'] = temp[3].strip(" \n")

if ret['asn_registry'] not in NIC_WHOIS.keys():

return None

ret['asn'] = temp[0].strip(' "\n')
ret['asn_cidr'] = temp[1].strip(" \n")
ret['asn_country_code'] = temp[2].strip(" \n").upper()
ret['asn_registry'] = temp[3].strip(" \n")
ret['asn_date'] = temp[4].strip(' "\n')

return ret
Expand Down Expand Up @@ -414,6 +425,7 @@ def get_asn_whois(self, retry_count = 3):

#Create the connection for the Cymru whois query.
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.settimeout(self.timeout)
conn.connect((CYMRU_WHOIS, 43))

#Query the Cymru whois server, and store the results.
Expand All @@ -436,10 +448,15 @@ def get_asn_whois(self, retry_count = 3):

ret = {}

ret['asn_registry'] = temp[4].strip(" \n")

if ret['asn_registry'] not in NIC_WHOIS.keys():

return None

ret['asn'] = temp[0].strip(" \n")
ret['asn_cidr'] = temp[2].strip(" \n")
ret['asn_country_code'] = temp[3].strip(" \n").upper()
ret['asn_registry'] = temp[4].strip(" \n")
ret['asn_date'] = temp[5].strip(" \n")

return ret
Expand Down Expand Up @@ -474,6 +491,7 @@ def get_whois(self, asn_registry = 'arin', retry_count = 3):

#Create the connection for the whois query.
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.settimeout(self.timeout)
conn.connect((NIC_WHOIS[asn_registry]["server"], 43))

#Prep the query.
Expand All @@ -488,13 +506,7 @@ def get_whois(self, asn_registry = 'arin', retry_count = 3):
response = ''
while True:

if asn_registry == "lacnic":

d = conn.recv(4096).decode("latin-1")

else:

d = conn.recv(4096).decode()
d = conn.recv(4096).decode("utf-8", "ignore")

response += d

Expand Down

0 comments on commit cd1e681

Please sign in to comment.