1 #! /usr/local/bin/python -O
5 Written by BroytMann, Mar 1999 - Feb 2000. Copyright (C) 1999-2000 PhiloSoft Design
9 import sys, os, stat, string, time
10 import urllib, www_util
14 from subproc import RecordFile
16 from md5wrapper import md5wrapper
20 def myftpwrapper(user, passwd, host, port, dirs):
22 ftpcache_key = (user, host, port, string.joinfields(dirs, '/'))
23 return _ftpwrapper(user, passwd, host, port, dirs)
25 _ftpwrapper = urllib.ftpwrapper
26 urllib.ftpwrapper = myftpwrapper
30 _welcome = urllib._urlopener.ftpcache[ftpcache_key].ftp.welcome
31 ftpcache_key = None # I am assuming there are no duplicate ftp URLs in db.
32 # If there are - ftpcache_key in prev line is invalid.
36 class RedirectException(Exception):
41 def __init__(self, errcode, newurl):
42 Exception.__init__(self, "(%s.) to %s" % (self.reloc_dict[errcode], newurl))
45 class MyURLopener(urllib.URLopener):
46 # Error 302 -- relocated (temporarily)
47 def http_error_302(self, url, fp, errcode, errmsg, headers, data=None):
48 if headers.has_key('location'):
49 newurl = headers['location']
50 elif headers.has_key('uri'):
51 newurl = headers['uri']
54 raise RedirectException(errcode, newurl)
56 # Error 301 -- also relocated (permanently)
57 http_error_301 = http_error_302
59 # Error 401 -- authentication required
60 def http_error_401(self, url, fp, errcode, errmsg, headers, data=None):
61 raise IOError, ('http error', errcode, "Authentication required ", headers)
65 if type(msg) == type(""):
71 s.append("'%s'" % string.join(string.split(str(i), "\n"), "\\n"))
72 return "(%s)" % string.join(s)
74 def check_url(record):
76 now = str(int(time.time()))
77 url_type, url_rest = urllib.splittype(record["URL"])
78 url_host, url_path = urllib.splithost(url_rest)
79 url_path, url_tag = urllib.splittag(url_path)
81 tempfname = record["TEMPFILE"]
82 del record["TEMPFILE"]
84 fname, headers = urllib.urlretrieve("%s://%s%s" % (url_type, url_host, url_path), tempfname)
87 record["Size"] = str(os.stat(tempfname)[stat.ST_SIZE])
91 last_modified = headers["Last-Modified"]
96 last_modified = www_util.parse_time(last_modified)
99 last_modified = str(int(last_modified))
101 last_modified = record["LastVisit"]
103 record["LastModified"] = last_modified
106 if url_type == "ftp": # Pass welcome message through MD5
107 md5.update(get_welcome())
109 md5.md5file(tempfname)
110 record["MD5"] = str(md5)
113 if (msg[0] == "http error") and (msg[1] == -1):
114 record["NoError"] = "The server did not return any header - it is not an error, actually"
116 record["Error"] = get_error(msg)
119 record["Error"] = "Unexpected EOF (FTP server closed connection)"
121 except RedirectException, msg:
122 record["Moved"] = str(msg)
124 # Mark this even in case of error
125 record["LastTested"] = now
129 urllib._urlopener = MyURLopener()
131 # Some sites allow only Mozilla-compatible browsers; way to stop robots?
132 server_version = "Mozilla/3.0 (compatible; Python-urllib/%s)" % urllib.__version__
133 urllib._urlopener.addheaders[0] = ('User-agent', server_version)
135 rec_in = RecordFile(sys.stdin)
136 rec_out = RecordFile(sys.stdout)
139 record = pickle.loads(rec_in.read_record())
141 rec_out.write_record(pickle.dumps(record))
144 if __name__ == '__main__':