/
tpcad.py
68 lines (55 loc) · 1.48 KB
/
tpcad.py
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
### tpcad.py
#
# Update TPC News Address
#
# 1997.02.09 Jim Tittsler Jim.Tittsler@tokyopc.bbs.net
#
import os
import regex
from httplib import HTTP
hostname = "www.dtinet.or.jp"
url = "/~tpc/address.htm"
print "TPC News Address Updater 1997.02.10 23:20JST 7j1ajh@amsat.org"
print "Making connection to http://%s%s..." % (hostname,url)
h = HTTP(hostname)
h.putrequest('GET', url)
h.putheader('Accept', 'text/html')
h.putheader('Accept', 'text/plain')
h.endheaders()
errcode, errmsg, headers = h.getreply()
if errcode != 200:
print "Unable to connect"
print "Reading current address..."
f = h.getfile()
cahtml = f.readlines()
f.close()
print " ",cahtml[1]
windir = "C:\WINDOWS"
if os.environ.has_key("windir"):
windir = os.environ["windir"]
print "Updating %s\HOSTS..." % (windir)
fin = open("%s\HOSTS" % (windir) , "r")
hosts = fin.readlines()
fin.close()
prog = regex.compile("news\.tokyopc\.bbs\.net", regex.casefold)
fout = open("%s/HOSTS.NEW" % (windir), "w")
updated = 0
for host in hosts:
if (prog.search(host) != -1):
fout.write(cahtml[1])
updated = 1
else:
fout.write(host)
if (updated == 0):
fout.write(cahtml[1])
fout.close()
try:
os.unlink("%s/HOSTS.BAK" % (windir))
except os.error:
pass
try:
os.rename("%s/HOSTS" % (windir), "%s/HOSTS.BAK" % (windir))
except os.error:
pass
os.rename("%s/HOSTS.NEW" % (windir), "%s/HOSTS" % (windir))
print "Ready to read news!"