get_ip_utils.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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,json
  13. def getIpFromIpaddress(site):
  14. 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',
  15. 'Host': 'ipaddress.com'}
  16. url = "https://ipaddress.com/search/" + site
  17. trueip = None
  18. try:
  19. res = requests.get(url, headers=headers, timeout=5)
  20. soup = BeautifulSoup(res.text, 'html.parser')
  21. ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", res.text)
  22. result = soup.find_all('div', class_="comma-separated")
  23. for c in result:
  24. if len(ip) != 0:
  25. trueip = ip[0]
  26. except Exception as e:
  27. print("查询" + site + " 时出现错误: " + str(e))
  28. return trueip
  29. def getIpFromChinaz(site):
  30. 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',
  31. 'Host': 'ip.tool.chinaz.com'}
  32. url = "http://ip.tool.chinaz.com/" + site
  33. trueip = None
  34. try:
  35. res = requests.get(url, headers=headers,timeout=5)
  36. soup = BeautifulSoup(res.text, 'html.parser')
  37. result = soup.find_all('span', class_="Whwtdhalf w15-0")
  38. for c in result:
  39. ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", c.text)
  40. if len(ip) != 0:
  41. trueip = ip[0]
  42. except Exception as e:
  43. print("查询" + site + " 时出现错误: " + str(e))
  44. return trueip
  45. def getIpFromWhatismyipaddress(site):
  46. 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',
  47. 'Host': 'ip.tool.chinaz.com'}
  48. url = "https://whatismyipaddress.com//hostname-ip"
  49. data = {
  50. "DOMAINNAME": site,
  51. "Lookup IP Address": "Lookup IP Address"
  52. }
  53. trueip = None
  54. try:
  55. res = requests.post(url, headers=headers, data=data,timeout=5)
  56. soup = BeautifulSoup(res.text, 'html.parser')
  57. result = soup.find_all('span', class_="Whwtdhalf w15-0")
  58. for c in result:
  59. ip = re.findall(r"\b(?:[0-9]{1,3}\.){3}[0-9]{1,3}\b", c.text)
  60. if len(ip) != 0:
  61. trueip = ip[0]
  62. except Exception as e:
  63. print("查询" + site + " 时出现错误: " + str(e))
  64. return trueip
  65. def getIpFromipapi(site):
  66. '''
  67. return trueip: None or ip
  68. '''
  69. 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',
  70. 'Host': 'ip-api.com'}
  71. url = "http://ip-api.com/json/%s?lang=zh-CN" % (site)
  72. trueip = None
  73. try:
  74. res = requests.get(url, headers=headers,timeout=5)
  75. res=json.loads(res.text)
  76. if(res["status"]=="success"):
  77. trueip=res["query"]
  78. except Exception as e:
  79. print("查询" + site + " 时出现错误: " + str(e))
  80. return trueip