You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

是否有用于IP定位的Python模块?能否导入模块实现内置IP查询无需跳转网站?

Python Modules for IP Geolocation (No External Website Needed)

Absolutely! You don’t have to jump to external sites to look up IP locations—there are solid Python modules that let you handle this directly in your code. Here are the most reliable, widely used options:

1. geoip2 (from MaxMind)

This is the go-to choice for accurate, offline IP geolocation. It works with MaxMind’s free (GeoLite2) or paid databases, so you can run queries without relying on external APIs if you prefer.

  • Install it: pip install geoip2
  • Quick example:
    First, download MaxMind’s free GeoLite2-City database (you can grab it directly from their official platform). Once you have the .mmdb file, use this code:
    import geoip2.database
    
    # Replace with your database file path
    reader = geoip2.database.Reader('GeoLite2-City.mmdb')
    response = reader.city('8.8.8.8')
    
    print(f"City: {response.city.name}")
    print(f"Country: {response.country.name}")
    print(f"Coordinates: {response.location.latitude}, {response.location.longitude}")
    
    reader.close()
    

2. ipapi

A lightweight, API-based module with a generous free tier. It’s perfect if you want minimal setup without managing local databases.

  • Install it: pip install ipapi
  • Quick example:
    No API key required for basic free requests:
    import ipapi
    
    location = ipapi.location(ip='8.8.8.8')
    print(f"Country: {location['country_name']}")
    print(f"City: {location['city']}")
    print(f"Timezone: {location['timezone']}")
    

3. maxminddb

A lower-level, faster alternative to geoip2 for working with MaxMind databases. Great if you want more control over raw data.

  • Install it: pip install maxminddb
  • Quick example:
    import maxminddb
    
    with maxminddb.open_database('GeoLite2-City.mmdb') as reader:
        record = reader.get('8.8.8.8')
        print(f"Country: {record['country']['names']['en']}")
        print(f"City: {record['city']['names']['en']}")
    

4. ipgeolocation

Another API-based option with free and paid tiers, supporting detailed location data including currency and ISP info.

  • Install it: pip install ipgeolocation
  • Quick example:
    You’ll need a free API key from their service, but queries stay within Python:
    from ipgeolocation import IPGeolocation
    
    geo = IPGeolocation(api_key='YOUR_API_KEY')
    location = geo.get_geoip('8.8.8.8')
    
    print(f"Country: {location['country_name']}")
    print(f"Region: {location['region_name']}")
    print(f"ISP: {location['isp']}")
    

All these tools let you run IP location queries entirely within your Python script—no browser tabs or external site redirects required. Choose database-based modules for offline use, or API-based ones for zero local setup.

内容的提问来源于stack exchange,提问作者Floyd Ball

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:36:43