python 獲取網站IP以及服務器
阿新 • • 發佈:2017-11-09
hit __name__ pan == sys height line es2017 mon
寫了一個識別網站的ip以及服務器的代碼,比較爛,希望大佬指教
1 import requests 2 import sys 3 import socket 4 5 def get_ip(url): # 獲取IP 6 if ‘http‘ in str(url): 7 url = url.split(‘//‘)[1] 8 ip = socket.gethostbyname(url) 9 else: 10 ip = socket.gethostbyname(url) 11 return ip12 13 def get_server(url): # 獲取服務器 14 if ‘http‘ in str(url): 15 http_url = url 16 https_url = url 17 else: 18 http_url = ‘http://‘ + str(url) 19 https_url = ‘https://‘ + str(url) 20 try: 21 re_header = requests.get(http_url).headers 22 except: 23re_header = requests.get(https_url).headers 24 try: 25 server = re_header[‘server‘] 26 except: server = ‘unknow‘ 27 return server 28 29 30 if __name__ == "__main__": 31 if sys.argv[1] == ‘-r‘: 32 with open(sys.argv[2]) as f: 33 for line in f: 34 line = line.strip()35 try: 36 target_ip = get_ip(line) 37 server = get_server(line) 38 print("website:", line) 39 print("-------------IP:"+target_ip+"-------------") 40 print(‘Server:‘, server,‘\n‘) 41 except : print("website:", line, "can‘t visit\n") 42 else: 43 url = sys.argv[1] 44 try: 45 target_ip = get_ip(url) 46 server = get_server(url) 47 print("website:", url) 48 print("-------------IP:"+target_ip+"-------------") 49 print(‘Server:‘, server,‘\n‘) 50 except : print("website:", url, "can‘t visit\n")
用了requests,socket,sys 標準庫
對單個網站進行操作:
對存放文本文件進行操作:
python 獲取網站IP以及服務器