Commit 05322189 by William Tisäter

Fix PEP8 violations and convert dos format to unix

parent 3c3225b9
Just run: Just run:
python setup.py install python setup.py install
\ No newline at end of file
# Pure Python GeoIP API # # Pure Python GeoIP API #
The API is based on [MaxMind's C-based Python API](http://www.maxmind.com/app/python), The API is based on [MaxMind's C-based Python API](http://www.maxmind.com/app/python),
but the code itself is ported from the [Pure PHP GeoIP API](http://pear.php.net/package/Net_GeoIP) by Jim Winstead and Hans Lellelid. but the code itself is ported from the [Pure PHP GeoIP API](http://pear.php.net/package/Net_GeoIP) by Jim Winstead and Hans Lellelid.
It is mostly a drop-in replacement, except the `new` and `open` methods are gone. It is mostly a drop-in replacement, except the `new` and `open` methods are gone.
Tested using tox with Python version 2.5, 2.6, 2.7, 3.0 and 3.1. Tested using tox with Python version 2.5, 2.6, 2.7, 3.0 and 3.1.
## Issues and contribution ## ## Issues and contribution ##
Bug reports are done by [creating an issue on Github](https://github.com/appliedsec/pygeoip/issues). If you want to contribute you can always [create a pull request](https://github.com/appliedsec/pygeoip/pulls) for discussion and code submission. Bug reports are done by [creating an issue on Github](https://github.com/appliedsec/pygeoip/issues). If you want to contribute you can always [create a pull request](https://github.com/appliedsec/pygeoip/pulls) for discussion and code submission.
## Installation ## ## Installation ##
You can easily install pygeoip with setuptools: You can easily install pygeoip with setuptools:
easy_install pygeoip easy_install pygeoip
## Supported Databases ## ## Supported Databases ##
* Country * Country
* Region * Region
* City * City
* Organization * Organization
* ISP * ISP
## Quick Documentation ## ## Quick Documentation ##
Create your GeoIP instance with appropriate access flag. `STANDARD` reads data from disk when needed, `MEMORY_CACHE` loads database into memory on instantiation and `MMAP_CACHE` loads database into memory using mmap. Create your GeoIP instance with appropriate access flag. `STANDARD` reads data from disk when needed, `MEMORY_CACHE` loads database into memory on instantiation and `MMAP_CACHE` loads database into memory using mmap.
import pygeoip import pygeoip
gi = pygeoip.GeoIP('/path/to/GeoIP.dat', pygeoip.MEMORY_CACHE) gi = pygeoip.GeoIP('/path/to/GeoIP.dat', pygeoip.MEMORY_CACHE)
### Country lookup ### ### Country lookup ###
>>> gi.country_code_by_name('google.com') >>> gi.country_code_by_name('google.com')
'US' 'US'
>>> gi.country_code_by_addr('64.233.161.99') >>> gi.country_code_by_addr('64.233.161.99')
'US' 'US'
>>> gi.country_name_by_addr('64.233.161.99') >>> gi.country_name_by_addr('64.233.161.99')
'United States' 'United States'
### City lookup ### ### City lookup ###
>>> gic = pygeoip.GeoIP('/path/to/GeoIPCity.dat') >>> gic = pygeoip.GeoIP('/path/to/GeoIPCity.dat')
>>> gic.record_by_addr('64.233.161.99') >>> gic.record_by_addr('64.233.161.99')
{ {
'city': 'Mountain View', 'city': 'Mountain View',
'region_name': 'CA', 'region_name': 'CA',
'area_code': 650, 'area_code': 650,
'longitude': -122.0574, 'longitude': -122.0574,
'country_code3': 'USA', 'country_code3': 'USA',
'latitude': 37.419199999999989, 'latitude': 37.419199999999989,
'postal_code': '94043', 'postal_code': '94043',
'dma_code': 807, 'dma_code': 807,
'country_code': 'US', 'country_code': 'US',
'country_name': 'United States' 'country_name': 'United States'
} }
### Timezone lookup ### ### Timezone lookup ###
>>> gic = pygeoip.GeoIP('/path/to/GeoIPCity.dat') >>> gic = pygeoip.GeoIP('/path/to/GeoIPCity.dat')
>>> gic.time_zone_by_addr('64.233.161.99') >>> gic.time_zone_by_addr('64.233.161.99')
'America/Los_Angeles' 'America/Los_Angeles'
For more information, [check out the full API documentation](http://packages.python.org/pygeoip). For more information, [check out the full API documentation](http://packages.python.org/pygeoip).
\ No newline at end of file
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" """
Pure Python GeoIP API. The API is based off of U{MaxMind's C-based Python API<http://www.maxmind.com/app/python>}, Pure Python GeoIP API
but the code itself is based on the U{pure PHP5 API<http://pear.php.net/package/Net_GeoIP/>}
by Jim Winstead and Hans Lellelid.
It is mostly a drop-in replacement, except the The API is based on U{MaxMind's C-based Python
C{new} and C{open} methods are gone. You should instantiate the L{GeoIP} class yourself: API<http://www.maxmind.com/app/python>}, but the code itself is based on
the U{pure PHP5 API<http://pear.php.net/package/Net_GeoIP/>} by Jim Winstead
and Hans Lellelid.
It is mostly a drop-in replacement, except the C{new} and C{open} methods
are gone. You should instantiate the L{GeoIP} class yourself:
C{gi = GeoIP('/path/to/GeoIP.dat', pygeoip.MEMORY_CACHE)} C{gi = GeoIP('/path/to/GeoIP.dat', pygeoip.MEMORY_CACHE)}
...@@ -63,9 +66,9 @@ class GeoIPMetaclass(type): ...@@ -63,9 +66,9 @@ class GeoIPMetaclass(type):
""" """
Singleton method to gets an instance without reparsing the db. Unique Singleton method to gets an instance without reparsing the db. Unique
instances are instantiated based on the filename of the db. Flags are instances are instantiated based on the filename of the db. Flags are
ignored for this, i.e. if you initialize one with STANDARD flag (default) ignored for this, i.e. if you initialize one with STANDARD
and then try later to initialize with MEMORY_CACHE, it will still flag (default) and then try later to initialize with MEMORY_CACHE, it
return the STANDARD one. will still return the STANDARD one.
""" """
if not hasattr(cls, '_instances'): if not hasattr(cls, '_instances'):
...@@ -150,9 +153,9 @@ class GeoIP(GeoIPBase): ...@@ -150,9 +153,9 @@ class GeoIP(GeoIPBase):
for i in range(const.STRUCTURE_INFO_MAX_SIZE): for i in range(const.STRUCTURE_INFO_MAX_SIZE):
chars = chr(255) * 3 chars = chr(255) * 3
encoding = 'unicode_escape' flag = 'unicode_escape'
delim = self._filehandle.read(3) delim = self._filehandle.read(3)
if (delim == chars) if PY3 else (delim == unicode(chars, encoding)): if (delim == chars) if PY3 else (delim == unicode(chars, flag)):
self._databaseType = ord(self._filehandle.read(1)) self._databaseType = ord(self._filehandle.read(1))
# Backwards compatibility with databases from # Backwards compatibility with databases from
...@@ -277,7 +280,7 @@ class GeoIP(GeoIPBase): ...@@ -277,7 +280,7 @@ class GeoIP(GeoIPBase):
elif seek_region < const.CANADA_OFFSET: elif seek_region < const.CANADA_OFFSET:
country_code = 'US' country_code = 'US'
region = get_region_name(seek_region - const.US_OFFSET) region = get_region_name(seek_region - const.US_OFFSET)
elif seek_region < const.WORLD_OFFSET: elif seek_region < const.WORLD_OFFSET:
country_code = 'CA' country_code = 'CA'
region = get_region_name(seek_region - const.CANADA_OFFSET) region = get_region_name(seek_region - const.CANADA_OFFSET)
else: else:
...@@ -289,7 +292,7 @@ class GeoIP(GeoIPBase): ...@@ -289,7 +292,7 @@ class GeoIP(GeoIPBase):
country_code = rec['country_code'] if 'country_code' in rec else '' country_code = rec['country_code'] if 'country_code' in rec else ''
region = rec['region_name'] if 'region_name' in rec else '' region = rec['region_name'] if 'region_name' in rec else ''
return {'country_code' : country_code, 'region_name' : region } return {'country_code': country_code, 'region_name': region}
def _get_record(self, ipnum): def _get_record(self, ipnum):
""" """
...@@ -363,7 +366,7 @@ class GeoIP(GeoIPBase): ...@@ -363,7 +366,7 @@ class GeoIP(GeoIPBase):
if record['country_code'] == 'US': if record['country_code'] == 'US':
for j in range(3): for j in range(3):
char = ord(record_buf[record_buf_pos]) char = ord(record_buf[record_buf_pos])
dmaarea_combo += (char << (j*8)) dmaarea_combo += (char << (j * 8))
record_buf_pos += 1 record_buf_pos += 1
record['dma_code'] = int(math.floor(dmaarea_combo / 1000)) record['dma_code'] = int(math.floor(dmaarea_combo / 1000))
...@@ -422,13 +425,16 @@ class GeoIP(GeoIPBase): ...@@ -422,13 +425,16 @@ class GeoIP(GeoIPBase):
@rtype: str @rtype: str
""" """
try: try:
COUNTRY_EDITIONS = (const.COUNTRY_EDITION, const.COUNTRY_EDITION_V6) VALID_EDITIONS = (const.COUNTRY_EDITION, const.COUNTRY_EDITION_V6)
if self._databaseType in COUNTRY_EDITIONS: if self._databaseType in VALID_EDITIONS:
ipv = 6 if addr.find(':') >= 0 else 4 ipv = 6 if addr.find(':') >= 0 else 4
if ipv == 4 and self._databaseType != const.COUNTRY_EDITION: if ipv == 4 and self._databaseType != const.COUNTRY_EDITION:
raise ValueError('Invalid database type; expected IPv6 address') message = 'Invalid database type; expected IPv6 address'
raise ValueError(message)
if ipv == 6 and self._databaseType != const.COUNTRY_EDITION_V6: if ipv == 6 and self._databaseType != const.COUNTRY_EDITION_V6:
raise ValueError('Invalid database type; expected IPv4 address') message = 'Invalid database type; expected IPv4 address'
raise ValueError(message)
country_id = self.id_by_addr(addr) country_id = self.id_by_addr(addr)
...@@ -465,8 +471,8 @@ class GeoIP(GeoIPBase): ...@@ -465,8 +471,8 @@ class GeoIP(GeoIPBase):
@rtype: str @rtype: str
""" """
try: try:
COUNTRY_EDITIONS = (const.COUNTRY_EDITION, const.COUNTRY_EDITION_V6) VALID_EDITIONS = (const.COUNTRY_EDITION, const.COUNTRY_EDITION_V6)
if self._databaseType in COUNTRY_EDITIONS: if self._databaseType in VALID_EDITIONS:
return const.COUNTRY_NAMES[self.id_by_addr(addr)] return const.COUNTRY_NAMES[self.id_by_addr(addr)]
elif self._databaseType in const.CITY_EDITIONS: elif self._databaseType in const.CITY_EDITIONS:
return self.record_by_addr(addr)['country_name'] return self.record_by_addr(addr)['country_name']
......
# -*- coding: utf-8 -*-
""" """
Misc. utility functions. It is part of the pygeoip package. Misc. utility functions. It is part of the pygeoip package.
...@@ -51,10 +52,11 @@ def ip2long_v4(ip): ...@@ -51,10 +52,11 @@ def ip2long_v4(ip):
ip_array = ip.split('.') ip_array = ip.split('.')
if PY3: if PY3:
# int and long are unified in py3 # int and long are unified in py3
ip_long = int(ip_array[0]) * 16777216 + int(ip_array[1]) * 65536 + int(ip_array[2]) * 256 + int(ip_array[3]) return int(ip_array[0]) * 16777216 + int(ip_array[1]) * 65536 + \
int(ip_array[2]) * 256 + int(ip_array[3])
else: else:
ip_long = long(ip_array[0]) * 16777216 + long(ip_array[1]) * 65536 + long(ip_array[2]) * 256 + long(ip_array[3]) return long(ip_array[0]) * 16777216 + long(ip_array[1]) * 65536 + \
return ip_long long(ip_array[2]) * 256 + long(ip_array[3])
def ip2long_v6(ip): def ip2long_v6(ip):
......
#!/usr/bin/env python #!/usr/bin/env python
""" """
Setup file for pygeoip package. Setup file for pygeoip package.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment