]>
git.ipfire.org Git - ipfire.org.git/blob - download/getfile.py
9 from mimetypes
import guess_type
11 access
= open("access.log", "a")
12 error
= open("error.log", "a")
15 # PRIO SCHEME HOSTNAME PATH
16 ( 2, "http", "mirror1.ipfire.org", "/",),
17 ( 4, "http", "mirror3.ipfire.org", "/",),
18 ( 4, "http", "mirror5.ipfire.org", "/",),
19 (10, "http", "www.rowie.at", "/ipfire/",),
20 ( 8, "http", "ipfire.earl-net.com", "/",),
21 ( 8, "http", "ipfire.kbarthel.de", "/",),
22 ( 8, "http", "ipfire.1l0v3u.com", "/",),
23 ( 4, "http", "hex2.sv-altikon.ch", "/ipfire/",),
24 ( 8, "http", "ipfire.wintertech.de", "/",),
25 ( 8, "http", "kraefte.org", "/ipfire",),
29 return random
.randint(0, len(servers
)-1)
32 mimetype
= guess_type(file)[0] or "text/plain"
34 size
= os
.fstat(f
.fileno()).st_size
35 print "Status: 200 OK"
36 print "Content-Type:", mimetype
38 print "Content-Length:", size
44 def __init__(self
, scheme
="http", hostname
=None, path
=None, priority
=0):
45 self
.hostname
= hostname
49 self
.priority
= priority
52 return "%s://%s%s" % (self
.scheme
, self
.hostname
, self
.path
)
58 return "<%s.%s %s>" % (__name__
, __class__
, self
.__str
__())
61 return os
.system("ping -c1 -w1 %s &>/dev/null" % self
.hostname
)
66 f
= urllib2
.urlopen("%s" % urlparse
.urljoin(self
.url(), file))
67 except (urllib2
.HTTPError
, urllib2
.URLError
), e
:
69 error
.write("%s " % time
.asctime())
70 error
.write("ERR 500 - %s %s\n" % (self
.url(), e
))
79 self
.prio_servers
= []
89 for server
in self
.all():
90 for priority
in range(0, server
.priority
):
95 servers
= self
.shuffled()
96 return servers
[rnd(servers
)]
98 def add(self
, server
):
99 self
.servers
.append(server
)
101 def rem(self
, server
):
103 for host
in self
.all():
104 if not host
== server
:
110 for (priority
, scheme
, hostname
, path
) in hosts
:
111 servers
.add(Server(scheme
=scheme
, hostname
=hostname
, path
=path
, priority
=priority
))
113 file = cgi
.FieldStorage().getfirst("file")
116 server
= servers
.one()
122 url
= server
.file(file)
127 print "Status: 302 Moved Temporarily"
128 print "Location:", url
129 print "Pragma: no-cache"
132 access
.write("%s " % time
.asctime())
133 access
.write("%s\n" % url
)
137 error
.write("%s " % time
.asctime())
138 error
.write("%s was not found on mirror servers. Trying local.\n" % file)
139 filename
= ".%s" % file
140 if os
.access(filename
, os
.R_OK
):
141 access
.write("%s " % time
.asctime())
142 access
.write("(local) %s\n" % filename
)
145 print "Status: 404 Not Found"
146 print "Pragma: no-cache"
150 error
.write("%s " % time
.asctime())
151 error
.write("ERR 404 - %s wasn't found on any server" % file)