1 from builtins
import range
2 from io
import StringIO
7 """Manager of multiple concurrent or overlapping HTTP requests. 9 This is a utility class acting as a pump of several overlapping 10 HTTP requests against any number of HTTP or HTTPS servers. It 11 uses a configurable number of simultaneous connections, ten by 12 default. The actual connection layer is handled using curl, and 13 the client classes need to aware of this to a limited degree. 15 The client supplies optional callback methods for initialising, 16 responding and handling errors on connections. At the very least 17 the request response callback should be defined. 19 This class is not designed for multi-threaded use. It employs 20 overlapping requests, but in a single thread. Only one thread 21 at a time should be calling `process()`; several threads may 22 call `.put()` provided the caller uses a mutex so that only one 23 thread calls into the method at a time.""" 25 def __init__(self, num_connections = 10, ssl_opts = None,
26 user_agent = None, request_headers = None,
27 request_init = None, request_respond = None,
28 request_error = None, handle_init = None):
29 """Initialise the request manager. The arguments are: 31 :arg num_connections: maximum number of simultaneous connections. 32 :arg ssl_opts: optional SSLOptions (Monitoring.Core.X509) for SSL 33 X509 parametre values, e.g. for X509 client authentication. 34 :arg user_agent: sets user agent identification string if defined. 35 :arg request_headers: if defined, specifies list of additional HTTP 36 request headers to be added to each request. 37 :arg request_init: optional callback to initialise requests; the 38 default assumes each task is a URL to access and sets the `URL` 39 property on the curl object to the task value. 40 :arg request_respond: callback for handling responses; at the very 41 minimum this should be defined as the default one does nothing. 42 :arg request_error: callback for handling connection errors; the 43 default one raises a RuntimeException. 44 :arg handle_init: callback for customising connection handles at 45 creation time; the callback will be invoked for each connection 46 object as it's created and queued to the idle connection list.""" 58 c.setopt(TIMEOUT, 300)
59 c.setopt(CONNECTTIMEOUT, 30)
60 c.setopt(FOLLOWLOCATION, 1)
61 c.setopt(MAXREDIRS, 5)
63 c.setopt(USERAGENT, user_agent)
65 c.setopt(CAPATH, ssl_opts.ca_path)
66 c.setopt(SSLCERT, ssl_opts.cert_file)
67 c.setopt(SSLKEY, ssl_opts.key_file)
69 c.setopt(SSLKEYPASSWD, ssl_opts.key_pass)
71 c.setopt(HTTPHEADER, request_headers)
76 """Default request initialisation callback.""" 80 """Default request error callback.""" 81 raise RuntimeError((task, errmsg, errno))
84 """Default request response callback.""" 88 """Add a new task. The task object should be a tuple and is 89 passed to ``request_init`` callback passed to the constructor.""" 93 """Process pending requests until none are left. 95 This method processes all requests queued with `.put()` until they 96 have been fully processed. It calls the ``request_respond`` callback 97 for all successfully completed requests, and ``request_error`` for 100 Any new requests added by callbacks by invoking ``put()`` are also 101 processed before returning.""" 103 while self.
queue or npending:
106 c.task = self.
queue.pop(0)
107 c.buffer = b = BytesIO()
108 c.setopt(WRITEFUNCTION, b.write)
110 self.
cm.add_handle(c)
114 ret, nhandles = self.
cm.perform()
115 if ret != E_CALL_MULTI_PERFORM:
119 numq, ok, err = self.
cm.info_read()
123 self.
cm.remove_handle(c)
129 for c, errno, errmsg
in err:
131 self.
cm.remove_handle(c)
def _request_init(self, c, url)
def _request_error(self, c, task, errmsg, errno)
def _request_respond(self, args)
def __init__(self, num_connections=10, ssl_opts=None, user_agent=None, request_headers=None, request_init=None, request_respond=None, request_error=None, handle_init=None)