ip-region


Nameip-region JSON
Version 1.0.6 PyPI version JSON
download
home_pagehttps://github.com/jeeaay/py-ip-location
SummaryGet geographic location through IP address, support IPv4 and IPv6. Combine IP address library and online API.
upload_time2024-12-24 09:00:51
maintainerNone
docs_urlNone
authorJeay
requires_pythonNone
licenseApache-2.0 License
keywords ip loaction ip country ip region ip address loaction ip geographic location
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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](https://github.com/lionsoul2014/ip2region), and the online API comes from `ipwho.is`, `ip-api` and `ip.sb`.

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

Pypi: [ipregion](https://pypi.org/project/ip-region/)

## How to use 使用方法

Install 安装:

```bash
pip install ip-region
```

Use 使用:

```python
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`里,如果想要持久保存缓存数据,可以在实例化时传入缓存文件路径,例如:

```python
# 实例化时指定缓存路径
# 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](https://github.com/jeeaay/py-ip-location/blob/main/example1_flask.py)

insstall Flask 安装Flask:

```bash
pip install flask
```

run 运行:
```bash
python example1_flask.py
```

visit 访问本地测试路径:
```
http://127.0.0.1:5000/ip/<search ip>
```

API:
```bash
# 获取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 示例代码:

```python
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

```bash
pip install twine build
```

build

```bash
python -m build
```

upload

```bash
twine upload dist/*
```

or use one line command

```bash
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/*
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jeeaay/py-ip-location",
    "name": "ip-region",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "ip loaction, ip country, ip region, IP address loaction, IP geographic location",
    "author": "Jeay",
    "author_email": "admin@jeay.net",
    "download_url": "https://files.pythonhosted.org/packages/46/31/0dc97492edd47f072562e7165a60a3ab92cef4dc847f0d3d6baae6833e00/ip_region-1.0.6.tar.gz",
    "platform": "any",
    "description": "# IP address location \u901a\u8fc7IP\u5730\u5740\u83b7\u53d6\u5730\u7406\u4f4d\u7f6e\r\n\r\n## Introduction \u7b80\u4ecb\r\n\r\nGet 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](https://github.com/lionsoul2014/ip2region), and the online API comes from `ipwho.is`, `ip-api` and `ip.sb`.\r\n\r\n\u901a\u8fc7IP\u5730\u5740\u83b7\u53d6\u5730\u7406\u4f4d\u7f6e\uff0c\u652f\u6301IPv4\u548cIPv6\u3002\u7ed3\u5408\u4e86IP\u5730\u5740\u5e93\u548c\u5728\u7ebfAPI\u3002\u672c\u5730\u7684IP\u5730\u5740\u5e93\u6765\u81ea\u9879\u76ee[lionsoul2014/ip2region](https://github.com/lionsoul2014/ip2region)\uff0c\u5728\u7ebfAPI\u6765\u81ea`ipwho.is`\u3001`ip-api`\u3001`ip.sb`\u3002\r\n\r\nPypi: [ipregion](https://pypi.org/project/ip-region/)\r\n\r\n## How to use \u4f7f\u7528\u65b9\u6cd5\r\n\r\nInstall \u5b89\u88c5\uff1a\r\n\r\n```bash\r\npip install ip-region\r\n```\r\n\r\nUse \u4f7f\u7528\uff1a\r\n\r\n```python\r\nfrom ipregion import IP2Region\r\nip2region = IP2Region()\r\nregion = ip2region.search('8.8.8.8')\r\n```\r\n\r\n## Caching Data \u7f13\u5b58\u6570\u636e\r\n\r\nBy 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:\r\n\r\n\u9ed8\u8ba4\u60c5\u51b5\u4e0b\uff0c\u8c03\u7528\u5728\u7ebfAPI\u540e\u5c06\u4f1a\u7f13\u5b58\u5728\u672c\u9879\u76ee\u4e0b\u7684`ipcache.db3`\u91cc\uff0c\u5982\u679c\u60f3\u8981\u6301\u4e45\u4fdd\u5b58\u7f13\u5b58\u6570\u636e\uff0c\u53ef\u4ee5\u5728\u5b9e\u4f8b\u5316\u65f6\u4f20\u5165\u7f13\u5b58\u6587\u4ef6\u8def\u5f84\uff0c\u4f8b\u5982\uff1a\r\n\r\n```python\r\n# \u5b9e\u4f8b\u5316\u65f6\u6307\u5b9a\u7f13\u5b58\u8def\u5f84\r\n# Specify the cache path when instantiating\r\nimport os\r\ncur_path = os.path.abspath(os.path.dirname(__file__))\r\ndb_path = os.path.join(cur_path, \"ipcache.db3\")\r\nip2region = IP2Region( db_path = db_path)\r\nregion = ip2region.search(\"2001:4860:4860::8888\")\r\n````\r\n\r\n> The first time you specify the cache path, the target database file will be generated, so it will be a little slower\r\n>\r\n> \u7b2c\u4e00\u6b21\u6307\u5b9a\u7f13\u5b58\u8def\u5f84\u540e\uff0c\u4f1a\u751f\u6210\u76ee\u6807\u6570\u636e\u5e93\u6587\u4ef6\uff0c\u6240\u4ee5\u4f1a\u6162\u4e00\u70b9\r\n\r\n## Use in Flask \u7ed3\u5408Flask\u4f7f\u7528\r\n\r\nExample file \u793a\u4f8b\u6587\u4ef6:   `example1_flask.py`\r\n\r\n> View it on github \u8bf7\u5728github\u4e0a\u67e5\u770b\r\n>\r\n> [example1_flask.py](https://github.com/jeeaay/py-ip-location/blob/main/example1_flask.py)\r\n\r\ninsstall Flask \u5b89\u88c5Flask:\r\n\r\n```bash\r\npip install flask\r\n```\r\n\r\nrun \u8fd0\u884c:\r\n```bash\r\npython example1_flask.py\r\n```\r\n\r\nvisit \u8bbf\u95ee\u672c\u5730\u6d4b\u8bd5\u8def\u5f84:\r\n```\r\nhttp://127.0.0.1:5000/ip/<search ip>\r\n```\r\n\r\nAPI:\r\n```bash\r\n# \u83b7\u53d6IP\u5bf9\u5e94\u4f4d\u7f6e\r\nGET http://127.0.0.1:5000/ip/8.8.8.8\r\n# \u83b7\u53d6IPv6\u5bf9\u5e94\u4f4d\u7f6e\r\nGET http://127.0.0.1:5000/ip/2406:da14:2e4:8900:b5fc:b35a:34d0:93f6\r\n### \u83b7\u53d6IP\u5bf9\u5e94\u4f4d\u7f6e, \u4f7f\u7528jsonp, callbackFunction\u53ef\u4ee5\u81ea\u5b9a\u4e49\r\nGET http://127.0.0.1:5000/ip/8.8.8.8?callback=callbackFunction\r\n```\r\n\r\nExample code \u793a\u4f8b\u4ee3\u7801:\r\n\r\n```python\r\nfrom flask import Flask, jsonify, request\r\nfrom ipregion import IP2Region\r\nimport json\r\napp = Flask(__name__)\r\n@app.route(\"/ip/<ip>\")\r\ndef get_ip(ip=None):\r\n    ip2region = IP2Region()\r\n    region = ip2region.search(ip)\r\n    # json\r\n    if not request.args.get('callback') or request.args.get('callback').strip() == '':\r\n        return jsonify(region)\r\n    # jsonp\r\n    else:\r\n        return request.args.get('callback') + \"(\" + json.dumps(region) + \")\"\r\nif __name__ == \"__main__\":\r\n    app.run(debug=True)\r\n```\r\n\r\n## LICENSE\r\n\r\nApache-2.0 License\r\n\r\n## Source code\r\n\r\nhttps://github.com/jeeaay/py-ip-location\r\n\r\n## Upload to pypi\r\n\r\ninstall twine and build\r\n\r\n```bash\r\npip install twine build\r\n```\r\n\r\nbuild\r\n\r\n```bash\r\npython -m build\r\n```\r\n\r\nupload\r\n\r\n```bash\r\ntwine upload dist/*\r\n```\r\n\r\nor use one line command\r\n\r\n```bash\r\nrm -rf ip_region.egg-info dist && python -m build && twine upload dist/*\r\n\r\n# windows\r\nDEL /S /Q ip_region.egg-info dist && python -m build && twine upload dist/*\r\n```\r\n",
    "bugtrack_url": null,
    "license": "Apache-2.0 License",
    "summary": "Get geographic location through IP address, support IPv4 and IPv6. Combine IP address library and online API.",
    "version": "1.0.6",
    "project_urls": {
        "Homepage": "https://github.com/jeeaay/py-ip-location"
    },
    "split_keywords": [
        "ip loaction",
        " ip country",
        " ip region",
        " ip address loaction",
        " ip geographic location"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "81edbabc9b8b2187e7862e03eeb38658861318e1ed7178668f07254d6483d627",
                "md5": "3f3708374f6866790d240cdfafbf73ec",
                "sha256": "809bc5063014dee1fd515da68b80ad0918982a788423a8675d3d65420a7e1758"
            },
            "downloads": -1,
            "filename": "ip_region-1.0.6-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3f3708374f6866790d240cdfafbf73ec",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 4342496,
            "upload_time": "2024-12-24T09:00:45",
            "upload_time_iso_8601": "2024-12-24T09:00:45.913183Z",
            "url": "https://files.pythonhosted.org/packages/81/ed/babc9b8b2187e7862e03eeb38658861318e1ed7178668f07254d6483d627/ip_region-1.0.6-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "46310dc97492edd47f072562e7165a60a3ab92cef4dc847f0d3d6baae6833e00",
                "md5": "0d56595505557820365895cdc4a42bf6",
                "sha256": "26c977d3156ceffacfa51c11e1ef7e2d0acb52fac9f02fe604589b3efb6f97cb"
            },
            "downloads": -1,
            "filename": "ip_region-1.0.6.tar.gz",
            "has_sig": false,
            "md5_digest": "0d56595505557820365895cdc4a42bf6",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 4341224,
            "upload_time": "2024-12-24T09:00:51",
            "upload_time_iso_8601": "2024-12-24T09:00:51.032389Z",
            "url": "https://files.pythonhosted.org/packages/46/31/0dc97492edd47f072562e7165a60a3ab92cef4dc847f0d3d6baae6833e00/ip_region-1.0.6.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-24 09:00:51",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jeeaay",
    "github_project": "py-ip-location",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "ip-region"
}
        
Elapsed time: 2.61340s