IP地址翻译成实际的物理地址
Web API经常被那些使用成熟的公开服务(public service)的开发者所使用。例如, ESPN提供了获取运动员信息,比赛分数等信息的API。Google的开发者社区也提供了几十个API,用于语言翻译、分析、地理位置等信息。下面利用ip-api的api和淘宝ip地址库提供的api,编写根据ip地址进行查询实际的物理地址。
运行环境
IDE: Pycharm2021
OS:macOS Ventura
Interpreter: Python3.9
示例代码
ip-api.com和淘宝IP地址库提供了一个简单易用的API, 它能将IP地址翻译成实际的物理地址。代码如下:
ip-api.com提供的API
importrequestsimportreheaders = {'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36'}# 匹配 0.0.0.0-255.255.255.255的表达式pattern = re.compile(r'(([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.){3}([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])')try:myip = input('请输入查询的ip: ')ifpattern.fullmatch(myip):url = f'http://ip-api.com/json/{myip}'resp = requests.get(url, headers=headers)print('国家:', dict(resp.json())['country'])print('区域:', dict(resp.json())['regionName'])print('纬度:', dict(resp.json())['lat'])print('经度:', dict(resp.json())['lon'])print('时区:', dict(resp.json())['timezone'])print('运行商:', dict(resp.json())['isp'])else:print('格式不正确!')except:print('内网IP')
运行结果如下图:
淘宝IP地址库提供的API,代码如下:
importrequestsimportsslimportressl._create_default_https_context = ssl._create_unverified_contexturl = 'https://ip.taobao.com/outGetIpInfo'# 匹配 0.0.0.0-255.255.255.255的表达式pattern = re.compile(r'(([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])\.){3}([1-9]?\d|1\d\d|2[0-4]\d|25[0-5])')try:myip = input('请输入查询的ip: ')ifpattern.fullmatch(myip):data = {'ip': myip,'accessKey': 'alibaba-inc' }resp = requests.post(url=url, params=data)print('国家: ', re.search('"country":"(.*?)"', resp.text).group(1))print('区域:', re.search('"region":"(.*?)"', resp.text).group(1))print('城市: ', re.search('"city":"(.*?)"', resp.text).group(1))else:print('输入格式不正确')exceptExceptionaserror:print(error)
运行结果如下图:
最新评论
推荐文章
作者最新文章
你可能感兴趣的文章
Copyright Disclaimer: The copyright of contents (including texts, images, videos and audios) posted above belong to the User who shared or the third-party website which the User shared from. If you found your copyright have been infringed, please send a DMCA takedown notice to [email protected]. For more detail of the source, please click on the button "Read Original Post" below. For other communications, please send to [email protected].
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。
版权声明:以上内容为用户推荐收藏至CareerEngine平台,其内容(含文字、图片、视频、音频等)及知识版权均属用户或用户转发自的第三方网站,如涉嫌侵权,请通知[email protected]进行信息删除。如需查看信息来源,请点击“查看原文”。如需洽谈其它事宜,请联系[email protected]。