def do_POST(self):
            form = cgi.FieldStorage(fp=self.rfile, headers=self.headers,
                                    environ={'REQUEST_METHOD': 'POST'})
            if self.path == '/node_jobs':
                ip_addr = None
                for item in form.list:
                    if item.name == 'host':
                        # if it looks like IP address, skip resolving
                        if re.match('^\d+[\.\d]+$', item.value):
                            ip_addr = item.value
                        else:
                            try:
                                ip_addr = socket.gethostbyname(item.value)
                            except:
                                ip_addr = item.value
                        break
                self._dispy_ctx._cluster_lock.acquire()
                cluster_infos = [(name, cluster_info) for name, cluster_info in
                                 self._dispy_ctx._clusters.items()]
                self._dispy_ctx._cluster_lock.release()
                jobs = []
                node = None
                for name, cluster_info in cluster_infos:
                    cluster_node = cluster_info.status.get(ip_addr, None)
                    if not cluster_node:
                        continue
                    if node:
                        node.jobs_done += cluster_node.jobs_done
                        node.cpu_time += cluster_node.cpu_time
                        node.update_time = max(node.update_time, cluster_node.update_time)
                    else:
                        node = copy.copy(cluster_node)
                    cluster_jobs = cluster_info.cluster.node_jobs(ip_addr)
                    # args and kwargs are sent as strings in Python,
                    # so an object's __str__ or __repr__ is used if provided;
                    # TODO: check job is in _dispy_ctx's jobs?
                    jobs.extend([{'uid': id(job), 'job_id': str(job.id),
                                  'args': ', '.join(str(arg) for arg in job.args),
                                  'kwargs': ', '.join('%s=%s' % (key, val)
                                                      for key, val in job.kwargs.items()),
                                  'sched_time_ms': int(1000 * job.start_time),
                                  'cluster': name}
                                 for job in cluster_jobs])
                self.send_response(200)
                self.send_header('Content-Type', 'application/json; charset=utf-8')
                self.end_headers()
                if node and node.avail_info:
                    node.avail_info = node.avail_info.__dict__
                self.wfile.write(json.dumps({'node': node.__dict__, 'jobs': jobs}).encode())
                return
            elif self.path == '/cancel_jobs':
                uids = []
                for item in form.list:
                    if item.name == 'uid':
                        try:
                            uids.append(int(item.value))
                        except __HOLE__:
                            logger.debug('Cancel job uid "%s" is invalid', item.value)

                self._dispy_ctx._cluster_lock.acquire()
                cluster_jobs = [(cluster_info.cluster, cluster_info.jobs.get(uid, None))
                                for cluster_info in self._dispy_ctx._clusters.values()
                                for uid in uids]
                self._dispy_ctx._cluster_lock.release()
                cancelled = []
                for cluster, job in cluster_jobs:
                    if not job:
                        continue
                    if cluster.cancel(job) == 0:
                        cancelled.append(id(job))
                self.send_response(200)
                self.send_header('Content-Type', 'application/json; charset=utf-8')
                self.end_headers()
                self.wfile.write(json.dumps(cancelled).encode())
                return
            elif self.path == '/add_node':
                node = {'host': '', 'port': None, 'cpus': 0, 'cluster': None}
                node_id = None
                cluster = None
                for item in form.list:
                    if item.name == 'host':
                        node['host'] = item.value
                    elif item.name == 'cluster':
                        node['cluster'] = item.value
                    elif item.name == 'port':
                        node['port'] = item.value
                    elif item.name == 'cpus':
                        try:
                            node['cpus'] = int(item.value)
                        except:
                            pass
                    elif item.name == 'id':
                        node_id = item.value
                if node['host']:
                    self._dispy_ctx._cluster_lock.acquire()
                    clusters = [cluster_info.cluster for name, cluster_info in
                                self._dispy_ctx._clusters.items()
                                if name == node['cluster'] or not node['cluster']]
                    self._dispy_ctx._cluster_lock.release()
                    for cluster in clusters:
                        cluster.allocate_node(node)
                    self.send_response(200)
                    self.send_header('Content-Type', 'text/html')
                    self.end_headers()
                    node['id'] = node_id
                    self.wfile.write(json.dumps(node).encode())
                    return
            elif self.path == '/set_poll_sec':
                for item in form.list:
                    if item.name != 'timeout':
                        continue
                    try:
                        timeout = int(item.value)
                        if timeout < 1:
                            timeout = 0
                    except:
                        logger.warning('HTTP client %s: invalid timeout "%s" ignored',
                                       self.client_address[0], item.value)
                        timeout = 0
                    self._dispy_ctx._poll_sec = timeout
                    self.send_response(200)
                    self.send_header('Content-Type', 'text/html')
                    self.end_headers()
                    return
            elif self.path == '/set_cpus':
                node_cpus = {}
                for item in form.list:
                    self._dispy_ctx._cluster_lock.acquire()
                    for cluster_info in self._dispy_ctx._clusters.values():
                        node = cluster_info.status.get(item.name, None)
                        if node:
                            node_cpus[item.name] = cluster_info.cluster.set_node_cpus(
                                item.name, item.value)
                            if node_cpus[item.name] >= 0:
                                break
                    self._dispy_ctx._cluster_lock.release()

                self.send_response(200)
                self.send_header('Content-Type', 'application/json; charset=utf-8')
                self.end_headers()
                self.wfile.write(json.dumps(node_cpus).encode())
                return

            logger.debug('Bad POST request from %s: %s', self.client_address[0], self.path)
            self.send_error(400)
            return
dataset/ETHPy150Open pgiri/dispy/py2/dispy/httpd.py/DispyHTTPServer._HTTPRequestHandler.do_POST