set svn:keywords property for proper keywords expansion
[plcapi.git] / pycurl / tests / test_multi2.py
1 #! /usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
3 # vi:ts=4:et
4 # $Id$
5
6 import os, sys
7 try:
8     from cStringIO import StringIO
9 except ImportError:
10     from StringIO import StringIO
11 import pycurl
12
13
14 urls = (
15     "http://curl.haxx.se",
16     "http://www.python.org",
17     "http://pycurl.sourceforge.net",
18     "http://pycurl.sourceforge.net/tests/403_FORBIDDEN",  # that actually exists ;-)
19     "http://pycurl.sourceforge.net/tests/404_NOT_FOUND",
20 )
21
22 # Read list of URIs from file specified on commandline
23 try:
24     urls = open(sys.argv[1], "rb").readlines()
25 except IndexError:
26     # No file was specified
27     pass
28
29 # init
30 m = pycurl.CurlMulti()
31 m.handles = []
32 for url in urls:
33     c = pycurl.Curl()
34     # save info in standard Python attributes
35     c.url = url
36     c.body = StringIO()
37     c.http_code = -1
38     m.handles.append(c)
39     # pycurl API calls
40     c.setopt(c.URL, c.url)
41     c.setopt(c.WRITEFUNCTION, c.body.write)
42     m.add_handle(c)
43
44 # get data
45 num_handles = len(m.handles)
46 while num_handles:
47      while 1:
48          ret, num_handles = m.perform()
49          if ret != pycurl.E_CALL_MULTI_PERFORM:
50              break
51      # currently no more I/O is pending, could do something in the meantime
52      # (display a progress bar, etc.)
53      m.select()
54
55 # close handles
56 for c in m.handles:
57     # save info in standard Python attributes
58     c.http_code = c.getinfo(c.HTTP_CODE)
59     # pycurl API calls
60     m.remove_handle(c)
61     c.close()
62 m.close()
63
64 # print result
65 for c in m.handles:
66     data = c.body.getvalue()
67     if 0:
68         print "**********", c.url, "**********"
69         print data
70     else:
71         print "%-53s http_code %3d, %6d bytes" % (c.url, c.http_code, len(data))
72