ips.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import ipdb, ipaddress, requests, re, json
  2. from flask import Flask, request
  3. from socket import gethostbyname
  4. from ctypes import *
  5. # GPS火星坐标互转
  6. # from china_shift import *
  7. class Location(Structure):
  8. _fields_ = [
  9. ('lon', c_double),
  10. ('lat', c_double)]
  11. db = ipdb.BaseStation("qqwry.ipdb")
  12. def iplocated(ip):
  13. city = db.find(ip, "CN")
  14. return ip + " @" + city[0] + city[1] + city[2] + city[3] + "\n"
  15. def getcity(ip):
  16. city = db.find(ip, "CN")
  17. return city[2] + '市'
  18. def getip():
  19. ip = request.remote_addr
  20. try:
  21. _ip = request.headers["X-Real-IP"]
  22. if _ip is not None:
  23. ip = _ip
  24. except Exception as e:
  25. print(e)
  26. return ip
  27. def is_Mozilla():
  28. # Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
  29. ua = request.headers["User-Agent"]
  30. if (ua.find("Mozilla") != -1):
  31. return True
  32. else:
  33. return False
  34. ## 百度免费的 IP 普通定位 API 接口
  35. def ip2bdgps(ip):
  36. url = 'https://api.map.baidu.com/location/ip?ak=>>>百度AK码<<<&coor=bd09ll&ip=' + ip
  37. try:
  38. r = requests.get(url)
  39. data = r.json()
  40. except :
  41. return
  42. if data['status'] != 0 :
  43. return (116.39564504, 39.92998578 , data['status']) # 查不到返回 北京 x,y
  44. else:
  45. x = data['content']['point']['x']
  46. y = data['content']['point']['y']
  47. return (x, y, data['status'], data)
  48. ## 免费试用500次, 1元能使用1万次 IP定位 API 接口 https://market.aliyun.com/products/57002002/cmapi00035184.html
  49. def ip2gdgps(ip):
  50. url = 'http://ips.market.alicloudapi.com/iplocaltion?ip=' + ip
  51. headers = {"Authorization":"APPCODE <<<APPCODE>>>" ,"Content-Type":"application/json; charset=utf-8" }
  52. try:
  53. r = requests.get(url=url , headers=headers)
  54. data = r.json()
  55. except :
  56. return
  57. # print(data)
  58. if data['code'] != 100:
  59. return (116.39564504, 39.92998578 , data['code']) # 查不到返回 北京 x,y
  60. elif data['message'] == "success":
  61. x = data['result']['lng']
  62. y = data['result']['lat']
  63. loc = Location(lon = float(x), lat = float(y))
  64. # loc = shift.transformFromWGSToGCJ(loc)
  65. # loc = shift.bd_encrypt(loc)
  66. return (loc.lon, loc.lat, data['code'], data)
  67. def select_ips(ips_text):
  68. ret =''
  69. iplist = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", ips_text)
  70. for ip in iplist:
  71. try:
  72. one = iplocated(ip)
  73. ret+=one
  74. except:
  75. pass
  76. return ret