当前位置: 首页>>代码示例>>Python>>正文


Python Logger.critical方法代码示例

本文整理汇总了Python中twisted.logger.Logger.critical方法的典型用法代码示例。如果您正苦于以下问题:Python Logger.critical方法的具体用法?Python Logger.critical怎么用?Python Logger.critical使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在twisted.logger.Logger的用法示例。


在下文中一共展示了Logger.critical方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Modem

# 需要导入模块: from twisted.logger import Logger [as 别名]
# 或者: from twisted.logger.Logger import critical [as 别名]
class Modem(object):
    '''
    classdocs
    '''

    def __init__(self, protocol, event_fct=None):
        '''
        Constructor
        '''
        self.log = Logger()
        self.first = True
        self.event = event_fct
        self.callback = None
        self.wait = False
        self.response = ''
        self.protocol = protocol
        self.protocol.addCallback(self.receive)
        self.resp_re = re.compile(
                    r'^OK|ERROR|(\+CM[ES] ERROR: \d+)|(COMMAND NOT SUPPORT)$')
        
    def receive(self, line):
        if self.wait:
            if self.resp_re.match(line):
                self.wait = False
                self.response.append(line)
                if line.startswith('ERROR'):
                    self.log.critical('error from Modem: %s' % line)
                    if self.callback:
                        self.callback.errback(self.response)
                else:
                    if self.callback:
                        self.callback.callback(self.response)
                self.response = ''
                if self.callback:
                    self.callback = None
            else:
                self.response.append(line)
        elif self.event:
            self.event(line)   
        else:
            self.log.debug('unmanaged message from Modem: %s' % line)
            
    def sendsms(self, recipient, message, callback_fct=None):
        def recipient_set(res):
            self.log.debug(
                'do we have > ? ==> %s' % ('OK' if res == '>' else 'No: ' + res))
            self.callback = Deferred
            if callback_fct:
                self.callback.addCallback(callback_fct)
            self.wait = True
            self.protocol.write(message + b'\x1a')
        def text_mode(res):
            self.callback = Deferred
            self.callback.addCallback(recipient_set)
            self.wait = True
            self.protocol.write(b'AT+CMGS="' + recipient.encode() + b'"\r')
        def modem_init(res):
            self.first = False
            self.callback = Deferred
            self.callback.addCallback(text_mode)
            self.wait = True
            self.protocol.write(b'AT+CMGF=1\r')
        if self.first:
            self.wait = True
            self.callback = Deferred()
            self.callback.addCallback(modem_init)
            self.protocol.write(b'ATZ\r')
        else:
            modem_init('OK')
        
                
开发者ID:lazytech-org,项目名称:onDemand,代码行数:71,代码来源:commands.py

示例2: SmsFactory

# 需要导入模块: from twisted.logger import Logger [as 别名]
# 或者: from twisted.logger.Logger import critical [as 别名]
class SmsFactory(ClientFactory, Client):
    room = 'NA'
    actions = ('sendsms, readsms')

    def __init__(self, event_fct=None):
        self.protocol = serialLineProtocol()
        self.uid = uuid.uuid4()
        self.protocol.factory = self
        self.log = Logger()
        self.first = True
        self.event = event_fct
        self.callback = None
        self.wait = False
        self.response = ''
        self.resp_re = re.compile(
            r'^OK|ERROR|(\+CM[ES] ERROR: \d+)|(COMMAND NOT SUPPORT)$')

    def receive(self, line):
        if self.wait:
            if self.resp_re.match(line):
                self.wait = False
                self.response.append(line)
                if line.startswith('ERROR'):
                    self.log.critical('error from Modem: %s' % line)
                    if self.callback:
                        self.callback.errback(self.response)
                else:
                    if self.callback:
                        self.callback.callback(self.response)
                self.response = ''
                if self.callback:
                    self.callback = None
            else:
                self.response.append(line)
        elif self.event:
            self.event(line)
        else:
            self.log.debug('unmanaged message from Modem: %s' % line)

    def sendsms(self, recipient, message, callback_fct=None):
        def recipient_set(res):
            self.log.debug(
                'do we have > ? ==> %s' % ('OK' if res == '>' else 'No: ' + res))
            self.callback = defer.Deferred
            if callback_fct:
                self.callback.addCallback(callback_fct)
            self.wait = True
            self.protocol.send(message + b'\x1a')

        def text_mode(res):
            self.callback = defer.Deferred
            self.callback.addCallback(recipient_set)
            self.wait = True
            self.protocol.send(b'AT+CMGS="' + recipient.encode() + b'"\r')

        def modem_init(res):
            self.first = False
            self.callback = defer.Deferred
            self.callback.addCallback(text_mode)
            self.wait = True
            self.protocol.send(b'AT+CMGF=1\r')
        if self.first:
            self.wait = True
            self.callback = defer.Deferred()
            self.callback.addCallback(modem_init)
            self.protocol.send(b'ATZ\r')
        else:
            modem_init('OK')

    def _write(self, txt):
        self.protocol.send(txt.encode())
