python 通过线上API查询ip归属地
API为国外API,频率限制1分钟45个ip
脚本如下
1 #!/usr/bin/env python3
2 #-*-coding:utf-8-*-
3
4 import requests,re,json,time,os
5
6 base_dir = os.path.dirname(os.path.abspath(__file__))
7 filename= base_dir + '/ipinfo'
8 with open(base_dir+'/ipaddress','r') as e:
9 ipaddress = e.read()
10 ip = json.loads(ipaddress)
11 ip_count = len(ip)
12 flag = 0
13 for x in ip:
14 r=requests.get('http://ip-api.com/json/%s'%x)
15 count = int(r.headers['X-Rl'])
16 ret = r.json()
17 info = {'ip':ret['query'],'country':ret['country'],'regionName':ret['regionName'],'city':ret['city'],'AS':ret['as']}
18
19 with open (filename,'a') as a:
20 a.write(json.dumps(info,ensure_ascii=False))
21 a.write('\n')
22 flag+=1
23 print('.'*flag,'%d%%'%int(flag/ip_count*100))
24 if count < 5:
25 print('the limit will run out please wite 60s')
26 time.sleep(60)
ipaddress文件为json格式
["103.219.22.126",
"107.155.10.38",
"107.155.16.239",
"107.155.19.39",
"108.61.123.142",
"108.61.170.254",
"108.61.196.36",
"108.61.198.51",
"128.1.55.57",
"146.185.173.26",
"151.80.238.250",
"154.16.202.151",
"172.247.36.37",
"176.126.83.222",
"185.137.181.144",
"185.157.232.103",
"185.175.56.111",
"185.175.58.114"]
ipinfo文件也保存为json格式
{"ip": "107.155.19.39", "country": "Spain", "regionName": "Madrid", "city": "Madrid", "AS": "AS21859 Zenlayer Inc"}
{"ip": "108.61.123.142", "country": "France", "regionName": "Île-de-France", "city": "Paris", "AS": "AS20473 The Constant Company, LLC"}
{"ip": "108.61.170.254", "country": "Germany", "regionName": "Hesse", "city": "Frankfurt am Main", "AS": "AS20473 The Constant Company, LLC"}
{"ip": "108.61.196.36", "country": "United Kingdom", "regionName": "England", "city": "London", "AS": "AS20473 The Constant Company, LLC"}
{"ip": "108.61.198.51", "country": "Netherlands", "regionName": "North Holland", "city": "Amsterdam", "AS": "AS20473 The Constant Company, LLC"}
{"ip": "128.1.55.57", "country": "Russia", "regionName": "Moscow", "city": "Moscow", "AS": "AS21859 Zenlayer Inc"}
{"ip": "146.185.173.26", "country": "Netherlands", "regionName": "North Holland", "city": "Amsterdam", "AS": "AS14061 DigitalOcean, LLC"}
{"ip": "151.80.238.250", "country": "France", "regionName": "Hauts-de-France", "city": "Roubaix", "AS": "AS16276 OVH SAS"}
{"ip": "154.16.202.151", "country": "Germany", "regionName": "Hesse", "city": "Frankfurt am Main", "AS": "AS61317 Digital Energy Technologies Ltd."}
{"ip": "172.247.36.37", "country": "Germany", "regionName": "Hesse", "city": "Frankfurt am Main", "AS": "AS134176 Rainbow network limited"}
{"ip": "176.126.83.222", "country": "Italy", "regionName": "Lombardy", "city": "Milan", "AS": "AS49367 Seflow S.N.C. Di Marco Brame' & C."}
{"ip": "185.137.181.144", "country": "Bulgaria", "regionName": "Varna", "city": "Varna", "AS": "AS3170 Etheroute Ltd"}
{"ip": "185.157.232.103", "country": "United Kingdom", "regionName": "England", "city": "Manchester", "AS": "AS46261 QuickPacket, LLC"}
{"ip": "185.175.56.111", "country": "Norway", "regionName": "Oslo County", "city": "Oslo", "AS": "AS63473 HostHatch, LLC"}
{"ip": "185.175.58.114", "country": "Austria", "regionName": "Vienna", "city": "Vienna", "AS": "AS63473 HostHatch, LLC"}
python 通过线上API查询ip归属地的更多相关文章
- [Ubuntu] geoip-bin 程序包 - 查询 IP 归属地
简述:在Linux命令行下查询IP归属地. 对Ubuntu/Debian系统,使用APT命令进行安装: $ sudo apt-get install geoip-bin 该包由MaxMind提供,它同 ...
- 填坑!线上Presto查询Hudi表异常排查
1. 引入 线上用户反馈使用Presto查询Hudi表出现错误,而将Hudi表的文件单独创建parquet类型表时查询无任何问题,关键报错信息如下 40931f6e-3422-4ffd-a692-6c ...
- 任何快速查询IP归属地
最近公司项目需要做一个IP归属地查询的功能,想着如果用现成的API就可以大大提高开发效率,所以在网上的API商店搜索了一番,发现了 APISpace,它里面的IP归属地API非常符合我的开发需求. ...
- Linux shell查询ip归属地
起因 有的时候写脚本需要能够在脚本中获取到ip的归属地,比如分析登录日志列出攻击者的相关信息等. 可以使用whois来查询ip的详细信息,但是whois并不是每台机器预装的,而且我想看中文的结果,所以 ...
- python的线上环境配置
1.安装python 2.7 http://www.cnblogs.com/strikebone/p/3970512.html 2.安装相关前置工具 pip, Django http://www ...
- python 利用cip.cc查询IP归属地
def ipinfocip(ip): # 获得 输入框中的信息 url = "http://www.cip.cc/%s" % ip # 模拟浏览器请求网络 headers={'Us ...
- python 利用淘宝IP库 查询IP归属地
#coding:utf-8 from django.test import TestCase import json import urllib ip = "114.114.114.114& ...
- 利用纯真ip地址库 查询 ip所属地
1. 首先下周数据源,选择是纯真数据库:http://www.cz88.net/ 2. 安装后,打开软件,将数据导出为txt格式. 3. 处理数据,参照网上的文章(http://www.jb51.ne ...
- JS 新浪API获取IP归属地
http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 返回值数据格式:var remote_ip_info = {“ret”:1,” ...
随机推荐
- 学习SVN02
代码发布方案: 1,安装,优化 软件环境,(nginx,lvs) <-------运维工程师 2,程序代码(不断更新). <--------开发工程师,(开发,运维都可以发布) 3, ...
- 学习MFS(五)
########################################################## mfs master 安装 建议 cp eth0 eth0:0 ifup eth ...
- Effective Java —— try-with-resources 优先于 try-finally
本文参考 本篇文章参考自<Effective Java>第三版第九条"Prefer try-with-resources to try-finally" The cod ...
- 算法 | 串匹配算法之KMP算法及其优化
主串 s:A B D A B C A B C 子串 t: A B C A B 问题:在主串 s 中是否存在一段 t 的子串呢? 形如上述问题,就是串匹配类问题.[串匹配--百度百科] 串匹配问题是一 ...
- Numpy的数学统计函数
Numpy的数学统计函数 本节内容: 1.Numpy有哪些数学统计函数: 函数名 说明 np.sum 所有元素的和 np.prod 所有元素的乘积 np.cumsum 元素的累积加和 np.cumpr ...
- html5不熟悉的标签全称
<dl></dl> 定义列表(英文全称:DefinitionList) <dt> 放在每个定义术语词前(定义术语.英文全称:DefinitionTerm) 名称 & ...
- html5知识点补充—footer元素的使用
使用footer元素创建脚注 顾名思义,footer元素通常位于页面的底部.尽管footer通常位于某个区域或者页面的底部,但并非总是如此.footer元素旨在包含作者.网站所有者.版权数据.网站规章 ...
- Spring Security 一键接入验证码登录和小程序登录
最近实现了一个多端登录的Spring Security组件,用起来非常丝滑,开箱即用,可插拔,而且灵活性非常强.我觉得能满足大部分场景的需要.目前完成了手机号验证码和微信小程序两种自定义登录,加上默认 ...
- PyQt5 基本语法(五)
目录 2. 输入控件(二) 2.2 步长调节 2.2.1 QAbstractSpinBox 2.2.1.1 描述 2.2.1.2 功能作用 2.2.1.2.1 使用 2.2.1.2.2 主要功能 2. ...
- Kubernetes探针原理详解
前言: 分布式系统和微服务体系结构的挑战之一是自动检测不正常的应用程序,并将请求(request)重新路由到其他可用系统,恢复损坏的组件.健康检查是应对该挑战的一种可靠方法.使用 Kubernetes ...