Skip to main content

IP address location 通过IP地址获取地理位置

Introduction 简介

Get geographic location through IP address, support IPv4 and IPv6. Combine IP address library and online API. The local IP address library comes from the project lionsoul2014/ip2region, and the online API comes from ipwho.is, ip-api and ip.sb.

通过IP地址获取地理位置,支持IPv4和IPv6。结合了IP地址库和在线API。本地的IP地址库来自项目lionsoul2014/ip2region,在线API来自ipwho.is、ip-api、ip.sb。

Pypi: ipregion

How to use 使用方法

Install 安装:

pip install ip-region

Use 使用:

from ipregion import IP2Region
ip2region = IP2Region()
region = ip2region.search('8.8.8.8')

Caching Data 缓存数据

By default, after calling the online API, it will be cached in ipcache.db3 under this project. If you want to persist the cache data, you can pass in the cache file path when instantiating it, for example:

默认情况下,调用在线API后将会缓存在本项目下的ipcache.db3里,如果想要持久保存缓存数据,可以在实例化时传入缓存文件路径,例如:

# 实例化时指定缓存路径
# Specify the cache path when instantiating
import os
cur_path = os.path.abspath(os.path.dirname(__file__))
db_path = os.path.join(cur_path, "ipcache.db3")
ip2region = IP2Region( db_path = db_path)
region = ip2region.search("2001:4860:4860::8888")

The first time you specify the cache path, the target database file will be generated, so it will be a little slower

第一次指定缓存路径后,会生成目标数据库文件,所以会慢一点

Use in Flask 结合Flask使用

Example file 示例文件: example1_flask.py

View it on github 请在github上查看

example1_flask.py

insstall Flask 安装Flask:

pip install flask

run 运行:

python example1_flask.py

visit 访问本地测试路径:

http://127.0.0.1:5000/ip/<search ip>

API:

# 获取IP对应位置
GET http://127.0.0.1:5000/ip/8.8.8.8
# 获取IPv6对应位置
GET http://127.0.0.1:5000/ip/2406:da14:2e4:8900:b5fc:b35a:34d0:93f6
### 获取IP对应位置, 使用jsonp, callbackFunction可以自定义
GET http://127.0.0.1:5000/ip/8.8.8.8?callback=callbackFunction

Example code 示例代码:

from flask import Flask, jsonify, request
from ipregion import IP2Region
import json
app = Flask(__name__)
@app.route("/ip/<ip>")
def get_ip(ip=None):
    ip2region = IP2Region()
    region = ip2region.search(ip)
    # json
    if not request.args.get('callback') or request.args.get('callback').strip() == '':
        return jsonify(region)
    # jsonp
    else:
        return request.args.get('callback') + "(" + json.dumps(region) + ")"
if __name__ == "__main__":
    app.run(debug=True)

LICENSE

Apache-2.0 License

Source code

https://github.com/jeeaay/py-ip-location

Upload to pypi

install twine and build

pip install twine build

build

python -m build

upload

twine upload dist/*

or use one line command

rm -rf ip_region.egg-info dist && python -m build && twine upload dist/*

# windows
DEL /S /Q ip_region.egg-info dist && python -m build && twine upload dist/*

Metadata

Release files for ip-region 1.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ip-region 1.0.6
File Size Uploaded Provenance Actions
ip_region-1.0.6.tar.gz 4.3 MB Without provenance Details for ip_region-1.0.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ip-region 1.0.6
File Interpreter ABI Platform Actions
ip_region-1.0.6-py3-none-any.whl Python 3 none any Details for ip_region-1.0.6-py3-none-any.whl

Total release size: 8.7 MB

Release files / ip_region-1.0.6.tar.gz

Download URL ip_region-1.0.6.tar.gz
Size 4.3 MB
Tags Source
SHA-256 checksum
How to use checksums
26c977d3156ceffacfa51c11e1ef7e2d0acb52fac9f02fe604589b3efb6f97cb
BLAKE2b-256 checksum
How to use checksums
46310dc97492edd47f072562e7165a60a3ab92cef4dc847f0d3d6baae6833e00
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Release files / ip_region-1.0.6-py3-none-any.whl

Download URL ip_region-1.0.6-py3-none-any.whl
Size 4.3 MB
Tags Python 3
SHA-256 checksum
How to use checksums
809bc5063014dee1fd515da68b80ad0918982a788423a8675d3d65420a7e1758
BLAKE2b-256 checksum
How to use checksums
81edbabc9b8b2187e7862e03eeb38658861318e1ed7178668f07254d6483d627
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.0.1 CPython/3.12.8

Security for ip-region 1.0.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page