def __init__( self, host, path, port=80, params={}, headers={}, timeout=61, username=None, password=None, min_tcp_ip_delay=0.25, max_tcp_ip_delay=16, min_http_delay=10, max_http_delay=240 ): """Store config and build the connection headers. """ self.host = host self.port = port self.path = path self.body = unicode_urlencode(params) if username and password: headers['Authorization'] = generate_auth_header(username, password) header_lines = [ 'POST %s HTTP/1.1' % self.path, 'Host: %s' % self.host, 'Content-Length: %s' % len(self.body), 'Content-Type: application/x-www-form-urlencoded' ] header_lines.extend([ '%s: %s' % (k, v) for k, v in headers.iteritems() ] + ['', ''] ) self.headers = '\r\n'.join(header_lines) self.timeout = timeout self.min_tcp_ip_delay = min_tcp_ip_delay self.max_tcp_ip_delay = max_tcp_ip_delay self.min_http_delay = min_http_delay self.max_http_delay = max_http_delay self.id = generate_hash()
def _post(self, items): data = unicode_urlencode({'items': items}) request = urllib2.Request( self.url, data=data, headers=self.headers ) status = urllib2.urlopen(request).getcode() logging.debug(status) return 200 <= status < 300
def post(self): # queue_name and limit are optional kwargs = {} queue_name = self.get_argument('queue_name', False) limit = self.get_argument('limit', False) if queue_name: kwargs['queue_name'] = queue_name if limit: kwargs['limit'] = limit tasks = fetch_tasks(**kwargs) len_tasks = len(tasks) if len_tasks == 0: self.set_status(204) check_pending = self.get_argument('check_pending') if not isinstance(check_pending, bool): check_pending = eval(check_pending) if check_pending: kwargs = queue_name and {'queue_name': queue_name} or {} if count_tasks(**kwargs) < 1: self.set_status(205) self.finish() else: # maintain a list of ids, which we pop from in _handle_response # so that we know when we're done self.task_ids = [] # build a dict of {task_id: status_code, ...} for each task # to return as the response self.status_codes = {} http = httpclient.AsyncHTTPClient(max_clients=len_tasks) for task in tasks: callback = self.async_callback(self._handle_response, task_id=task.id) kwargs = {} if task.params: kwargs = { 'method': 'POST', 'body': unicode_urlencode(task.params) } if options.request_timeout: kwargs['request_timeout'] = options.request_timeout http.fetch(task.url, callback=callback, **kwargs) self.task_ids.append(task.id)