Debian Codegraph @

examples/retriever-multi.py

raw
1 #! /usr/bin/env python
2 # vi:ts=4:et
3
4 #
5 # Usage: python retriever-multi.py <file with URLs to fetch> [<# of
6 # concurrent connections>]
7 #
8
9 import sys
10 import pycurl
11
12 # We should ignore SIGPIPE when using pycurl.NOSIGNAL - see
13 # the libcurl tutorial for more info.
14 try:
15 import signal
16 from signal import SIGPIPE, SIG_IGN
17 except ImportError:
18 pass
19 else:
20 signal.signal(SIGPIPE, SIG_IGN)
21
22
23
24 # Get args
25 num_conn = 10
26 try:
27 if sys.argv[1] == "-":
28 urls = sys.stdin.readlines()
29 else:
30 urls = open(sys.argv[1]).readlines()
31 if len(sys.argv) >= 3:
32 num_conn = int(sys.argv[2])
33 except:
34 print("Usage: %s <file with URLs to fetch> [<# of concurrent connections>]" % sys.argv[0])
35 raise SystemExit
36
37
38 # Make a queue with (url, filename) tuples
39 queue = []
40 for url in urls:
41 url = url.strip()
42 if not url or url[0] == "#":
43 continue
44 filename = "doc_%03d.dat" % (len(queue) + 1)
45 queue.append((url, filename))
46
47
48 # Check args
49 assert queue, "no URLs given"
50 num_urls = len(queue)
51 num_conn = min(num_conn, num_urls)
52 assert 1 <= num_conn <= 10000, "invalid number of concurrent connections"
53 print("PycURL %s (compiled against 0x%x)" % (pycurl.version, pycurl.COMPILE_LIBCURL_VERSION_NUM))
54 print("----- Getting", num_urls, "URLs using", num_conn, "connections -----")
55
56
57 # Pre-allocate a list of curl objects
58 m = pycurl.CurlMulti()
59 m.handles = []
60 for i in range(num_conn):
61 c = pycurl.Curl()
62 c.fp = None
63 c.setopt(pycurl.FOLLOWLOCATION, 1)
64 c.setopt(pycurl.MAXREDIRS, 5)
65 c.setopt(pycurl.CONNECTTIMEOUT, 30)
66 c.setopt(pycurl.TIMEOUT, 300)
67 c.setopt(pycurl.NOSIGNAL, 1)
68 m.handles.append(c)
69
70
71 # Main loop
72 freelist = m.handles[:]
73 num_processed = 0
74 while num_processed < num_urls:
75 # If there is an url to process and a free curl object, add to multi stack
76 while queue and freelist:
77 url, filename = queue.pop(0)
78 c = freelist.pop()
79 c.fp = open(filename, "wb")
80 c.setopt(pycurl.URL, url)
81 c.setopt(pycurl.WRITEDATA, c.fp)
82 m.add_handle(c)
83 # store some info
84 c.filename = filename
85 c.url = url
86 # Run the internal curl state machine for the multi stack
87 _, num_handles = m.perform()
88 # Check for curl objects which have terminated, and add them to the freelist
89 while 1:
90 num_q, ok_list, err_list = m.info_read()
91 for c in ok_list:
92 c.fp.close()
93 c.fp = None
94 m.remove_handle(c)
95 print("Success:", c.filename, c.url, c.getinfo(pycurl.EFFECTIVE_URL))
96 freelist.append(c)
97 for c, errno, errmsg in err_list:
98 c.fp.close()
99 c.fp = None
100 m.remove_handle(c)
101 print("Failed: ", c.filename, c.url, errno, errmsg)
102 freelist.append(c)
103 num_processed = num_processed + len(ok_list) + len(err_list)
104 if num_q == 0:
105 break
106 # Currently no more I/O is pending, could do something in the meantime
107 # (display a progress bar, etc.).
108 # We just call select() to sleep until some more data is available.
109 m.select(1.0)
110
111
112 # Cleanup
113 for c in m.handles:
114 if c.fp is not None:
115 c.fp.close()
116 c.fp = None
117 c.close()
118 m.close()
119