-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpkie.py
executable file
·226 lines (187 loc) · 7.05 KB
/
httpkie.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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Easy to use downloader library based on urllib/urllib2
by Bystroushaak ([email protected])
This work is licensed under a Creative Commons Licence
(http://creativecommons.org/licenses/by/3.0/cz/).
"""
# Imports =====================================================================
import urllib
import urllib2
# Variables ===================================================================
# IE 7/Windows XP headers.
IEHeaders = {
"User-Agent": "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
"Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",
"Accept-Language": "cs,en-us;q=0.7,en;q=0.3",
"Accept-Charset": "utf-8",
"Keep-Alive": "300",
"Connection": "keep-alive",
}
# Linux ubuntu x86_64 Firefox 23 headers
LFFHeaders = {
"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:23.0) Gecko/20100101 Firefox/23.0",
"Accept": "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain",
"Accept-Language": "cs,en-us;q=0.7,en;q=0.3",
"Accept-Charset": "utf-8",
"Keep-Alive": "300",
"Connection": "keep-alive",
}
#= Functions & objects ========================================================
class NoRedirectHandler(urllib2.HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
infourl = urllib.addinfourl(fp, headers, req.get_full_url())
infourl.status = code
infourl.code = code
return infourl
http_error_300 = http_error_302
http_error_301 = http_error_302
http_error_303 = http_error_302
http_error_307 = http_error_302
class Downloader():
"""
Lightweight class utilizing downloads from internet.
Main method: .download()
Important properties:
.headers
.response_headers
.cookies
.handle_cookies
"""
def __init__(self,
headers=None,
handle_cookies=True,
http_proxy=None,
disable_redirect=False):
"""
You can set:
headers -- default IEHeaders, but there is also LFFHeaders
handle_cookies -- set to false if you don't wish to automatically
handle cookies
http_proxy -- 'url:port' describing HTTP proxy
disable_redirect -- dont follow 300/301/302/307 redirects
"""
self.headers = headers if headers is not None else IEHeaders
self.response_headers = None
self.cookies = {}
self.handle_cookies = True
self.disable_redirect = disable_redirect
self.http_proxy = None
if http_proxy is not None:
self.http_proxy = {'http': http_proxy}
def download(self, url, get=None, post=None, head=None):
"""
Parameters:
url -- set url to download, automatically adds htt:// if not present
get -- dict with GET parameters
post -- dict with POST parameters
head -- set to True if you wish to use HEAD request. Returns headers
from server.
"""
# POST params
if post is not None:
if type(post) not in [dict, str]:
raise TypeError("Unknown type of post paramters.")
if type(post) == dict:
post = urllib.urlencode(post)
# append GET params to url
if get is not None:
if type(get) != dict:
raise TypeError("Unknown type of get paramters.")
get = urllib.urlencode(get)
if "?" in url:
if url[-1] == "&":
url += get
else:
url += "&" + get
else:
url += "?" + get
get = None
# check if protocol is specified in |url|
if not "://" in url:
url = "http://" + url
if self.handle_cookies:
self.__setCookies(url)
# HEAD request support
url_req = urllib2.Request(url, post, self.headers)
if head is not None:
url_req.get_method = lambda: "HEAD"
# redirect disabling support
if self.disable_redirect:
urllib2.install_opener(urllib2.build_opener(NoRedirectHandler))
else:
urllib2.install_opener(
urllib2.build_opener(urllib2.HTTPRedirectHandler)
)
# http proxy support
opener = None
if self.http_proxy is not None:
opener = urllib2.build_opener(
urllib2.ProxyHandler(self.http_proxy)
)
urllib2.install_opener(opener)
# download page and save headers from server
f = urllib2.urlopen(url_req)
data = f.read()
self.response_headers = f.info().items()
f.close()
if self.handle_cookies:
self.__readCookies(url)
# i suppose I could fix __readCookies() to use dict, but .. meh
self.response_headers = dict(self.response_headers)
# head doesn't have content, so return just response headers
if head is not None:
return self.response_headers
return data
def __setCookies(self, url):
# add cokies into headers
domain = self.__getDomain(url)
if domain in self.cookies.keys():
cookie_string = ""
for key in self.cookies[domain].keys():
cookie_string += key + "=" + str(self.cookies[domain][key]) + "; "
self.headers["Cookie"] = cookie_string.strip()
def __readCookies(self, url):
# simple (and lame) cookie handling
# parse "set-cookie" string
cookie_string = ""
for c in self.response_headers:
if c[0].lower() == "set-cookie":
cookie_string = c[1]
# parse keyword:values
tmp_cookies = {}
for c in cookie_string.split(","):
cookie = c
if ";" in c:
cookie = c.split(";")[0]
cookie = cookie.strip()
cookie = cookie.split("=")
keyword = cookie[0]
value = "=".join(cookie[1:])
tmp_cookies[keyword] = value
# append global variable cookies with new cookies
if len(tmp_cookies) > 0:
domain = self.__getDomain(url)
if domain in self.cookies.keys():
for key in tmp_cookies.keys():
self.cookies[domain][key] = tmp_cookies[key]
else:
self.cookies[domain] = tmp_cookies
# check for blank cookies
if len(self.cookies) > 0:
for domain in self.cookies.keys():
for key in self.cookies[domain].keys():
if self.cookies[domain][key].strip() == "":
del self.cookies[domain][key]
if len(self.cookies[domain]) == 0:
del self.cookies[domain]
def __getDomain(self, url):
"""
Parse domain from url.
"""
if "://" in url:
url = url.split("://")[1]
if "/" in url:
url = url.split("/")[0]
return url