Repository navigation
Expand file tree
/
Copy pathBrowser.py
More file actions
146 lines (111 loc) · 4.09 KB
/
Copy pathBrowser.py
File metadata and controls
146 lines (111 loc) · 4.09 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from logging import getLogger
from HTTPRequest import HTTPRequest
from HTTPDownload import HTTPDownload
class Browser(object):
__slots__ = ("log", "options", "bucket", "cj", "_size", "http", "dl")
def __init__(self, bucket=None, options={}):
self.log = getLogger("log")
self.options = options #holds pycurl options
self.bucket = bucket
self.cj = None # needs to be setted later
self._size = 0
self.renewHTTPRequest()
self.dl = None
def renewHTTPRequest(self):
if hasattr(self, "http"): self.http.close()
self.http = HTTPRequest(self.cj, self.options)
def setLastURL(self, val):
self.http.lastURL = val
# tunnel some attributes from HTTP Request to Browser
lastEffectiveURL = property(lambda self: self.http.lastEffectiveURL)
lastURL = property(lambda self: self.http.lastURL, setLastURL)
code = property(lambda self: self.http.code)
cookieJar = property(lambda self: self.cj)
def setCookieJar(self, cj):
self.cj = cj
self.http.cj = cj
@property
def speed(self):
if self.dl:
return self.dl.speed
return 0
@property
def size(self):
if self._size:
return self._size
if self.dl:
return self.dl.size
return 0
@property
def arrived(self):
if self.dl:
return self.dl.arrived
return 0
@property
def percent(self):
if not self.size: return 0
return (self.arrived * 100) / self.size
def clearCookies(self):
if self.cj:
self.cj.clear()
self.http.clearCookies()
def clearReferer(self):
self.http.lastURL = None
def abortDownloads(self):
self.http.abort = True
if self.dl:
self._size = self.dl.size
self.dl.abort = True
def httpDownload(self, url, filename, get={}, post={}, ref=True, cookies=True, chunks=1, resume=False,
progressNotify=None, disposition=False):
""" this can also download ftp """
self._size = 0
self.dl = HTTPDownload(url, filename, get, post, self.lastEffectiveURL if ref else None,
self.cj if cookies else None, self.bucket, self.options, progressNotify, disposition)
name = self.dl.download(chunks, resume)
self._size = self.dl.size
self.dl = None
return name
def load(self, *args, **kwargs):
""" retrieves page """
return self.http.load(*args, **kwargs)
def putHeader(self, name, value):
""" add a header to the request """
self.http.putHeader(name, value)
def addAuth(self, pwd):
"""Adds user and pw for http auth
:param pwd: string, user:password
"""
self.options["auth"] = pwd
self.renewHTTPRequest() #we need a new request
def removeAuth(self):
if "auth" in self.options: del self.options["auth"]
self.renewHTTPRequest()
def setOption(self, name, value):
"""Adds an option to the request, see HTTPRequest for existing ones"""
self.options[name] = value
def deleteOption(self, name):
if name in self.options: del self.options[name]
def clearHeaders(self):
self.http.clearHeaders()
def close(self):
""" cleanup """
if hasattr(self, "http"):
self.http.close()
del self.http
if hasattr(self, "dl"):
del self.dl
if hasattr(self, "cj"):
del self.cj
if __name__ == "__main__":
browser = Browser()#proxies={"socks5": "localhost:5000"})
ip = "http://www.whatismyip.com/automation/n09230945.asp"
#browser.getPage("http://google.com/search?q=bar")
#browser.getPage("https://encrypted.google.com/")
#print browser.getPage(ip)
#print browser.getRedirectLocation("http://google.com/")
#browser.getPage("https://encrypted.google.com/")
#browser.getPage("http://google.com/search?q=bar")
browser.httpDownload("http://speedtest.netcologne.de/test_10mb.bin", "test_10mb.bin")