app.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import ipdb, ipaddress, requests, json
  2. from flask import Flask, request, render_template, jsonify
  3. from socket import gethostbyname
  4. db = ipdb.BaseStation("qqwry.ipdb")
  5. app = Flask(__name__)
  6. def iplocated(ip):
  7. city = db.find(ip, "CN")
  8. return ip + " @" + city[0] + city[1] + city[2] + city[3] + "\n"
  9. def getcity(ip):
  10. city = db.find(ip, "CN")
  11. return city[2] + '市'
  12. def getip():
  13. ip = request.remote_addr
  14. try:
  15. _ip = request.headers["X-Real-IP"]
  16. if _ip is not None:
  17. ip = _ip
  18. except Exception as e:
  19. print(e)
  20. return ip
  21. def is_Mozilla():
  22. # Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
  23. ua = request.headers["User-Agent"]
  24. if (ua.find("Mozilla") != -1):
  25. return True
  26. else:
  27. return False
  28. ## 百度免费的 IP 普通定位 API 接口
  29. def ip2bdgps(ip):
  30. url = 'https://api.map.baidu.com/location/ip?ak=>>>百度AK码<<<&coor=bd09ll&ip=' + ip
  31. try:
  32. r = requests.get(url)
  33. data = r.json()
  34. except :
  35. return
  36. if data['status'] != 0 :
  37. return (116.39564504, 39.92998578 , data['status']) # 查不到返回 北京 x,y
  38. else:
  39. x = data['content']['point']['x']
  40. y = data['content']['point']['y']
  41. return (x, y, data['status'], data)
  42. ## 免费试用500次, 1元能使用1万次 IP定位 API 接口 https://market.aliyun.com/products/57002002/cmapi00035184.html
  43. def ip2gdgps(ip):
  44. url = 'http://ips.market.alicloudapi.com/iplocaltion?ip=' + ip
  45. headers = {"Authorization":"APPCODE <<<IP定位APPCODE>>>" ,"Content-Type":"application/json; charset=utf-8" }
  46. try:
  47. r = requests.get(url=url , headers=headers)
  48. data = r.json()
  49. except :
  50. return
  51. # print(data)
  52. if data['code'] != 100:
  53. return (116.39564504, 39.92998578 , data['code']) # 查不到返回 北京 x,y
  54. elif data['message'] == "success":
  55. x = data['result']['lng']
  56. y = data['result']['lat']
  57. return (x, y, data['code'], data)
  58. @app.route("/")
  59. def hello():
  60. ip = getip()
  61. if is_Mozilla():
  62. return ip + render_template('hello.html', ip=ip) + "\n\n" + request.headers["User-Agent"]
  63. else:
  64. return ip
  65. @app.route("/ip/maps/")
  66. def maps():
  67. ip = getip()
  68. # bdgps = ip2bdgps(ip) # 百度地图 IP 定位 API 比较慢 ,换用 免费 高德定位
  69. bdgps = ip2gdgps(ip)
  70. return render_template('maps.html', bdgps=bdgps)
  71. @app.route("/ip/bdmaps/")
  72. def bdmaps():
  73. ip = getip()
  74. bdgps = ip2bdgps(ip) # 百度地图 IP 定位 API 比较慢
  75. return render_template('maps.html', bdgps=bdgps)
  76. @app.route("/ip/")
  77. @app.route("/ip/<ipaddr>")
  78. def show_ip(ipaddr=None):
  79. # ip 地址为空获得浏览器客户端IP
  80. if ipaddr is None:
  81. ip = getip()
  82. ipaddr = iplocated(ip)
  83. if is_Mozilla():
  84. return render_template('hello.html', ip=ip, ipaddr=ipaddr, city=getcity(ip))
  85. else:
  86. return ip
  87. else:
  88. ip = ipaddr
  89. # ip地址 从纯真IP数据库 搜索城市定位
  90. try:
  91. ipaddress.ip_address(ip).is_global
  92. ipaddr = iplocated(ip)
  93. except:
  94. try:
  95. ip = gethostbyname(ip) # 域名反解析得到的IP
  96. ipaddr = iplocated(ip)
  97. except Exception as e:
  98. print(e)
  99. return ipaddr
  100. if __name__ == '__main__':
  101. app.run(host='0.0.0.0', debug=True)
  102. # export FLASK_ENV=development # 调试模式: 修改代码不用重启服务
  103. # flask run --host=0.0.0.0 # 监听所有公开的 IP