0%

ip查询工具

实现两个功能:

  1. 从服务器日志等文本中提取出IP
  2. 调用接口获取IP位置
    可根据实际需求自由选择

工具

  • python
    • re
    • requests
    • json
  • 百度ip查询API

流程图

程序流程图

正则表达式

\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b

百度查询API

"http://opendata.baidu.com/api.php?query=[ip]&co=&resource_id=6006&oe=utf8"

代码

git地址:http://hdyb.hbu.cn/git/Mahaotian/IpSearch

# -*- coding: UTF-8 -*-
import re
from time import sleep
import requests
import json

# 读取日志文件


def readFile(path):
f = open(path, encoding="utf-8")
text = f.read()
f.close()
return text

# 提取IP


def extractIP(text):
l = re.findall(
r"\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b", text)
# 去重
return list(set(l))

# 获取定位


def getPosition(ipList):
for item in ipList:
url = "http://opendata.baidu.com/api.php?query=" + \
item+"&co=&resource_id=6006&oe=utf8"
response = requests.get(url)
print(item, ": ", json.loads(response.text)['data'][0]['location'])
# 加个延时
sleep(1)


def main():
text = readFile("./file.txt")
ipList = extractIP(text)
getPosition(ipList)


if __name__ == '__main__':
main()