mitsuhiko / python-geoip

GeoIP database access for Python under a BSD license.
Other
249 stars 54 forks source link

Socket has no `inet_pton` method in Python 2.7 on Windows #4

Closed EmilStenstrom closed 9 years ago

EmilStenstrom commented 10 years ago

Hi,

This library does not work with Python 2.7 on Windows at the moment since the socket module does not have a inet_pton there. Is windows support on your radar?

Stacktrace when running a simple lookup.ip():

Traceback (most recent call last):
  File "manage.py", line 14, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Users\Admin\Envs\presskoll\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "C:\Users\Admin\Envs\presskoll\lib\site-packages\django\core\management\__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Admin\Envs\presskoll\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "C:\Users\Admin\Envs\presskoll\lib\site-packages\django\core\management\base.py", line 285, in execute
    output = self.handle(*args, **options)
  File "C:\Emils\Projects\presskoll\presskoll\webhook\management\commands\extract_sources.py", line 24, in handle
    match = geolite2.lookup(ip)
  File "C:\Users\Admin\Envs\presskoll\lib\site-packages\geoip.py", line 364, in lookup
    return self._get_actual_db().lookup(ip_addr)
  File "C:\Users\Admin\Envs\presskoll\lib\site-packages\geoip.py", line 204, in lookup
    return self._lookup(ip_addr)
  File "C:\Users\Admin\Envs\presskoll\lib\site-packages\geoip.py", line 249, in _lookup
    packed_addr = pack_ip(ip_addr)
  File "C:\Users\Admin\Envs\presskoll\lib\site-packages\geoip.py", line 34, in pack_ip
    return socket.inet_pton(fmly, ip)
AttributeError: 'module' object has no attribute 'inet_pton'
EmilStenstrom commented 10 years ago

Luckily, it seems to be easily monkey-patched: Just import [https://gist.github.com/nnemkin/4966028](this windows implementation) and monkey-patch socket.inet_pton and things work fine.

hickeroar commented 9 years ago

FYI, I've packaged up a solution to this problem based on that above gist: https://pypi.python.org/pypi/win_inet_pton

EmilStenstrom commented 9 years ago

Thank you!

anviloro commented 8 years ago

Thank you!!!, just my 2 cents for newbies like me, after downloading the package, you need to import win_inet_pton in \Lib\site-packages\geoip.py and works like a charm.

6233114 commented 7 years ago

This is what worked for a newbie (like me):

  1. Install win inet pton using terminal or cmd
  2. Run pip install win-inet-pton
  3. In the project code add import win_inet_pton in \Lib\site-packages\geoip.py as @anviloro suggests (above).