CMS 3D CMS Logo

HTTP.py
Go to the documentation of this file.
1 from builtins import range
2 from cStringIO import StringIO
3 from pycurl import *
4 
6  """Manager of multiple concurrent or overlapping HTTP requests.
7 
8 This is a utility class acting as a pump of several overlapping
9 HTTP requests against any number of HTTP or HTTPS servers. It
10 uses a configurable number of simultaneous connections, ten by
11 default. The actual connection layer is handled using curl, and
12 the client classes need to aware of this to a limited degree.
13 
14 The client supplies optional callback methods for initialising,
15 responding and handling errors on connections. At the very least
16 the request response callback should be defined.
17 
18 This class is not designed for multi-threaded use. It employs
19 overlapping requests, but in a single thread. Only one thread
20 at a time should be calling `process()`; several threads may
21 call `.put()` provided the caller uses a mutex so that only one
22 thread calls into the method at a time."""
23 
24  def __init__(self, num_connections = 10, ssl_opts = None,
25  user_agent = None, request_headers = None,
26  request_init = None, request_respond = None,
27  request_error = None, handle_init = None):
28  """Initialise the request manager. The arguments are:
29 
30 :arg num_connections: maximum number of simultaneous connections.
31 :arg ssl_opts: optional SSLOptions (Monitoring.Core.X509) for SSL
32 X509 parametre values, e.g. for X509 client authentication.
33 :arg user_agent: sets user agent identification string if defined.
34 :arg request_headers: if defined, specifies list of additional HTTP
35 request headers to be added to each request.
36 :arg request_init: optional callback to initialise requests; the
37 default assumes each task is a URL to access and sets the `URL`
38 property on the curl object to the task value.
39 :arg request_respond: callback for handling responses; at the very
40 minimum this should be defined as the default one does nothing.
41 :arg request_error: callback for handling connection errors; the
42 default one raises a RuntimeException.
43 :arg handle_init: callback for customising connection handles at
44 creation time; the callback will be invoked for each connection
45 object as it's created and queued to the idle connection list."""
46  self.request_respond = request_respond or self._request_respond
47  self.request_error = request_error or self._request_error
48  self.request_init = request_init or self._request_init
49  self.cm = CurlMulti()
50  self.handles = [Curl() for i in range(0, num_connections)]
51  self.free = [c for c in self.handles]
52  self.queue = []
53 
54  for c in self.handles:
55  c.buffer = None
56  c.setopt(NOSIGNAL, 1)
57  c.setopt(TIMEOUT, 300)
58  c.setopt(CONNECTTIMEOUT, 30)
59  c.setopt(FOLLOWLOCATION, 1)
60  c.setopt(MAXREDIRS, 5)
61  if user_agent:
62  c.setopt(USERAGENT, user_agent)
63  if ssl_opts:
64  c.setopt(CAPATH, ssl_opts.ca_path)
65  c.setopt(SSLCERT, ssl_opts.cert_file)
66  c.setopt(SSLKEY, ssl_opts.key_file)
67  if ssl_opts.key_pass:
68  c.setopt(SSLKEYPASSWD, ssl_opts.key_pass)
69  if request_headers:
70  c.setopt(HTTPHEADER, request_headers)
71  if handle_init:
72  handle_init(c)
73 
74  def _request_init(self, c, url):
75  """Default request initialisation callback."""
76  c.setopt(URL, url)
77 
78  def _request_error(self, c, task, errmsg, errno):
79  """Default request error callback."""
80  raise RuntimeError((task, errmsg, errno))
81 
82  def _request_respond(self, *args):
83  """Default request response callback."""
84  pass
85 
86  def put(self, task):
87  """Add a new task. The task object should be a tuple and is
88 passed to ``request_init`` callback passed to the constructor."""
89  self.queue.append(task)
90 
91  def process(self):
92  """Process pending requests until none are left.
93 
94 This method processes all requests queued with `.put()` until they
95 have been fully processed. It calls the ``request_respond`` callback
96 for all successfully completed requests, and ``request_error`` for
97 all failed ones.
98 
99 Any new requests added by callbacks by invoking ``put()`` are also
100 processed before returning."""
101  npending = 0
102  while self.queue or npending:
103  while self.queue and self.free:
104  c = self.free.pop()
105  c.task = self.queue.pop(0)
106  c.buffer = b = StringIO()
107  c.setopt(WRITEFUNCTION, b.write)
108  self.request_init(c, *c.task)
109  self.cm.add_handle(c)
110  npending += 1
111 
112  while True:
113  ret, nhandles = self.cm.perform()
114  if ret != E_CALL_MULTI_PERFORM:
115  break
116 
117  while True:
118  numq, ok, err = self.cm.info_read()
119 
120  for c in ok:
121  assert npending > 0
122  self.cm.remove_handle(c)
123  self.request_respond(c)
124  c.buffer = None
125  self.free.append(c)
126  npending -= 1
127 
128  for c, errno, errmsg in err:
129  assert npending > 0
130  self.cm.remove_handle(c)
131  self.free.append(c)
132  npending -= 1
133  self.request_error(c, c.task, errmsg, errno)
134 
135  if numq == 0:
136  break
137 
138  self.cm.select(1.)
139 
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
HTTP.RequestManager._request_respond
def _request_respond(self, *args)
Definition: HTTP.py:82
HTTP.RequestManager
Definition: HTTP.py:5
singleTopDQM_cfi.select
select
Definition: singleTopDQM_cfi.py:50
HTTP.RequestManager.queue
queue
Definition: HTTP.py:49
HTTP.RequestManager.__init__
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)
Definition: HTTP.py:24
HTTP.RequestManager.request_error
request_error
Definition: HTTP.py:44
HTTP.RequestManager.free
free
Definition: HTTP.py:48
HTTP.RequestManager._request_init
def _request_init(self, c, url)
Definition: HTTP.py:74
HTTP.RequestManager._request_error
def _request_error(self, c, task, errmsg, errno)
Definition: HTTP.py:78
HTTP.RequestManager.put
def put(self, task)
Definition: HTTP.py:86
mps_setup.append
append
Definition: mps_setup.py:85
HTTP.RequestManager.handles
handles
Definition: HTTP.py:47
HTTP.RequestManager.request_respond
request_respond
Definition: HTTP.py:43
HTTP.RequestManager.process
def process(self)
Definition: HTTP.py:91
HTTP.RequestManager.request_init
request_init
Definition: HTTP.py:45
HTTP.RequestManager.cm
cm
Definition: HTTP.py:46