本文整理匯總了Python中pyxmpp2.streambase.StreamBase.receive方法的典型用法代碼示例。如果您正苦於以下問題:Python StreamBase.receive方法的具體用法?Python StreamBase.receive怎麽用?Python StreamBase.receive使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pyxmpp2.streambase.StreamBase
的用法示例。
在下文中一共展示了StreamBase.receive方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: TestBindingReceiver
# 需要導入模塊: from pyxmpp2.streambase import StreamBase [as 別名]
# 或者: from pyxmpp2.streambase.StreamBase import receive [as 別名]
class TestBindingReceiver(ReceiverSelectTestCase):
def test_bind_no_resource(self):
handler = EventRecorder()
handlers = [ResourceBindingHandler(), handler]
processor = StanzaProcessor()
self.start_transport(handlers)
self.stream = StreamBase(u"jabber:client", processor, handlers)
processor.uplink = self.stream
self.stream.receive(self.transport, self.addr[0])
self.stream.set_peer_authenticated(JID("[email protected]"))
processor.setup_stanza_handlers(handlers, "post-auth")
self.client.write(C2S_CLIENT_STREAM_HEAD)
features = self.wait(
expect = re.compile(br".*<stream:features>"
br"(.*<bind.*urn:ietf:params:xml:ns:xmpp-bind.*)"
br"</stream:features>"))
self.assertIsNotNone(features)
self.client.write(BIND_GENERATED_REQUEST)
resource = self.wait(
expect = re.compile(br".*<iq.*id=(?:\"42\"|'42').*>"
br"<bind.*<jid>[email protected]/(.*)</jid>.*</bind>"))
self.assertTrue(resource)
self.client.write(STREAM_TAIL)
self.client.disconnect()
self.wait()
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [AuthenticatedEvent,
StreamConnectedEvent, AuthorizedEvent, DisconnectedEvent])
def test_bind_resource(self):
handler = EventRecorder()
handlers = [ResourceBindingHandler(), handler]
processor = StanzaProcessor()
self.start_transport(handlers)
self.stream = StreamBase(u"jabber:client", processor, handlers)
processor.uplink = self.stream
self.stream.receive(self.transport, self.addr[0])
self.stream.set_peer_authenticated(JID("[email protected]"))
processor.setup_stanza_handlers(handlers, "post-auth")
self.client.write(C2S_CLIENT_STREAM_HEAD)
features = self.wait(
expect = re.compile(br".*<stream:features>"
br"(.*<bind.*urn:ietf:params:xml:ns:xmpp-bind.*)"
br"</stream:features>"))
self.assertIsNotNone(features)
self.client.write(BIND_PROVIDED_REQUEST)
resource = self.wait(
expect = re.compile(br".*<iq.*id=(?:\"42\"|'42').*>"
br"<bind.*<jid>[email protected]/(.*)</jid>.*</bind>"))
self.assertEqual(resource, b"Provided")
self.client.write(STREAM_TAIL)
self.client.disconnect()
self.wait()
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [AuthenticatedEvent,
StreamConnectedEvent, AuthorizedEvent, DisconnectedEvent])
示例2: TestReceiverSelect
# 需要導入模塊: from pyxmpp2.streambase import StreamBase [as 別名]
# 或者: from pyxmpp2.streambase.StreamBase import receive [as 別名]
class TestReceiverSelect(ReceiverSelectTestCase):
def test_stream_connect_disconnect(self):
handler = JustStreamConnectEventHandler()
self.start_transport([handler])
self.stream = StreamBase("jabber:client", None, [])
self.stream.receive(self.transport, self.addr[0])
self.client.write(C2S_CLIENT_STREAM_HEAD)
self.wait_short(0.25)
self.wait_short(0.25)
self.client.write(STREAM_TAIL)
self.wait()
self.assertFalse(self.stream.is_connected())
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [StreamConnectedEvent,
DisconnectedEvent])
def test_parse_error(self):
handler = IgnoreEventHandler()
self.start_transport([handler])
self.stream = StreamBase("jabber:client", None, [])
self.stream.receive(self.transport, self.addr[0])
self.client.write(C2S_CLIENT_STREAM_HEAD)
self.wait_short(0.25)
self.wait_short(0.25)
self.client.write(b"</stream:test>")
logger.debug("waiting for exception...")
with self.assertRaises(StreamParseError):
self.wait()
logger.debug(" got it!")
self.assertFalse(self.stream.is_connected())
self.wait_short(0.1)
logger.debug("waiting for connection close...")
self.client.wait(1)
logger.debug(" done")
self.assertTrue(self.client.eof)
self.assertTrue(self.client.rdata.endswith(PARSE_ERROR_RESPONSE))
self.client.disconnect()
logger.debug("final wait...")
self.wait()
logger.debug(" done")
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [StreamConnectedEvent,
DisconnectedEvent])
示例3: Server
# 需要導入模塊: from pyxmpp2.streambase import StreamBase [as 別名]
# 或者: from pyxmpp2.streambase.StreamBase import receive [as 別名]
class Server(StanzaProcessor, EventHandler, TimeoutHandler, XMPPFeatureHandler):
"""
The XMPP end of the proxy.
Handles local XMPP client connections. Forwards stanzas from the
client to the Tlen server, and the other way around. Stanzas are
adapted to achieve maximum satisfaction on both sides ;)
"""
def __init__(self, transport, avatars):
StanzaProcessor.__init__(self)
logger.debug('-- New connection')
self.avatars = avatars
self.settings = XMPPSettings()
self.handlers = [self, ResourceBindingHandler(self.settings)]
self.stream = StreamBase(u"jabber:client", self, self.handlers, self.settings)
self.stream.set_authenticated(JID(domain='tlen.pl'))
self.tlen = None
self.transport = transport
self.stream.receive(self.transport, 'tlen.pl')
self.uplink = self.stream
self.stream.set_authenticated(JID(domain='tlen.pl'))
@event_handler(AuthenticatedEvent)
def authenticated(self, event):
self.setup_stanza_handlers(self.handlers, 'post-auth')
@event_handler(DisconnectedEvent)
def disconnected(self, event):
logger.debug('Client disconnected')
if self.tlen:
self.tlen.close()
self.tlen = None
return QUIT
# Stanza handlers. Except for authentication stuff, they
# push everything to the TlenClient. Anything that needs
# fixing up before being sent to the server is performed
# in TlenClient.send()
@iq_get_stanza_handler(XMLPayload, '{jabber:iq:auth}query')
def handle_auth_get(self, stanza):
logger.debug('auth get %s', stanza.serialize())
resp = stanza.make_result_response()
query = ElementTree.Element('{jabber:iq:auth}query')
for x in ('username', 'password', 'resource'):
query.append(ElementTree.Element('{jabber:iq:auth}' + x))
resp.add_payload(query)
return resp
@iq_set_stanza_handler(XMLPayload, '{jabber:iq:auth}query')
def handle_auth_set(self, stanza):
"""
This is the part that actually starts the Tlen end
of the proxy.
We're expecting PLAIN XMPP authentication here, so we
can grab the auth data and log in to Tlen.pl, on behalf
of the user.
"""
query = stanza.get_payload(None, 'query').as_xml()
# XXX: Assuming the client will use legacy auth
username = query.findtext('{jabber:iq:auth}username')
password = query.findtext('{jabber:iq:auth}password')
resource = query.findtext('{jabber:iq:auth}resource')
if not username or not password:
# XXX
return stanza.make_error_response('bad-request')
self.tlen = TlenStream(JID(username, 'tlen.pl'), password, resource, self.avatars)
self.tlen.uplink = self.stream
self.tlen.start()
if self.tlen.wait_for_auth():
return stanza.make_result_response()
else:
# XXX: is this code ok?
return stanza.make_error_response('not-authorized')
@iq_get_stanza_handler(XMLPayload, '{jabber:iq:roster}query')
def handle_roster_get(self, stanza):
self.tlen.send(stanza)
@iq_set_stanza_handler(XMLPayload, '{jabber:iq:roster}query')
def handle_roster_set(self, stanza):
self.tlen.send(stanza)
#.........這裏部分代碼省略.........
示例4: TestReceiver
# 需要導入模塊: from pyxmpp2.streambase import StreamBase [as 別名]
# 或者: from pyxmpp2.streambase.StreamBase import receive [as 別名]
class TestReceiver(ReceiverSelectTestCase):
def test_auth(self):
handler = EventRecorder()
self.start_transport([handler])
settings = XMPPSettings({
"user_passwords": {
"user": "secret",
},
"sasl_mechanisms": ["SCRAM-SHA-1", "PLAIN"],
})
self.stream = StreamBase("jabber:client", None,
[StreamSASLHandler(settings), handler], settings)
self.stream.receive(self.transport, self.addr[0])
self.client.write(C2S_CLIENT_STREAM_HEAD)
xml = self.wait(expect = re.compile(
br".*<stream:features>(.*)</stream:features>"))
self.assertIsNotNone(xml)
element = ElementTree.XML(xml)
self.assertEqual(element.tag,
"{urn:ietf:params:xml:ns:xmpp-sasl}mechanisms")
self.assertEqual(element[0].tag,
"{urn:ietf:params:xml:ns:xmpp-sasl}mechanism")
self.assertEqual(element[0].text, "SCRAM-SHA-1")
self.assertEqual(element[1].tag,
"{urn:ietf:params:xml:ns:xmpp-sasl}mechanism")
self.assertEqual(element[1].text, "PLAIN")
response = base64.standard_b64encode(b"\000user\000secret")
self.client.write(PLAIN_AUTH.format(response.decode("utf-8"))
.encode("utf-8"))
xml = self.wait(expect = re.compile(br".*(<success.*>)"))
self.assertIsNotNone(xml)
self.client.write(C2S_CLIENT_STREAM_HEAD)
xml = self.wait(expect = re.compile(br".*(<stream:stream.*>)"))
self.assertIsNotNone(xml)
self.assertTrue(self.stream.peer_authenticated)
self.client.write(b"</stream:stream>")
self.client.disconnect()
self.wait()
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [
StreamConnectedEvent, AuthenticatedEvent,
StreamRestartedEvent, DisconnectedEvent])
def test_auth_fail(self):
handler = EventRecorder()
self.start_transport([handler])
settings = XMPPSettings({
"user_passwords": {
"user": "secret",
},
"sasl_mechanisms": ["SCRAM-SHA-1", "PLAIN"],
})
self.stream = StreamBase("jabber:client", None,
[StreamSASLHandler(settings), handler], settings)
self.stream.receive(self.transport, self.addr[0])
self.client.write(C2S_CLIENT_STREAM_HEAD)
xml = self.wait(expect = re.compile(
br".*<stream:features>(.*)</stream:features>"))
self.assertIsNotNone(xml)
element = ElementTree.XML(xml)
self.assertEqual(element.tag,
"{urn:ietf:params:xml:ns:xmpp-sasl}mechanisms")
self.assertEqual(element[0].tag,
"{urn:ietf:params:xml:ns:xmpp-sasl}mechanism")
self.assertEqual(element[0].text, "SCRAM-SHA-1")
self.assertEqual(element[1].tag,
"{urn:ietf:params:xml:ns:xmpp-sasl}mechanism")
self.assertEqual(element[1].text, "PLAIN")
response = base64.standard_b64encode(b"\000user\000bad")
self.client.write(PLAIN_AUTH.format(response.decode("us-ascii"))
.encode("utf-8"))
with self.assertRaises(SASLAuthenticationFailed):
self.wait()
self.client.write(b"</stream:stream>")
self.client.disconnect()
self.wait()
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [StreamConnectedEvent,
DisconnectedEvent])