2 # -*- coding: utf-8 -*-
13 from gzip import GzipFile
15 from Keepalive import HTTPHandler
16 from cStringIO import StringIO
19 handles all outgoing HTTP-Requests of the Server
20 Usage: create Request Instance
21 use retrieveURL and call it with a url at least
22 additionaly you can firstly pass the get and secondly the post data in form of a dictonary
23 when the last argument is true the handler simulate a http referer with the last called url.
24 retrieveUrl returns response as string
27 class AbortDownload(Exception):
43 self.cj = cookielib.CookieJar()
44 handler = HTTPHandler()
45 self.opener = urllib2.build_opener(handler, urllib2.HTTPCookieProcessor(self.cj))
46 self.downloader = urllib2.build_opener()
47 #self.opener.add_handler()
49 self.opener.addheaders = [
50 ("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.10"),
51 ("Accept-Encoding", "deflate"),
52 ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"),
53 ("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7"),
54 ("Connection", "keep-alive"),
55 ("Keep-Alive", "300")]
57 self.downloader.addheaders = [
58 ("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en; rv:1.9.0.8) Gecko/2009032609 Firefox/3.0.10"),
59 ("Accept-Encoding", "deflate"),
60 ("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"),
61 ("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7")]
64 def load(self, url, get={}, post={}, ref=True, cookies=False):
67 post = urllib.urlencode(post)
72 get = urllib.urlencode(get)
77 req = urllib2.Request(url, data=post)
79 if ref and self.lastURL is not None:
80 req.add_header("Referer", self.lastURL)
86 rep = self.opener.open(req)
88 for cookie in self.cj.make_cookies(rep, req):
89 self.cookies.append(cookie)
93 if rep.headers.has_key("content-encoding"):
94 if rep.headers["content-encoding"] == "gzip":
95 output = GzipFile('', 'r', 0, StringIO(output)).read()
101 def add_auth(self, user, pw):
102 self.downloader.addheaders.append(['Authorization', 'Basic ' + base64.encodestring(user + ':' + pw)[:-1]])
104 def add_cookies(self, req):
106 for cookie in self.cookies:
107 cookie_head += cookie.name + "=" + cookie.value + "; "
108 req.add_header("Cookie", cookie_head)
109 #def download(url, filename, reporthook = None, data = None): #default von urlretrieve auch None?
110 # return self.downloader.urlretrieve(url, filename, reporthook, data)
112 def clear_cookies(self):
115 def add_proxy(self, protocol, adress):
116 handler = urllib2.ProxyHandler({protocol: adress})
117 self.opener.add_handler(handler)
118 self.downloader.add_handler(handler)
120 def download(self, url, filename, get={}, post={}, ref=True, cookies=False):
123 post = urllib.urlencode(post)
128 get = urllib.urlencode(get)
133 req = urllib2.Request(url, data=post)
135 if ref and self.lastURL is not None:
136 req.add_header("Referer", self.lastURL)
139 self.add_cookies(req)
141 rep = self.opener.open(req)
143 for cookie in self.cj.make_cookies(rep, req):
144 self.cookies.append(cookie)
148 file = open(filename, 'wb')
149 conn = self.downloader.open(req, post)
150 if conn.headers.has_key("content-length"):
151 self.dl_size = int(conn.headers["content-length"])
155 self.dl_time = time.time()
157 if self.abort: raise AbortDownload
158 self.dl_arrived += len(chunk)
163 self.dl_finished = time.time()
168 return (self.dl_arrived / ((time.time() if self.dl else self.dl_finished) - self.dl_time)) / 1024
174 return (self.dl_size - self.dl_arrived) / (self.dl_arrived / (time.time() - self.dl_time))
179 return (self.dl_size - self.dl_arrived) / 1024
181 if __name__ == "__main__":