-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathfasthost.py
More file actions
126 lines (107 loc) · 3.74 KB
/
fasthost.py
File metadata and controls
126 lines (107 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import re
import os
import shutil
hosts_location = os.environ['SYSTEMROOT'] + '\\System32\\drivers\\etc\\hosts'
backup_hosts_location = 'C:\\hosts_backup\\hosts'
def abstract_str(content, start_str, end_str):
if not content:
return ''
res = ''
if start_str:
if start_str not in content:
return res
else:
content = content[content.index(start_str) + len(start_str):]
if end_str:
if end_str not in content:
return res
else:
res = content[:content.index(end_str)]
else:
res = content
return res
# def get_fast_dns2(domain):
# try:
# import requests
# session = requests.session()
# headers = dict()
# headers[
# "User-Agent"] = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"
# headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
# headers["Referer"] = "http://ipaddress.com/"
# headers["Accept-Encoding"] = "gzip, deflate, sdch"
# headers["Accept-Language"] = "zh-CN,zh;q=0.8"
# headers["Content-Type"] = "application/x-www-form-urlencoded; charset=UTF-8"
# session.headers = headers
# response = session.post("http://ipaddress.com/search/", data='host=' + domain)
# return abstract_str(response.content, '<tr><th>IP Address:</th><td>', '</td></tr>')
# except:
# return ''
def get_fast_dns(domain):
import socket
# myaddr = socket.getaddrinfo("www." + domain, 'http')[0][4][0]
myaddr = socket.gethostbyname("www." + domain)
return myaddr
def backup_hosts():
try:
if not os.path.exists('C:\\hosts_backup'):
os.mkdir('C:\\hosts_backup')
shutil.copyfile(hosts_location, backup_hosts_location)
return True
except:
return False
def revert_hosts():
try:
if not os.path.exists('C:\\hosts_backup\hosts'):
print 'backup file not exsit!'
shutil.copyfile(backup_hosts_location, hosts_location)
os.system('ipconfig /flushdns')
os.system('ipconfig /displaydns')
print 'revert hosts success!'
except:
print 'revert hosts failed!'
def update_hosts(dns, domain):
try:
with open(hosts_location, 'a') as fs:
fs.write('\n%s %s' % (dns, domain))
return True
except:
return False
def read_hosts():
with open(hosts_location, 'r') as fs:
print fs.read()
def main(domain):
print 'start to update your hosts...'
fast_dns = get_fast_dns(domain)
if not re.match(r'\d+\.\d+\.\d+\.\d+', fast_dns):
print 'get fast dns failed'
return False
print 'get fast dns success:' + fast_dns + ',backup hosts...'
if not backup_hosts():
print 'backup host failed'
return False
print 'backup host success'
if not update_hosts(fast_dns, domain):
print 'update hosts failed'
return False
os.system('ipconfig /flushdns')
os.system('ipconfig /displaydns')
print 'update host success,now you can fly!'
if __name__ == '__main__':
if len(sys.argv) <= 1:
print 'input --google.com or other domain'
print 'input -read to review your hosts'
print 'input -revert to revert your hosts'
elif '--' in sys.argv[1] and '.' in sys.argv[1]:
main(sys.argv[1][2:])
elif sys.argv[1] == '-revert':
revert_hosts()
elif sys.argv[1] == '-read':
read_hosts()
else:
print 'input --google.com or other domain'
print 'input -read to review your hosts'
print 'input -revert to revert your hosts'