开发者ID:bverdu,项目名称:onDemand,代码行数:73,代码来源:__init__.py

示例3: Rest

# 需要导入模块: from twisted.logger import Logger [as 别名]
# 或者: from twisted.logger.Logger import critical [as 别名]
class Rest(object):

    def __init__(
            self,
            host='https://developer-api.nest.com',
            token=None,
            event_handler=None,
            net_type='lan'):
        self.log = Logger()
        self.host = host
        self.token = token
        self.event_handler = event_handler
        self.pool = HTTPConnectionPool(reactor, persistent=True)
        self.loc = None
        self.reconnect = False
        self.fail_count = 0
        if event_handler:
            self.reconnect = True
            d = self.request(headers={'User-Agent': ['onDemand Rest Client'],
                                      'Accept': ['text/event-stream']})
            d.addCallback(self.on_disconnect)

    def __getattr__(self, name):
        try:
            super(Rest, self).__getattr__(name)
        except AttributeError:
            return RestCall(self, name)

    def on_disconnect(self, reason):
        if not reason:
            reason = {'reason': 'no_message'}
        self.log.critical(
            'disconnected: {reason}', reason=reason['reason'])
        if self.fail_count > 10:
            self.log.error('Max error count reached, aborting connection')

        def test_connectivity(count):
            if self.fail_count == count:
                self.fail_count = 0

        self.fail_count += 1
        c = self.fail_count
        reactor.callLater(10, test_connectivity, c)  # @UndefinedVariable
        if self.reconnect:
            d = self.request(headers={'User-Agent': ['onDemand Rest Client'],
                                      'Accept': ['text/event-stream']})
            d.addCallback(self.on_disconnect)

    def request(self, method='GET',
                path='',
                headers={'User-Agent': ['onDemand/1.0 (Rest_Client)'],
                         'Accept': ['application/json']},
                body=None):

        data = None
        if self.loc:
            host = '/'.join((self.loc, path))
        else:
            host = '/'.join((self.host, path))
        if self.token:
            host += '?auth=' + self.token
        if body:
            headers.update({'Content-Type': ['application/json']})
            data = FileBodyProducer(StringIO(json.dumps(body)))
        agent = RedirectAgent(Agent(reactor, pool=self.pool))
        d = agent.request(method, host, Headers(headers), data)

        def cbFail(fail):

            if hasattr(fail.value, 'response'):
                if hasattr(fail.value.response, 'code'):
                    if fail.value.response.code == 307:
                        loc = fail.value.response.headers.getRawHeaders(
                            'location')
                        new = urlparse(loc[0])
                        newhost = '://'.join((new.scheme, new.netloc))
                        if newhost == self.host:
                            self.loc = None
                        else:
                            self.loc = newhost
                        self.log.debug('redirect: %s' % self.loc)
                        data = FileBodyProducer(StringIO(json.dumps(body)))
                        d = agent.request(
                            method, loc[0], Headers(headers), data)
                        d.addCallbacks(cbRequest, cbFail)
                        return d
                    elif fail.value.response.code == 404 and self.loc:
                        self.loc = None
                        host = '/'.join((self.host, path))
                        if self.token:
                            host += '?auth=' + self.token
                        d = self.request(method, host, Headers(headers), body)
                        d.addCallbacks(cbRequest, cbFail)
                        return d
                else:
                    print(dir(fail.value))
                    print(fail.value.message)
                    print(fail.value.args)

            self.log.error('unhandled failure: %s -- %s' % (
#.........这里部分代码省略.........
开发者ID:bverdu,项目名称:onDemand,代码行数:103,代码来源:rest.py


注:本文中的twisted.logger.Logger.critical方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。