用于与ipstack geo api接口的python库
ipstack的Python项目详细描述
ipstack for python(地理位置库)
IPStack for Python is a simple library used to interface with an IPStack Geo API.
Hint: IPStack for Python is available through
Python PIP
.pip install ipstack
支持python 2.7+
在此处了解有关ipstack的更多信息:ipstack.net
功能
- 检索任何IP地址的地理位置数据。
- 检索执行此代码的系统的地理位置数据。
- 检索客户端的地理位置数据。
- 检索一批IP地址的地理位置数据。
- 评估IP地址的安全性。
基本用法
fromipstackimportGeoLookupgeo_lookup=GeoLookup(".....")location=geo_lookup.get_location("github.com")print(location)
示例用法
Note: See IPStack: Response Objects for a list of available properties in a response object.
创建geolookup对象
fromipstackimportGeoLookup# Create the GeoLookup object using your API key.geo_lookup=GeoLookup("acecac3893c90871c3")
查找IP地址的位置
# Lookup a location for an IP Address# and catch any exceptions that might# be throwntry:# Retrieve the location information for # github.com by using it's hostname.# # This function will work with hostnames# or IP addresses.location=geo_lookup.get_location("github.com")# If we are unable to retrieve the location information# for an IP address, null will be returned.iflocationisNone:print("Failed to find location.")else:# Print the Location dictionary.print(location)exceptExceptionase:print(e)
查找自己的位置
For looking up your own location we use https://ip.42.pl/raw
location=geo_lookup.get_own_location()print(location)
其他功能
# Lookup a location for an IP Address# and catch any exceptions that might# be throwntry:# Retrieve the location information for # github.com by using it's hostname.# # This function will work with hostnames# or IP addresses.location=geo_lookup.get_location("github.com")# If we are unable to retrieve the location information# for an IP address, null will be returned.iflocationisNone:print("Failed to find location.")else:# Print the Location dictionary.print(location)exceptExceptionase:print(e)
查找自己的位置
For looking up your own location we use https://ip.42.pl/raw
location=geo_lookup.get_own_location()print(location)
其他功能
For looking up your own location we use https://ip.42.pl/raw
这个库和ipstack api中还内置了一些其他有用的特性。
批量位置查找
ipstack api还提供了同时为多个ipv4或ipv6地址请求数据的能力。这需要专业的teir api密钥或更高的密钥,并且一次限制为50个ip。
locations=geo_lookup.get_location("github.com","stackoverflow.com")print(locations)
请求IP地址的主机名。
默认情况下,ipstack api不返回有关给定ip地址解析到的主机名的信息。要包含主机名,请使用以下命令。
location=geo_lookup.find_hostname().get_location("1.1.1.1")print(location["hostname"])
one.one.one.one
评估安全性
订阅Professional Plus计划的客户可以访问Ipstack API的安全模块,该模块可用于在对网站或Web应用程序造成任何损害之前评估来自某些IP地址的风险和威胁。
location=geo_lookup.assess_security().get_location("github.com")
设置响应的语言
ipstack api能够以不同的语言交付其结果集。要以英语以外的语言(默认)请求数据,请使用以下支持的语言代码之一。
location=geo_lookup.language("en").get_location("github.com")
配置您的请求
# Use HTTPS# This requires IPStack Basic plan or higher.location=geo_lookup.use_https().get_location("github.com")# Configure the timeout for requestslocation=geo_lookup.timeout(10).get_location("github.com")
发展
在提交任何内容之前,请使用以下内容创建预提交挂钩。
这将确保pycodestyle徽章正确更新。
#!/bin/bash
python3 tests/style.py