实现两个功能:
- 从服务器日志等文本中提取出IP
- 调用接口获取IP位置
 可根据实际需求自由选择
工具
流程图
![程序流程图]()
正则表达式
\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
| 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
 
 
 
 
 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()
 
 |