Skip to content

Commit

Permalink
Merge pull request #18 from secynic/dev
Browse files Browse the repository at this point in the history
v0.3.0
  • Loading branch information
secynic committed Sep 30, 2013
2 parents 39af4f3 + d5b5533 commit 5111f18
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
8 changes: 8 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
Changelog
=========

0.3.0 (2013-09-30)
------------------

- Fixed get_countries() to work with frozen executables.
- Added dnspython3 rdtypes import to fix issue with frozen executables.
- Moved iso_3166-1_list_en.xml to /data.
- Added retry_count to IPWhois.lookup() and IPWhois.lookup_rws().

0.2.1 (2013-09-27)
------------------

Expand Down
2 changes: 1 addition & 1 deletion ipwhois/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
# (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.2.1'
__version__ = '0.3.0'

from .ipwhois import IPWhois, IPDefinedError, ASNLookupError, WhoisLookupError, HostLookupError
File renamed without changes.
23 changes: 14 additions & 9 deletions ipwhois/ipwhois.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
from urllib import request
from time import sleep

#Import the dnspython3 rdtypes to resolve the dynamic import problem when frozen to exe.
import dns.rdtypes.ANY.TXT

IETF_RFC_REFERENCES = {
#IPv4
'RFC 1122, Section 3.2.1.3': 'http://tools.ietf.org/html/rfc1122#section-3.2.1.3',
Expand Down Expand Up @@ -471,12 +474,13 @@ def get_host(self, retry_count = 3):

raise HostLookupError('Host lookup failed for %r.' % self.address_str)

def lookup(self, inc_raw = False):
def lookup(self, inc_raw = False, retry_count = 3):
"""
The function for retrieving and parsing whois information for an IP address via port 43 (WHOIS).
Args:
inc_raw: Boolean for whether to include the raw whois results in the returned dictionary.
retry_count: The number of times to retry in case socket errors, timeouts, connection resets, etc. are encountered.
Returns:
Dictionary: A dictionary containing the following keys:
Expand All @@ -499,7 +503,7 @@ def lookup(self, inc_raw = False):

except ASNLookupError:

asn_data = self.get_asn_whois()
asn_data = self.get_asn_whois(retry_count)

#Create the return dictionary.
results = {
Expand All @@ -512,7 +516,7 @@ def lookup(self, inc_raw = False):
results.update(asn_data)

#Retrieve the whois data.
response = self.get_whois(results['asn_registry'])
response = self.get_whois(results['asn_registry'], retry_count)

#If the inc_raw parameter is True, add the response to the return dictionary.
if inc_raw:
Expand Down Expand Up @@ -675,7 +679,7 @@ def lookup(self, inc_raw = False):

return results

def lookup_rws(self, inc_raw = False):
def lookup_rws(self, inc_raw = False, retry_count = 3):
"""
The function for retrieving and parsing whois information for an IP address via HTTP (Whois-RWS).
Expand All @@ -685,6 +689,7 @@ def lookup_rws(self, inc_raw = False):
Args:
inc_raw: Boolean for whether to include the raw whois results in the returned dictionary.
retry_count: The number of times to retry in case socket errors, timeouts, connection resets, etc. are encountered.
Returns:
Dictionary: A dictionary containing the following keys:
Expand All @@ -707,7 +712,7 @@ def lookup_rws(self, inc_raw = False):

except ASNLookupError:

asn_data = self.get_asn_whois()
asn_data = self.get_asn_whois(retry_count)

#Create the return dictionary.
results = {
Expand All @@ -722,12 +727,12 @@ def lookup_rws(self, inc_raw = False):
#Retrieve the whois data.
try:

response = self.get_rws(NIC_WHOIS[results['asn_registry']]['url'].format(self.address_str))
response = self.get_rws(NIC_WHOIS[results['asn_registry']]['url'].format(self.address_str), retry_count)

#If the query failed, try the radb-grs source.
except WhoisLookupError:

response = self.get_rws('http://apps.db.ripe.net/whois/grs-search?query-string={0}&source=radb-grs'.format(self.address_str))
response = self.get_rws('http://apps.db.ripe.net/whois/grs-search?query-string={0}&source=radb-grs'.format(self.address_str), retry_count)

#If the inc_raw parameter is True, add the response to the return dictionary.
if inc_raw:
Expand Down Expand Up @@ -793,7 +798,7 @@ def lookup_rws(self, inc_raw = False):

try:

ref_response = self.get_rws(ref_url)
ref_response = self.get_rws(ref_url, retry_count)

except WhoisLookupError:

Expand Down Expand Up @@ -835,7 +840,7 @@ def lookup_rws(self, inc_raw = False):
try:

poc_url = poc['$']
poc_response = self.get_rws(poc_url)
poc_response = self.get_rws(poc_url, retry_count)

net['%s_emails' % poc['@description'].lower()] = poc_response['poc']['emails']['email']['$'].strip()

Expand Down
12 changes: 11 additions & 1 deletion ipwhois/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import ipaddress
from xml.dom.minidom import parseString
from os import path
import sys

def get_countries():
"""
Expand All @@ -38,8 +39,17 @@ def get_countries():

try:

#Set the data directory based on if the script is a frozen executable or not.
if sys.platform == 'win32' and getattr(sys, 'frozen', False):

data_dir = path.dirname(sys.executable)

else:

data_dir = path.dirname(__file__)

#Create the country codes file object.
f = open(str(path.dirname(__file__)) + '/iso_3166-1_list_en.xml', 'r')
f = open(str(data_dir) + '/data/iso_3166-1_list_en.xml', 'r')

#Read the file.
data = f.read()
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

PACKAGES = ['ipwhois']

PACKAGE_DATA = {'ipwhois': ['*.xml']}
PACKAGE_DATA = {'ipwhois': ['data/*.xml']}

INSTALL_REQUIRES = [
"dnspython3"
Expand Down

0 comments on commit 5111f18

Please sign in to comment.