def handler(eniron, start_response):
    prefix = eniron.get('PATH_PREFIX', None)
    uri = eniron['PATH_INFO']
    if prefix and uri.startswith(prefix):
        uri = uri[len(prefix):]
    match = canteen_request.match(uri)
    if not match:
        start_response("404 Wrong Path", [("Content-type", 'application/xml; charset=utf-8')])
        return ['<xml version="1.0"><info>{provider}/{canteen}/{feed}.xml</info></xml>']
    request = utils.Request(eniron)
    try:
        content = parse(request, *(match.group('dirs').split('/') + [match.group('file')]))
        content = content.encode('utf8')
        start_response('200 OK', [('Content-Type', 'application/xml; charset=utf-8'),
                                  ('Content-Length', str(len(content)))])
        return (content,)
    except utils.Redirect as e:
        start_response('301 Permanent Redirect', [('Location', e.location)])
        return ('',)
    except utils.ParserNotFound as e:
        start_response('404 Parser not found', [('Content-Type', 'text/plain; charset=utf-8')])
        return (e.reason,)
    except utils.SourceNotFound as e:
        start_response('404 Source not found', [('Content-Type', 'text/plain; charset=utf-8')])
        return (e.reason,)
    except utils.FeedNotFound as e:
        start_response('404 Feed not found', [('Content-Type', 'text/plain; charset=utf-8')])
        return (e.reason,)
    except utils.NotFoundError as e:
        start_response('404 Unknown file format', [('Content-Type', 'text/plain; charset=utf-8')])
        return (e.reason,)
    except Exception:
        traceback.print_exception(*sys.exc_info())
        start_response('500 Internal Server Error', [])
        return ('', )
def _available():
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect(utils.MASTER_SOCK)
        utils.send_flag(s, utils.Request(1).value)
        output = utils.receive_data(s)
        avail = utils.WorkerStatus(int(output)).name
        return avail
def handle_connection(conn, addr):
    global jobs, jobs_queue

    print('Connected by', addr)
    header = conn.recv(utils.HEADER_SIZE).decode()

    if header:
        token = header[:-1]
        flag = utils.Request(int(header[-1:]))

        if token != utils.TOKEN:
            utils.send(conn, "FATAL: Authentication Error")

        elif flag == utils.Request.GET_JOB_STATUS:
            job_id = utils.receive_data(conn)
            if job_id in jobs_output:
                utils.send(conn, utils.JobStatus.FINISHED.value)
            else:
                utils.send(conn, utils.JobStatus.WAITING.value)

        elif flag == utils.Request.GET_WORKER_STATUS:
            with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
                s.connect(utils.WORKER_SOCK)
                utils.send_flag(s, flag.value)
                output = utils.receive_data(s)
                utils.send(conn, output)

        elif flag == utils.Request.EXECUTE_JOB:
            code = utils.receive_data(conn)
            job_id = str(uuid.uuid4())
            thread = threading.Thread(target=forward_code,
                                      args=(job_id, code, flag))
            jobs[job_id] = thread
            jobs_queue.append(job_id)
            utils.send(conn, job_id)

        elif flag == utils.Request.GET_OUTPUT:
            job_id = utils.receive_data(conn)
            if job_id in jobs_output:
                utils.send(conn, jobs_output.get(job_id))
            elif job_id not in jobs:
                utils.send(conn, "FATAL: job id doesn't exist")
            else:
                with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
                    s.connect(utils.WORKER_SOCK)
                    utils.send_data(s, job_id, flag.value)
                    output = utils.receive_data(s)
                    try:
                        json.loads(output)
                        jobs_output[job_id] = output
                    except JSONDecodeError as e:
                        pass
                    utils.send(conn, output)

    conn.close()
    print('Disconnected from', addr)
def _get_output(job_id):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.connect(utils.MASTER_SOCK)
        utils.send_data(s, job_id, utils.Request(5).value)
        output = utils.receive_data(s)
        try:
            loads(output)
        except JSONDecodeError as e:
            output = {'message': output}
            return output
        return output
示例#5
0
    def _get_request(self, client: socket.socket) -> utils.Request:
        """
		Waits for a connexion from a client and return a
		Request object.
		:param client: The client we are listening to
		:type client: socket.socket
		:return: A request object of the request the client sent
		:rtype: utils.Request
		"""
        req = client.recv(1024).decode("utf-8")

        return utils.Request(req)
示例#6
0
def handle_connection(conn, addr):
    global jobs, jobs_queue

    print('Connected by', addr)
    header = conn.recv(utils.HEADER_SIZE).decode()

    if header:
        token = header[:-1]
        flag = utils.Request(int(header[-1:]))

        if token != utils.TOKEN:
            utils.send(conn, "FATAL: Authentication Error")

        elif flag == utils.Request.GET_WORKER_STATUS:
            if IS_RUNNING:
                utils.send(conn, utils.WorkerStatus.RUNNING.value)
            else:
                utils.send(conn, utils.WorkerStatus.FREE.value)

        elif flag == utils.Request.EXECUTE_JOB:
            text = utils.receive_data(conn)
            job_id = text[:36]
            code = text[36:]
            thread = threading.Thread(target=run_code, args=(job_id, code))
            jobs[job_id] = thread
            jobs_queue.append(job_id)

        elif flag == utils.Request.GET_OUTPUT:
            job_id = utils.receive_data(conn)
            if (job_id in jobs_output):
                utils.send(conn, jobs_output.get(job_id))
            else:
                utils.send(conn, "RUNNING")

    conn.close()
    print('Disconnected from', addr)