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


Python SRVConnector.disconnect方法代码示例

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


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

示例1: __init__

# 需要导入模块: from twisted.names.srvconnect import SRVConnector [as 别名]
# 或者: from twisted.names.srvconnect.SRVConnector import disconnect [as 别名]
class XMPPClient:
    '''
    The XMPPClients uses the username, server and password to connect to the xmpp server.
    
    A message can be send using the sendMessage function, received presences and messages
    can be processed by a callback function.
    
    If the connection fails or is lost, the client will try to reconnect automatically.
    The disconnectedCallback can be used to catch the connection failed or connection lost event.
    At startup, a watchdog is started: if the server can't authenticate the user within the
    timeOut, the connection will be closed and the disconnectedCallback will be called. 
    '''
    
    def __init__(self, username, server, password, hostname, timeOut=5):
        self.username = username
        self.server = server
        self.password = password
        self.timeOut = timeOut
        self.hostname = hostname
        self.xmpp_user = None

        self.status = 'NOT_CONNECTED'
        
        self.messageReceivedCallback = None
        self.presenceReceivedCallback = None
        self.connectedCallback = None
        self.disconnectedCallback = None
        
        self.connector = None
        self.xmlstream = None
    
    def start(self):
        ''' Start the xmpp client, opens the connection to the server '''
        if self.status <> 'NOT_CONNECTED':
            raise RuntimeError('The XmppClient has already been started.')
        
        q.logger.log("[XMPPCLIENT] Starting the xmpp client to " + self.username + "@" + self.server, 5)
        
        self.startup_watchdog = reactor.callLater(self.timeOut, self._watchdog_timeout)
        self._connect()
    
    def stop(self):
        ''' Stop the xmpp client '''
        if self.status == 'NOT_CONNECTED':
            raise RuntimeError('The XmppClient has not yet been started.')
        
        q.logger.log("[XMPPCLIENT] Stopping the xmpp client to " + self.username + "@" + self.server, 5)
        self.connector.disconnect()
    
    def sendMessage(self, to, type, id, message=' '):
        ''' Send a message
        @param to: The username of the client to send the message to
        @type to: string
        @param type: The type of the message
        @type type: string
        @param id: The id of the message
        @type id: string
        @param message: The message to send
        @type message: string 
        '''
        if self.status <> 'RUNNING':
            raise NotConnectedException()
        
        q.logger.log("[XMPPCLIENT] Sending message '" + str(id) + "' of type '" + str(type) +"' to " + str(to) + " for " + self.username + "@" + self.server, 5)
        
        elemToSend = domish.Element(('jabber:client','message'), attribs={'to':to+"@"+self.server, 'type':type, 'id':id})
        body = domish.Element((None, 'body'))
        body.addContent(message)
        elemToSend.addContent(body)
        self.xmlstream.send(elemToSend)
    
    def sendPresence(self, to=None, type=None):
        ''' Send a presence
        @param to: The username of the client to send the presence to. None=send to all your friends
        @type to: string
        @param type: The type of the presence. Possible values: None=available, unavailable, subscribe, subscribed 
        @type type: string
        '''
        if self.status <> 'RUNNING':
            raise NotConnectedException()
        
        q.logger.log("[XMPPCLIENT] Sending presence of type '" + str(type) +"' to " + str(to) + "'", 5)
        
        attribs={}
        if to <> None: attribs['to'] =  to+"@"+self.server
        if type <> None: attribs['type'] = type
        
        presence = domish.Element(('jabber:client','presence'), attribs=attribs)
        self.xmlstream.send(presence)
    
    def _presence_received(self, elem):
        fromm = elem.getAttribute('from').split("@")[0]
        if not elem.hasAttribute('type'):
            type = 'available'
        else:
            type = elem.getAttribute('type')
            
        q.logger.log("[XMPPCLIENT] Presence received from '" + fromm + "' of type '" + type +"'", 5)
        
        if self.presenceReceivedCallback:
#.........这里部分代码省略.........
开发者ID:racktivity,项目名称:ext-pylabs_agent,代码行数:103,代码来源:xmppclient.py


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