1
1

ips.py 2.9 KB

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