标签:visit def http mon head 获取ip site exce .com
写了一个识别网站的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 ip 12 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: 23 re_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 标准库
对单个网站进行操作:
对存放文本文件进行操作:
标签:visit def http mon head 获取ip site exce .com
原文地址:http://www.cnblogs.com/Tim1995/p/7810636.html