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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
|
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vi:ts=4:et
from . import localhost
import pycurl
import pytest
import unittest
import select
from . import appmanager
from . import util
setup_module_1, teardown_module_1 = appmanager.setup(('app', 8380))
setup_module_2, teardown_module_2 = appmanager.setup(('app', 8381))
setup_module_3, teardown_module_3 = appmanager.setup(('app', 8382))
def setup_module(mod):
setup_module_1(mod)
setup_module_2(mod)
setup_module_3(mod)
def teardown_module(mod):
teardown_module_3(mod)
teardown_module_2(mod)
teardown_module_1(mod)
class MultiTest(unittest.TestCase):
def test_multi(self):
io1 = util.BytesIO()
io2 = util.BytesIO()
m = pycurl.CurlMulti()
handles = []
c1 = util.DefaultCurl()
c2 = util.DefaultCurl()
c1.setopt(c1.URL, 'http://%s:8380/success' % localhost)
c1.setopt(c1.WRITEFUNCTION, io1.write)
c2.setopt(c2.URL, 'http://%s:8381/success' % localhost)
c2.setopt(c1.WRITEFUNCTION, io2.write)
m.add_handle(c1)
m.add_handle(c2)
handles.append(c1)
handles.append(c2)
num_handles = len(handles)
while num_handles:
while 1:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
m.select(1.0)
m.remove_handle(c2)
m.remove_handle(c1)
m.close()
c1.close()
c2.close()
self.assertEqual('success', io1.getvalue().decode())
self.assertEqual('success', io2.getvalue().decode())
def test_multi_select_fdset(self):
c1 = util.DefaultCurl()
c2 = util.DefaultCurl()
c3 = util.DefaultCurl()
c1.setopt(c1.URL, "http://%s:8380/success" % localhost)
c2.setopt(c2.URL, "http://%s:8381/success" % localhost)
c3.setopt(c3.URL, "http://%s:8382/success" % localhost)
c1.body = util.BytesIO()
c2.body = util.BytesIO()
c3.body = util.BytesIO()
c1.setopt(c1.WRITEFUNCTION, c1.body.write)
c2.setopt(c2.WRITEFUNCTION, c2.body.write)
c3.setopt(c3.WRITEFUNCTION, c3.body.write)
m = pycurl.CurlMulti()
m.add_handle(c1)
m.add_handle(c2)
m.add_handle(c3)
# Number of seconds to wait for a timeout to happen
SELECT_TIMEOUT = 0.1
# Stir the state machine into action
while 1:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
# Keep going until all the connections have terminated
while num_handles:
select.select(*m.fdset() + (SELECT_TIMEOUT,))
while 1:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
# Cleanup
m.remove_handle(c3)
m.remove_handle(c2)
m.remove_handle(c1)
m.close()
c1.close()
c2.close()
c3.close()
self.assertEqual('success', c1.body.getvalue().decode())
self.assertEqual('success', c2.body.getvalue().decode())
self.assertEqual('success', c3.body.getvalue().decode())
def test_multi_status_codes(self):
# init
m = pycurl.CurlMulti()
m.handles = []
urls = [
'http://%s:8380/success' % localhost,
'http://%s:8381/status/403' % localhost,
'http://%s:8382/status/404' % localhost,
]
for url in urls:
c = util.DefaultCurl()
# save info in standard Python attributes
c.url = url.rstrip()
c.body = util.BytesIO()
c.http_code = -1
m.handles.append(c)
# pycurl API calls
c.setopt(c.URL, c.url)
c.setopt(c.WRITEFUNCTION, c.body.write)
m.add_handle(c)
# get data
num_handles = len(m.handles)
while num_handles:
while 1:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
# currently no more I/O is pending, could do something in the meantime
# (display a progress bar, etc.)
m.select(0.1)
# close handles
for c in m.handles:
# save info in standard Python attributes
c.http_code = c.getinfo(c.HTTP_CODE)
# pycurl API calls
m.remove_handle(c)
c.close()
m.close()
# check result
self.assertEqual('success', m.handles[0].body.getvalue().decode())
self.assertEqual(200, m.handles[0].http_code)
# bottle generated response body
self.assertEqual('forbidden', m.handles[1].body.getvalue().decode())
self.assertEqual(403, m.handles[1].http_code)
# bottle generated response body
self.assertEqual('not found', m.handles[2].body.getvalue().decode())
self.assertEqual(404, m.handles[2].http_code)
def check_adding_closed_handle(self, close_fn):
# init
m = pycurl.CurlMulti()
m.handles = []
urls = [
'http://%s:8380/success' % localhost,
'http://%s:8381/status/403' % localhost,
'http://%s:8382/status/404' % localhost,
]
for url in urls:
c = util.DefaultCurl()
# save info in standard Python attributes
c.url = url
c.body = util.BytesIO()
c.http_code = -1
c.debug = 0
m.handles.append(c)
# pycurl API calls
c.setopt(c.URL, c.url)
c.setopt(c.WRITEFUNCTION, c.body.write)
m.add_handle(c)
# debug - close a handle
c = m.handles[2]
c.debug = 1
c.close()
# get data
num_handles = len(m.handles)
while num_handles:
while 1:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
# currently no more I/O is pending, could do something in the meantime
# (display a progress bar, etc.)
m.select(0.1)
# close handles
for c in m.handles:
# save info in standard Python attributes
try:
c.http_code = c.getinfo(c.HTTP_CODE)
except pycurl.error:
# handle already closed - see debug above
assert c.debug
c.http_code = -1
# pycurl API calls
close_fn(m, c)
m.close()
# check result
self.assertEqual('success', m.handles[0].body.getvalue().decode())
self.assertEqual(200, m.handles[0].http_code)
# bottle generated response body
self.assertEqual('forbidden', m.handles[1].body.getvalue().decode())
self.assertEqual(403, m.handles[1].http_code)
# bottle generated response body
self.assertEqual('', m.handles[2].body.getvalue().decode())
self.assertEqual(-1, m.handles[2].http_code)
def _remove_then_close(self, m, c):
m.remove_handle(c)
c.close()
def _close_then_remove(self, m, c):
# in the C API this is the wrong calling order, but pycurl
# handles this automatically
c.close()
m.remove_handle(c)
def _close_without_removing(self, m, c):
# actually, remove_handle is called automatically on close
c.close
def test_adding_closed_handle_remove_then_close(self):
self.check_adding_closed_handle(self._remove_then_close)
def test_adding_closed_handle_close_then_remove(self):
self.check_adding_closed_handle(self._close_then_remove)
def test_adding_closed_handle_close_without_removing(self):
self.check_adding_closed_handle(self._close_without_removing)
def test_multi_select(self):
c1 = util.DefaultCurl()
c2 = util.DefaultCurl()
c3 = util.DefaultCurl()
c1.setopt(c1.URL, "http://%s:8380/success" % localhost)
c2.setopt(c2.URL, "http://%s:8381/success" % localhost)
c3.setopt(c3.URL, "http://%s:8382/success" % localhost)
c1.body = util.BytesIO()
c2.body = util.BytesIO()
c3.body = util.BytesIO()
c1.setopt(c1.WRITEFUNCTION, c1.body.write)
c2.setopt(c2.WRITEFUNCTION, c2.body.write)
c3.setopt(c3.WRITEFUNCTION, c3.body.write)
m = pycurl.CurlMulti()
m.add_handle(c1)
m.add_handle(c2)
m.add_handle(c3)
# Number of seconds to wait for a timeout to happen
SELECT_TIMEOUT = 1.0
# Stir the state machine into action
while 1:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
# Keep going until all the connections have terminated
while num_handles:
# The select method uses fdset internally to determine which file descriptors
# to check.
m.select(SELECT_TIMEOUT)
while 1:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
# Cleanup
m.remove_handle(c3)
m.remove_handle(c2)
m.remove_handle(c1)
m.close()
c1.close()
c2.close()
c3.close()
self.assertEqual('success', c1.body.getvalue().decode())
self.assertEqual('success', c2.body.getvalue().decode())
self.assertEqual('success', c3.body.getvalue().decode())
def test_multi_info_read(self):
c1 = util.DefaultCurl()
c2 = util.DefaultCurl()
c3 = util.DefaultCurl()
c1.setopt(c1.URL, "http://%s:8380/short_wait" % localhost)
c2.setopt(c2.URL, "http://%s:8381/short_wait" % localhost)
c3.setopt(c3.URL, "http://%s:8382/short_wait" % localhost)
c1.body = util.BytesIO()
c2.body = util.BytesIO()
c3.body = util.BytesIO()
c1.setopt(c1.WRITEFUNCTION, c1.body.write)
c2.setopt(c2.WRITEFUNCTION, c2.body.write)
c3.setopt(c3.WRITEFUNCTION, c3.body.write)
m = pycurl.CurlMulti()
m.add_handle(c1)
m.add_handle(c2)
m.add_handle(c3)
# Number of seconds to wait for a timeout to happen
SELECT_TIMEOUT = 1.0
# Stir the state machine into action
while 1:
ret, num_handles = m.perform()
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
infos = []
# Keep going until all the connections have terminated
while num_handles:
# The select method uses fdset internally to determine which file descriptors
# to check.
m.select(SELECT_TIMEOUT)
while 1:
ret, num_handles = m.perform()
info = m.info_read()
infos.append(info)
if ret != pycurl.E_CALL_MULTI_PERFORM:
break
all_handles = []
for info in infos:
handles = info[1]
# last info is an empty array
if handles:
all_handles.extend(handles)
self.assertEqual(3, len(all_handles))
assert c1 in all_handles
assert c2 in all_handles
assert c3 in all_handles
# Cleanup
m.remove_handle(c3)
m.remove_handle(c2)
m.remove_handle(c1)
m.close()
c1.close()
c2.close()
c3.close()
self.assertEqual('success', c1.body.getvalue().decode())
self.assertEqual('success', c2.body.getvalue().decode())
self.assertEqual('success', c3.body.getvalue().decode())
def test_multi_close(self):
m = pycurl.CurlMulti()
m.close()
def test_multi_close_twice(self):
m = pycurl.CurlMulti()
m.close()
m.close()
# positional arguments are rejected
def test_positional_arguments(self):
with pytest.raises(TypeError):
pycurl.CurlMulti(1)
# keyword arguments are rejected
def test_keyword_arguments(self):
with pytest.raises(TypeError):
pycurl.CurlMulti(a=1)
|