# IP address location 通过IP地址获取地理位置
## 简介
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 `ip-api` and `ip.sb`.
通过IP地址获取地理位置,支持IPv4和IPv6。结合了IP地址库和在线API。本地的IP地址库来自项目[lionsoul2014/ip2region](https://github.com/lionsoul2014/ip2region),在线API来自`ip-api`、`ip.sb`。
Pypi: [LocateIP](https://pypi.org/project/LocateIP/)
## How to use 使用方法
Install 安装:
```bash
pip install LocateIP
```
Use 使用:
```python
from LocateIP import LocateIP
locateIP = LocateIP()
region = locateIP.search('8.8.8.8')
```
## 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 LocateIP import LocateIP
import json
app = Flask(__name__)
@app.route("/ip/<ip>")
def get_ip(ip=None):
locateIP = LocateIP()
region = locateIP.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/*
```
Raw data
{
"_id": null,
"home_page": "https://github.com/jeeaay/py-ip-location",
"name": "LocateIP",
"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/fc/c9/d5f8877bcb2d7e5db477a3f7c3d700e853bc8bf1b40c5792ec48b1aa63cf/locateip-1.0.0.tar.gz",
"platform": "any",
"description": "# IP address location \u901a\u8fc7IP\u5730\u5740\u83b7\u53d6\u5730\u7406\u4f4d\u7f6e\r\n\r\n## \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 `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`ip-api`\u3001`ip.sb`\u3002\r\n\r\nPypi: [LocateIP](https://pypi.org/project/LocateIP/)\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 LocateIP\r\n```\r\n\r\nUse \u4f7f\u7528\uff1a\r\n\r\n```python\r\nfrom LocateIP import LocateIP\r\nlocateIP = LocateIP()\r\nregion = locateIP.search('8.8.8.8')\r\n```\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> [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 LocateIP import LocateIP\r\nimport json\r\napp = Flask(__name__)\r\n@app.route(\"/ip/<ip>\")\r\ndef get_ip(ip=None):\r\n locateIP = LocateIP()\r\n region = locateIP.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",
"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.0",
"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": "ae732f932c8bda96d5adde93eefa040c7967701a9c5a5b1641ea332feff20609",
"md5": "2809cb75024935feb3de1627efe47874",
"sha256": "82fedd5d8b9a9587d5d68956563a7a0a03d0f7944f2e028d2a1372c29c289f3f"
},
"downloads": -1,
"filename": "LocateIP-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2809cb75024935feb3de1627efe47874",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 4341582,
"upload_time": "2024-12-23T02:55:38",
"upload_time_iso_8601": "2024-12-23T02:55:38.638207Z",
"url": "https://files.pythonhosted.org/packages/ae/73/2f932c8bda96d5adde93eefa040c7967701a9c5a5b1641ea332feff20609/LocateIP-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "fcc9d5f8877bcb2d7e5db477a3f7c3d700e853bc8bf1b40c5792ec48b1aa63cf",
"md5": "5b3beba4d60b4777acc6d4c8f722ca53",
"sha256": "765e4108c51cb90e5e10b4617ed6649f43aac7ff0b7db964ec2caf70e442791e"
},
"downloads": -1,
"filename": "locateip-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "5b3beba4d60b4777acc6d4c8f722ca53",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4340055,
"upload_time": "2024-12-23T02:55:42",
"upload_time_iso_8601": "2024-12-23T02:55:42.516097Z",
"url": "https://files.pythonhosted.org/packages/fc/c9/d5f8877bcb2d7e5db477a3f7c3d700e853bc8bf1b40c5792ec48b1aa63cf/locateip-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-23 02:55:42",
"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": "locateip"
}