-
Notifications
You must be signed in to change notification settings - Fork 4
/
http.py
executable file
·309 lines (243 loc) · 7.48 KB
/
http.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import urllib2
import httplib
from Peach.publisher import Publisher
class HttpPost(Publisher):
"""
This publisher can be called like a method including one or more
parameters to be sent.
If called as a stream the data will be posted in the body.
"""
def __init__(self, url):
Publisher.__init__(self)
#: Indicates which method should be called.
self.withNode = False
self.url = url
(self.scheme, self.netloc, self.path, self.query, self.frag) = httplib.urlsplit(url)
self.headers = {'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'}
def connect(self):
pass
def start(self):
"""
Change state such that send/receave will work. For Tcp this
could be connecting to a remote host
"""
pass
def stop(self):
"""
Change state such that send/receave will not work. For Tcp this
could be closing a connection, for a file it might be closing the
file handle.
"""
pass
def send(self, data):
"""
Publish some data
@type data: string
@param data: Data to publish
"""
req = urllib2.Request(self.url, data, self.headers)
urllib2.urlopen(req)
def receive(self, size=None):
"""
Receive some data.
@type size: integer
@param size: Number of bytes to return
@rtype: string
@return: data received
"""
raise PeachException("Action 'receive' not supported by publisher")
def call(self, method, args):
"""
Call a method using arguments.
@type method: string
@param method: Method to call
@type args: Array
@param args: Array of strings as arguments
@rtype: string
@return: data returned (if any)
"""
raise PeachException("Action 'call' not supported by publisher")
def property(self, property, value=None):
"""
Get or set property
@type property: string
@param property: Name of method to invoke
@type value: object
@param value: Value to set. If None, return property instead
"""
self.headers[property] = value
def close(self):
"""
Close current stream/connection.
"""
pass
class HttpDigestAuth(Publisher):
"""
A simple HTTP publisher.
"""
def __init__(self, url, realm, username, password, headers=None, timeout=0.1):
"""
@type host: string
@param host: Remote host
@type port: number
@param port: Remote port
@type timeout: number
@param timeout: How long to wait for reponse
"""
Publisher.__init__(self)
self._url = url
self._realm = realm
self._username = username
self._password = password
self._headers = eval(headers)
self._timeout = float(timeout)
self._fd = None
def stop(self):
"""
Close connection if open.
"""
self.close()
def connect(self):
pass
def close(self):
if self._fd:
self._fd.close()
self._fd = None
def send(self, data):
"""
Send data via sendall.
@type data: string
@param data: Data to send
"""
passmgr = urllib2.HTTPPasswordMgr()
passmgr.add_password(self._realm, self._url, self._username, self._password)
auth_handler = urllib2.HTTPDigestAuthHandler(passmgr)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
req = urllib2.Request(self._url, data, self._headers)
try:
self._fd = urllib2.urlopen(req)
except:
self._fd = None
def receive(self, size=None):
"""
Receive upto 10000 bytes of data.
@rtype: string
@return: received data.
"""
if self._fd:
if size is not None:
return self._fd.read(size)
return self._fd.read()
else:
return ''
class HttpDigestAuthDefaultRealm(Publisher):
"""
A simple HTTP publisher.
"""
def __init__(self, url, authurl, username, password, headers=None, timeout=0.1):
Publisher.__init__(self)
self._url = url
self._authurl = authurl
self._username = username
self._password = password
self._headers = eval(headers)
self._timeout = float(timeout)
self._fd = None
def stop(self):
self.close()
def connect(self):
pass
def close(self):
if self._fd:
self._fd.close()
self._fd = None
def send(self, data):
passmgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
passmgr.add_password(None, self._authurl, self._username, self._password)
auth_handler = urllib2.HTTPDigestAuthHandler(passmgr)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
req = urllib2.Request(self._url, data, self._headers)
try:
self._fd = urllib2.urlopen(req)
except:
self._fd = None
def receive(self, size=None):
"""
Receive upto 10000 bytes of data.
@rtype: string
@return: received data.
"""
if self._fd:
if size is not None:
return self._fd.read(size)
return self._fd.read()
else:
return ''
class HttpBasicAuth(Publisher):
"""
A simple HTTP publisher.
"""
def __init__(self, url, realm, username, password, headers=None, timeout=0.1):
"""
@type host: string
@param host: Remote host
@type port: number
@param port: Remote port
@type timeout: number
@param timeout: How long to wait for reponse
"""
Publisher.__init__(self)
self._url = url
self._realm = realm
self._username = username
self._password = password
self._headers = headers
self._timeout = float(timeout)
self._fd = None
def start(self):
"""
Create connection.
"""
pass
def stop(self):
self.close()
def close(self):
"""
Close connection if open.
"""
if self._fd:
self._fd.close()
self._fd = None
def send(self, data):
"""
Send data via sendall.
@type data: string
@param data: Data to send
"""
passmgr = urllib2.HTTPPasswordMgr()
passmgr.add_password(self._realm, self._url, self._username, self._password)
auth_handler = urllib2.HTTPBasicAuthHandler(passmgr)
opener = urllib2.build_opener(auth_handler)
urllib2.install_opener(opener)
req = urllib2.Request(self._url, data, self._headers)
try:
self._fd = urllib2.urlopen(req)
except:
self._fd = None
def receive(self, size=None):
"""
Receive upto 10000 bytes of data.
@rtype: string
@return: received data.
"""
if self._fd:
if size is not None:
return self._fd.read(size)
return self._fd.read()
else:
return ''