6b143aaba2af859d1c145c0abedfc06a09d41c04
[plcapi.git] / pycurl / tests / test_multi4.py
1 #! /usr/bin/env python
2 # -*- coding: iso-8859-1 -*-
3 # vi:ts=4:et
4 # $Id: test_multi4.py,v 1.13 2003/04/21 18:46:10 mfx Exp $
5
6 import sys, select, time
7 import pycurl
8
9 c1 = pycurl.Curl()
10 c2 = pycurl.Curl()
11 c3 = pycurl.Curl()
12 c1.setopt(c1.URL, "http://www.python.org")
13 c2.setopt(c2.URL, "http://curl.haxx.se")
14 c3.setopt(c3.URL, "http://slashdot.org")
15 c1.body = open("doc1", "wb")
16 c2.body = open("doc2", "wb")
17 c3.body = open("doc3", "wb")
18 c1.setopt(c1.WRITEFUNCTION, c1.body.write)
19 c2.setopt(c2.WRITEFUNCTION, c2.body.write)
20 c3.setopt(c3.WRITEFUNCTION, c3.body.write)
21
22 m = pycurl.CurlMulti()
23 m.add_handle(c1)
24 m.add_handle(c2)
25 m.add_handle(c3)
26
27 # Number of seconds to wait for a timeout to happen
28 SELECT_TIMEOUT = 10
29
30 # Stir the state machine into action
31 while 1:
32     ret, num_handles = m.perform()
33     if ret != pycurl.E_CALL_MULTI_PERFORM:
34         break
35
36 # Keep going until all the connections have terminated
37 while num_handles:
38     apply(select.select, m.fdset() + (SELECT_TIMEOUT,))
39     while 1:
40         ret, num_handles = m.perform()
41         if ret != pycurl.E_CALL_MULTI_PERFORM:
42             break
43
44 # Cleanup
45 m.remove_handle(c3)
46 m.remove_handle(c2)
47 m.remove_handle(c1)
48 m.close()
49 c1.body.close()
50 c2.body.close()
51 c3.body.close()
52 c1.close()
53 c2.close()
54 c3.close()
55 print "http://www.python.org is in file doc1"
56 print "http://curl.haxx.se is in file doc2"
57 print "http://slashdot.org is in file doc3"