diff --git a/CHANGES.rst b/CHANGES.rst index f0d2383..0dffbd1 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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) ------------------ diff --git a/ipwhois/__init__.py b/ipwhois/__init__.py index 940e65d..c0413f4 100644 --- a/ipwhois/__init__.py +++ b/ipwhois/__init__.py @@ -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 \ No newline at end of file diff --git a/ipwhois/iso_3166-1_list_en.xml b/ipwhois/data/iso_3166-1_list_en.xml similarity index 100% rename from ipwhois/iso_3166-1_list_en.xml rename to ipwhois/data/iso_3166-1_list_en.xml diff --git a/ipwhois/ipwhois.py b/ipwhois/ipwhois.py index 56e2813..fa84c65 100644 --- a/ipwhois/ipwhois.py +++ b/ipwhois/ipwhois.py @@ -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', @@ -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: @@ -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 = { @@ -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: @@ -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). @@ -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: @@ -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 = { @@ -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: @@ -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: @@ -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() diff --git a/ipwhois/utils.py b/ipwhois/utils.py index 0dd099c..60f4c26 100644 --- a/ipwhois/utils.py +++ b/ipwhois/utils.py @@ -24,6 +24,7 @@ import ipaddress from xml.dom.minidom import parseString from os import path +import sys def get_countries(): """ @@ -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() diff --git a/setup.py b/setup.py index b1af383..ceb9c93 100644 --- a/setup.py +++ b/setup.py @@ -47,7 +47,7 @@ PACKAGES = ['ipwhois'] -PACKAGE_DATA = {'ipwhois': ['*.xml']} +PACKAGE_DATA = {'ipwhois': ['data/*.xml']} INSTALL_REQUIRES = [ "dnspython3"