0ef514a33383939474c3a0e2ecf777a67a5eff7d
[plcapi.git] / pycurl / tests / test_multi3.py
1 #! /usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
3 # vi:ts=4:et
4 # $Id$
5
6 # same as test_multi2.py, but enforce some debugging and strange API-calls
7
8 import os, sys
9 try:
10     from cStringIO import StringIO
11 except ImportError:
12     from StringIO import StringIO
13 import pycurl
14
15
16 urls = (
17     "http://curl.haxx.se",
18     "http://www.python.org",
19     "http://pycurl.sourceforge.net",
20     "http://pycurl.sourceforge.net/THIS_HANDLE_IS_CLOSED",
21 )
22
23 # init
24 m = pycurl.CurlMulti()
25 m.handles = []
26 for url in urls:
27     c = pycurl.Curl()
28     # save info in standard Python attributes
29     c.url = url
30     c.body = StringIO()
31     c.http_code = -1
32     c.debug = 0
33     m.handles.append(c)
34     # pycurl API calls
35     c.setopt(c.URL, c.url)
36     c.setopt(c.WRITEFUNCTION, c.body.write)
37     m.add_handle(c)
38
39 # debug - close a handle
40 if 1:
41     c = m.handles[3]
42     c.debug = 1
43     c.close()
44
45 # get data
46 num_handles = len(m.handles)
47 while num_handles:
48     while 1:
49         ret, num_handles = m.perform()
50         if ret != pycurl.E_CALL_MULTI_PERFORM:
51             break
52     # currently no more I/O is pending, could do something in the meantime
53     # (display a progress bar, etc.)
54     m.select()
55
56 # close handles
57 for c in m.handles:
58     # save info in standard Python attributes
59     try:
60         c.http_code = c.getinfo(c.HTTP_CODE)
61     except pycurl.error:
62         # handle already closed - see debug above
63         assert c.debug
64         c.http_code = -1
65     # pycurl API calls
66     if 0:
67         m.remove_handle(c)
68         c.close()
69     elif 0:
70         # in the C API this is the wrong calling order, but pycurl
71         # handles this automatically
72         c.close()
73         m.remove_handle(c)
74     else:
75         # actually, remove_handle is called automatically on close
76         c.close()
77 m.close()
78
79 # print result
80 for c in m.handles:
81     data = c.body.getvalue()
82     if 0:
83         print "**********", c.url, "**********"
84         print data
85     else:
86         print "%-53s http_code %3d, %6d bytes" % (c.url, c.http_code, len(data))
87