# 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: [ip2loaction](https://pypi.org/project/ip-location/)
## How to use 使用方法
Install 安装:
```bash
pip install ip2loaction
```
Use 使用:
```python
from ip2location import IP2Location
ip2location = IP2Location()
region = ip2location.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 ip2location import IP2Location
import json
app = Flask(__name__)
@app.route("/ip/<ip>")
def get_ip(ip=None):
# json
if not request.args.get('callback') or request.args.get('callback').strip() == '':
return jsonify(IP2Location(ip).search())
# jsonp
else:
return request.args.get('callback') + "(" + json.dumps(IP2Location(ip).search()) + ")"
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": "ip2loaction",
"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/24/78/72d0a797d055bc2969269b5fb3c9a2bd23046108bf0d6f8085fca5081e53/ip2loaction-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: [ip2loaction](https://pypi.org/project/ip-location/)\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 ip2loaction\r\n```\r\n\r\nUse \u4f7f\u7528\uff1a\r\n\r\n```python\r\nfrom ip2location import IP2Location\r\nip2location = IP2Location()\r\nregion = ip2location.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 ip2location import IP2Location\r\nimport json\r\napp = Flask(__name__)\r\n@app.route(\"/ip/<ip>\")\r\ndef get_ip(ip=None):\r\n # json\r\n if not request.args.get('callback') or request.args.get('callback').strip() == '':\r\n return jsonify(IP2Location(ip).search())\r\n # jsonp\r\n else:\r\n return request.args.get('callback') + \"(\" + json.dumps(IP2Location(ip).search()) + \")\"\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": "fadc9733e1906a4d8d8e00c0749de1c434d654856ce06f72519f01eab53e43ca",
"md5": "2ace4f5567425d361a4983da2e1dc11c",
"sha256": "5ad5340c516e44dcd280761e83a9c12d307957ceea173537e911186737d24c02"
},
"downloads": -1,
"filename": "ip2loaction-1.0.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "2ace4f5567425d361a4983da2e1dc11c",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 4341645,
"upload_time": "2024-12-20T08:48:08",
"upload_time_iso_8601": "2024-12-20T08:48:08.042680Z",
"url": "https://files.pythonhosted.org/packages/fa/dc/9733e1906a4d8d8e00c0749de1c434d654856ce06f72519f01eab53e43ca/ip2loaction-1.0.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "247872d0a797d055bc2969269b5fb3c9a2bd23046108bf0d6f8085fca5081e53",
"md5": "893051d79b7bc13a301af62f37eab4db",
"sha256": "42ee852666d11001f50cd61edba3548092683aea1b9d311aeea58a760d461284"
},
"downloads": -1,
"filename": "ip2loaction-1.0.0.tar.gz",
"has_sig": false,
"md5_digest": "893051d79b7bc13a301af62f37eab4db",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4340364,
"upload_time": "2024-12-20T08:48:13",
"upload_time_iso_8601": "2024-12-20T08:48:13.298716Z",
"url": "https://files.pythonhosted.org/packages/24/78/72d0a797d055bc2969269b5fb3c9a2bd23046108bf0d6f8085fca5081e53/ip2loaction-1.0.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-12-20 08:48:13",
"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": "ip2loaction"
}