Пример #1
0
    def handle(cls, request):
        try:

            if request['head']['type'] in TYPE and request['head']['name'] in NAME:
                controller = NAME.get(request['head']['name'])

                if 'session_id' not in request['head']:
                    return controller(request['body'])
                else:
                    return controller(request['body'], request['head']['session_id'])

            else:
                print('unknown_request')
                return shortcuts.unknown_request
        except Exception as err:
            print(err)
            return shortcuts.internal_server_error(err)
Пример #2
0
    def data_received(self, data):
        print('Data received: {}'.format(data))

        try:
            message=bytes_to_json(data)
            #Передаем декодирование сообщение на обратоку.
            answer=self.controler.handle(message)

            #Кодируем
            data=json_to_bytes(answer)

            #отсылаем ответ
            print(answer)
            self.transport.write(data)
        except Exception as err:
            json=internal_server_error(err)
            answer=json_to_bytes(json)
            self.transport.write(answer)
            return err