get_ip_utils.py 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Contact : liuyuqi.gov@msn.cn
  5. @Time : 2019/08/03 17:02:15
  6. @License : Copyright © 2017-2022 liuyuqi. All Rights Reserved.
  7. @Desc : get ip from ip address
  8. '''
  9. from email import header
  10. import requests
  11. from bs4 import BeautifulSoup
  12. import re
  13. import json
  14. def getIpFromIpaddress(site):
  15. headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebkit/737.36(KHTML, like Gecke) Chrome/52.0.2743.82 Safari/537.36',
  16. 'Host': 'ipaddress.com'}
  17. url = "https://ipaddress.com/search/" + site
  18. trueip = None
  19. try:
  20. res = requests.get(url, headers=headers, timeout=5)
  21. soup = BeautifulSoup(res.text, 'html.parser')
  22. ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", res.text)
  23. result = soup.find_all('div', class_="comma-separated")
  24. for c in result:
  25. if len(ip) != 0:
  26. trueip = ip[0]
  27. except Exception as e:
  28. print("查询" + site + " 时出现错误: " + str(e))
  29. return trueip
  30. def getIpFromChinaz(site):
  31. headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebkit/737.36(KHTML, like Gecke) Chrome/52.0.2743.82 Safari/537.36',
  32. 'Host': 'ip.tool.chinaz.com'}
  33. url = "http://ip.tool.chinaz.com/" + site
  34. trueip = None
  35. try:
  36. res = requests.get(url, headers=headers, timeout=5)
  37. soup = BeautifulSoup(res.text, 'html.parser')
  38. result = soup.find_all('span', class_="Whwtdhalf w15-0")
  39. for c in result:
  40. ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", c.text)
  41. if len(ip) != 0:
  42. trueip = ip[0]
  43. except Exception as e:
  44. print("查询" + site + " 时出现错误: " + str(e))
  45. return trueip
  46. def getIpFromWhatismyipaddress(site):
  47. headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebkit/737.36(KHTML, like Gecke) Chrome/52.0.2743.82 Safari/537.36',
  48. 'Host': 'ip.tool.chinaz.com'}
  49. url = "https://whatismyipaddress.com//hostname-ip"
  50. data = {
  51. "DOMAINNAME": site,
  52. "Lookup IP Address": "Lookup IP Address"
  53. }
  54. trueip = None
  55. try:
  56. res = requests.post(url, headers=headers, data=data, timeout=5)
  57. soup = BeautifulSoup(res.text, 'html.parser')
  58. result = soup.find_all('span', class_="Whwtdhalf w15-0")
  59. for c in result:
  60. ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", c.text)
  61. if len(ip) != 0:
  62. trueip = ip[0]
  63. except Exception as e:
  64. print("查询" + site + " 时出现错误: " + str(e))
  65. return trueip
  66. def getIpFromipapi(site):
  67. '''
  68. return trueip: None or ip
  69. '''
  70. headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebkit/737.36(KHTML, like Gecke) Chrome/52.0.2743.82 Safari/537.36',
  71. 'Host': 'ip-api.com'}
  72. url = "http://ip-api.com/json/%s?lang=zh-CN" % (site)
  73. trueip = None
  74. for i in range(5):
  75. try:
  76. res = requests.get(url, headers=headers, timeout=5)
  77. res = json.loads(res.text)
  78. if(res["status"] == "success") and len(re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", res["query"])) == 1:
  79. trueip = res["query"]
  80. break
  81. except Exception as e:
  82. print("查询" + site + " 时出现错误: " + str(e))
  83. return trueip