import os import time import datetime import _socket import traceback import sys NETWORK_CONNECTION_NAME = 'Ethernet' # change to your network connection name RESTORE_DNS_SETTINGS_TO = 'dhcp' # set to 'dhcp' or 'static X.X.X.X' to restore dns settings after script work LOGFILE_NAME = 'log.txt' funcs = [ 'gethostbyaddr', 'gethostbyname_ex' ] dns_servers = ( '8.8.4.4', '129.250.35.250', '156.154.70.22', '199.2.252.10', '204.194.232.200', '204.97.212.10', '208.67.220.220', '209.55.0.110', '209.55.1.220', '77.88.8.8', '77.88.8.88', '5.230.130.41', '87.118.126.225', '212.185.16.51', '51.255.48.247', '5.135.18.114', '74.63.222.26', '138.68.199.36', '201.158.6.14', '189.211.178.28', '46.101.26.141', '5.56.134.32', '195.110.58.92', '213.3.16.176', '62.167.1.132', '141.105.66.183', '77.34.36.138', '109.195.102.86', '84.237.20.90' ) hostnames = ( 'ubuntu.com', 'python.org', 'google.com', 'mysql.com', 'microsoft.com', 'yandex.ru', 'lingvolive.com', 'yahoo.com', 'github.com', ) def log_measures(duration_time, dns_server, hostname, func, error=''): with open(LOGFILE_NAME, 'a') as f: line = ';'.join( ( datetime.datetime.now().strftime("%Y-%m-%d;%H:%M"), str(datetime.timedelta(seconds = duration_time)).ljust(14), dns_server.ljust(15), hostname.ljust(15), func, error, ) ) f.write(line + '\n') def log_comment(text): with open(LOGFILE_NAME, 'a') as f: f.write('#{}: {}\n'.format(datetime.datetime.now().strftime("%Y-%m-%d;%H:%M"), text)) def main(): log_comment('Test started') for func in funcs: func_work_time = 0 for dns_server in dns_servers: os.system('netsh interface ip set dns "{}" static {}'. format(NETWORK_CONNECTION_NAME, dns_server)) time.sleep(2) # waiting for the shell a bit os.system('ipconfig /flushdns') time.sleep(2) # waiting for the shell a bit for hostname in hostnames: try: time1 = time.time() getattr(_socket, func)(hostname) except: time2 = time.time() error = traceback.format_exception(*sys.exc_info())[-1].strip() log_measures(time2-time1, dns_server, hostname, func, error) else: time2 = time.time() func_work_time += time2 - time1 log_comment('{} worked: {} sec'.format(func, func_work_time)) # Restore settings os.system('netsh interface ip set dns "{}" {}'. format(NETWORK_CONNECTION_NAME, RESTORE_DNS_SETTINGS_TO)) if __name__ == '__main__': main()