Quellcode durchsuchen

批量ip查询地址

hongwenjun vor 3 Jahren
Ursprung
Commit
5662116e30
3 geänderte Dateien mit 104 neuen und 4 gelöschten Zeilen
  1. 21 4
      app.py
  2. 66 0
      hello.html
  3. 17 0
      ips.html

+ 21 - 4
app.py

@@ -1,4 +1,4 @@
-import ipdb, ipaddress, requests, json
+import ipdb, ipaddress, requests, re, json
 from flask import Flask, request, render_template, jsonify
 from socket import gethostbyname
 
@@ -112,11 +112,28 @@ def show_ip(ipaddr=None):
 
     return ipaddr
 
-
-
+def select_ips(ips_text):
+    ret =''
+    iplist = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", ips_text)
+    for ip in iplist:
+        try:
+            one = iplocated(ip)
+            ret+=one
+        except:
+            pass
+    return ret
+
[email protected]("/ips/", methods=['POST', 'GET'])
+def getips():
+    if request.method == 'POST':
+        ips_text = request.form['ips']
+        ips = select_ips(ips_text)
+        return render_template('ips.html', ips=ips)
+    else:
+        return render_template('ips.html')
 
 if __name__ == '__main__':
-        app.run(host='0.0.0.0', debug=True)
+        app.run(host='0.0.0.0', debug=True, port=80)
 
 # export FLASK_ENV=development   # 调试模式: 修改代码不用重启服务
 # flask run --host=0.0.0.0       # 监听所有公开的 IP

+ 66 - 0
hello.html

@@ -0,0 +1,66 @@
+<!DOCTYPE html><head><meta charset="utf-8">
+    <title>Hello,亲: 本WebAPI支持IP城市定位和域名查IP定位</title>
+    </head>
+    <html><body style="text-align:center;">
+    {% if ip %}
+    <h2>Hello, 来自 {{ city }} 的亲:</h2>
+    <a href="https://262235.xyz/ip/maps/"> <img style="margin:5px" 
+        src="https://api.map.baidu.com/staticimage/v2?ak=tF8XaCqUG9ZjFR66lqqNXmLzeT24gtGF&mcode=666666&center=110,35&width=500&height=300&zoom=4 "/>
+        </a>
+    <h3>{{ ipaddr }}: <a href="https://262235.xyz/maps.php?city={{ city }}">查看地图</a></h3> 
+    {% else %}
+      <h1>Hello World!</h1>
+    {%  endif  %}
+
+    <div>IP/域名: <input id="name" type="text">
+    <input type="button" id="btn" value="提交">
+    <input type="button" id="json" value="获得json"></div>
+
+    <script src="https://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
+    <script>
+        $("#btn").click(function () {
+            jump_url()
+        })
+        function jump_url(params) {
+            var name = $("#name").val();
+            const url = window.location.href;
+            window.location.href = '/ip/' + name
+        }
+
+        $("#json").click(function () {
+            getjson()
+        })
+        function getjson(params) {
+            var ip = $("#name").val();
+            const url = 'https://api.map.baidu.com/location/ip?ak=tF8XaCqUG9ZjFR66lqqNXmLzeT24gtGF&ip=';
+            window.location.href = url + ip + '&coor=bd09ll'
+        }
+
+    </script>
+    <br>
+    <form action="/ips/" method="post">
+          <legend>批量ip查询地址:</legend>   
+          <textarea type="text" name ="ips" cols=120 rows=20 placeholder="输入多行包含ip地址的日志,例:
+          http://192.168.1.189:5000/ips/
+          172.67.158.52  cloudflare.com"></textarea>
+          <br>
+          <input type="submit" value="点击查询">
+        </form>
+    <br>
+    本WebAPI支持IP城市定位和域名查IP定位,同时支持命令行查询<br>
+    <br>github源码:<a href="https://github.com/hongwenjun/ip">https://github.com/hongwenjun/ip</a>
+    <br>
+    <br>docker镜像:<a href="https://hub.docker.com/r/hongwenjun/ip">https://hub.docker.com/r/hongwenjun/ip</a>
+    <h3>搭建WebAPI参考文章</h3>
+    <a href="https://262235.xyz/index.php/archives/342/">Python网络开发简单的IP城市定位WebAPI</a>
+    <p>
+
+    <h3>命令行 curl 使用:</h3>
+    <pre>
+    curl https://262235.xyz/ip/            # 命令行 URL 要带 /
+
+    curl https://262235.xyz/ip/8.8.8.8    # 查询自定义 IP 定位
+
+    curl https://262235.xyz/ip/github.com   # 按域名查 IP 定位
+
+

+ 17 - 0
ips.html

@@ -0,0 +1,17 @@
+<!DOCTYPE html><html><body>
+<form action="/ips/" method="post">
+<fieldset>
+{% if ips %}
+<legend>批量ip查询地址: <a href="/ips/">点击返回</a> </legend>   
+<textarea type="text" name ="ips" cols=120 rows=100>
+{{ ips }}</textarea>
+{% else %}
+  <legend>批量ip查询地址:</legend>   
+  <textarea type="text" name ="ips" cols=120 rows=20 placeholder="输入多行包含ip地址的日志,例:
+  http://192.168.1.189:5000/ips/
+  172.67.158.52  cloudflare.com"></textarea>
+  <br>
+  <input type="submit" value="点击查询">
+{%  endif  %}
+</fieldset>
+</form></body></html>