本文整理匯總了Python中pyxmpp2.streambase.StreamBase.initiate方法的典型用法代碼示例。如果您正苦於以下問題:Python StreamBase.initiate方法的具體用法?Python StreamBase.initiate怎麽用?Python StreamBase.initiate使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pyxmpp2.streambase.StreamBase
的用法示例。
在下文中一共展示了StreamBase.initiate方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: TestBindingInitiator
# 需要導入模塊: from pyxmpp2.streambase import StreamBase [as 別名]
# 或者: from pyxmpp2.streambase.StreamBase import initiate [as 別名]
class TestBindingInitiator(InitiatorSelectTestCase):
def test_bind_no_resource(self):
handler = AuthorizedEventHandler()
handlers = [ResourceBindingHandler(), handler]
processor = StanzaProcessor()
processor.setup_stanza_handlers(handlers, "post-auth")
self.stream = StreamBase(u"jabber:client", processor, handlers)
processor.uplink = self.stream
self.stream.me = JID("[email protected]")
self.start_transport([handler])
self.stream.initiate(self.transport)
self.connect_transport()
self.server.write(C2S_SERVER_STREAM_HEAD)
self.wait_short(1)
self.server.write(BIND_FEATURES)
req_id = self.wait(1,
expect = re.compile(br".*<iq[^>]*id=[\"']([^\"']*)[\"']"))
self.assertIsNotNone(req_id)
req_id = req_id.decode("utf-8")
self.server.write(BIND_GENERATED_RESPONSE.format(req_id)
.encode("utf-8"))
self.wait()
self.assertFalse(self.stream.is_connected())
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [ConnectingEvent,
ConnectedEvent, StreamConnectedEvent, GotFeaturesEvent,
BindingResourceEvent, AuthorizedEvent, DisconnectedEvent])
def test_bind(self):
handler = AuthorizedEventHandler()
handlers = [ResourceBindingHandler(), handler]
processor = StanzaProcessor()
processor.setup_stanza_handlers(handlers, "post-auth")
self.stream = StreamBase(u"jabber:client", processor, handlers,
XMPPSettings({"resource": "Provided"}))
processor.uplink = self.stream
self.stream.me = JID("[email protected]")
self.start_transport([handler])
self.stream.initiate(self.transport)
self.connect_transport()
self.server.write(C2S_SERVER_STREAM_HEAD)
self.wait_short(1)
self.server.write(BIND_FEATURES)
req_id = self.wait(1,
expect = re.compile(br".*<iq[^>]*id=[\"']([^\"']*)[\"'].*"
br"<resource>Provided</resource>"))
self.assertIsNotNone(req_id)
req_id = req_id.decode("utf-8")
self.server.write(BIND_PROVIDED_RESPONSE.format(req_id).encode("utf-8"))
self.wait()
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [ConnectingEvent,
ConnectedEvent, StreamConnectedEvent, GotFeaturesEvent,
BindingResourceEvent, AuthorizedEvent, DisconnectedEvent])
示例2: TestInitiator
# 需要導入模塊: from pyxmpp2.streambase import StreamBase [as 別名]
# 或者: from pyxmpp2.streambase.StreamBase import initiate [as 別名]
class TestInitiator(InitiatorSelectTestCase):
def test_enabled_optional(self):
"""Test TLS enabled in settings, and optional on the server."""
settings = XMPPSettings({
"starttls": True,
"tls_cacert_file": os.path.join(DATA_DIR, "ca.pem"),
})
handler = EventRecorder()
handlers = [StreamTLSHandler(settings), handler]
self.stream = StreamBase("jabber:client", None, handlers, settings)
self.start_transport(handlers)
self.stream.initiate(self.transport, to = "server.example.org")
self.connect_transport()
self.server.write(C2S_SERVER_STREAM_HEAD)
self.server.write(TLS_FEATURES)
xml = self.wait(2, expect = re.compile(br".*(<starttls.*/>)"))
self.assertIsNotNone(xml)
element = XML(xml)
self.assertEqual(element.tag,
"{urn:ietf:params:xml:ns:xmpp-tls}starttls")
self.server.write(PROCEED)
self.server.starttls(self.server.sock,
keyfile = os.path.join(DATA_DIR, "server-key.pem"),
certfile = os.path.join(DATA_DIR, "server.pem"),
server_side = True,
ca_certs = os.path.join(DATA_DIR, "ca.pem"),
)
stream_start = self.wait(expect = re.compile(
br"(<stream:stream[^>]*>)"))
self.assertIsNotNone(stream_start)
self.assertTrue(self.stream.tls_established)
self.stream.disconnect()
self.server.write(C2S_SERVER_STREAM_HEAD)
self.server.write(EMPTY_FEATURES)
self.server.write(b"</stream:stream>")
self.wait()
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [ConnectingEvent,
ConnectedEvent, StreamConnectedEvent, GotFeaturesEvent,
TLSConnectingEvent, TLSConnectedEvent, StreamRestartedEvent,
GotFeaturesEvent, DisconnectedEvent])
def test_enabled_required(self):
"""Test TLS enabled in settings, and required on the server."""
self.start_server()
settings = XMPPSettings({
"starttls": True,
"tls_cacert_file": os.path.join(DATA_DIR, "ca.pem"),
})
handler = EventRecorder()
handlers = [StreamTLSHandler(settings), handler]
self.stream = StreamBase("jabber:client", None, handlers, settings)
self.start_transport(handlers)
self.stream.initiate(self.transport, to = "server.example.org")
self.connect_transport()
self.server.write(C2S_SERVER_STREAM_HEAD)
self.server.write(TLS_REQUIRED_FEATURES)
xml = self.wait(expect = re.compile(br".*(<starttls.*/>)"))
self.assertIsNotNone(xml)
element = XML(xml)
self.assertEqual(element.tag,
"{urn:ietf:params:xml:ns:xmpp-tls}starttls")
self.server.write(PROCEED)
self.server.starttls(self.server.sock,
keyfile = os.path.join(DATA_DIR, "server-key.pem"),
certfile = os.path.join(DATA_DIR, "server.pem"),
server_side = True,
ca_certs = os.path.join(DATA_DIR, "ca.pem"),
)
stream_start = self.wait(expect = re.compile(
br"(<stream:stream[^>]*>)"))
self.assertIsNotNone(stream_start)
self.assertTrue(self.stream.tls_established)
self.stream.disconnect()
self.server.write(C2S_SERVER_STREAM_HEAD)
self.server.write(EMPTY_FEATURES)
self.server.write(b"</stream:stream>")
self.wait()
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [ConnectingEvent,
ConnectedEvent, StreamConnectedEvent, GotFeaturesEvent,
TLSConnectingEvent, TLSConnectedEvent, StreamRestartedEvent,
GotFeaturesEvent, DisconnectedEvent])
def test_enabled_missing(self):
"""Test TLS enabled in settings, and missing on the server."""
self.start_server()
settings = XMPPSettings({
"starttls": True,
"tls_cacert_file": os.path.join(DATA_DIR, "ca.pem"),
})
handler = EventRecorder()
handlers = [StreamTLSHandler(settings), handler]
self.stream = StreamBase("jabber:client", None, handlers, settings)
self.start_transport(handlers)
self.stream.initiate(self.transport, to = "server.example.org")
self.connect_transport()
self.server.write(C2S_SERVER_STREAM_HEAD)
self.server.write(EMPTY_FEATURES)
self.server.write(b"</stream:stream>")
#.........這裏部分代碼省略.........
示例3: TestInitiatorSelect
# 需要導入模塊: from pyxmpp2.streambase import StreamBase [as 別名]
# 或者: from pyxmpp2.streambase.StreamBase import initiate [as 別名]
class TestInitiatorSelect(InitiatorSelectTestCase):
def test_connect_close(self):
handler = JustConnectEventHandler()
self.stream = StreamBase("jabber:client", None, [])
self.start_transport([handler])
self.stream.initiate(self.transport)
self.connect_transport()
self.wait()
self.assertFalse(self.stream.is_connected())
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [ConnectingEvent,
ConnectedEvent, DisconnectedEvent])
def test_stream_connect_disconnect(self):
handler = JustStreamConnectEventHandler()
self.stream = StreamBase("jabber:client", None, [])
self.start_transport([handler])
self.stream.initiate(self.transport)
self.connect_transport()
self.wait_short(0.25)
self.wait_short(0.25)
self.assertTrue(self.stream.is_connected())
self.server.write(C2S_SERVER_STREAM_HEAD)
self.wait(expect = re.compile(b".*(</stream:stream>)"))
self.server.write(STREAM_TAIL)
self.server.disconnect()
self.wait()
self.assertFalse(self.stream.is_connected())
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [ConnectingEvent, ConnectedEvent,
StreamConnectedEvent, DisconnectedEvent])
def test_parse_error(self):
handler = IgnoreEventHandler()
self.stream = StreamBase("jabber:client", None, [])
self.start_transport([handler])
self.stream.initiate(self.transport)
self.connect_transport()
self.server.write(C2S_SERVER_STREAM_HEAD)
self.wait_short()
self.server.write(b"</stream:test>")
with self.assertRaises(StreamParseError):
logger.debug("-- WAIT start")
self.wait()
logger.debug("-- WAIT end")
self.assertFalse(self.stream.is_connected())
self.wait_short()
self.server.wait(1)
self.assertTrue(self.server.eof)
self.assertTrue(self.server.rdata.endswith(PARSE_ERROR_RESPONSE))
self.server.disconnect()
self.wait()
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [ConnectingEvent, ConnectedEvent,
StreamConnectedEvent, DisconnectedEvent])
def test_stanza_send(self):
handler = IgnoreEventHandler()
route = RecordingRoute()
self.stream = StreamBase("jabber:client", route, [])
self.start_transport([handler])
self.stream.initiate(self.transport)
self.connect_transport()
self.server.write(C2S_SERVER_STREAM_HEAD)
self.wait_short(0.25)
self.wait_short(0.25)
self.assertTrue(self.stream.is_connected())
self.stream.send(Message(to_jid = JID("[email protected]"),
body = "Test"))
xml = self.wait(expect = re.compile(b".*(<message.*</message>)"))
self.assertIsNotNone(xml)
if b"xmlns" not in xml:
xml = xml.replace(b"<message", b"<message xmlns='jabber:client'")
element = XML(xml)
stanza = Message(element)
self.assertEqual(stanza.body, "Test")
self.stream.disconnect()
self.server.write(STREAM_TAIL)
self.server.disconnect()
self.wait()
self.assertEqual(route.sent, [])
self.assertEqual(route.received, [])
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [ConnectingEvent, ConnectedEvent,
StreamConnectedEvent, DisconnectedEvent])
def test_stanza_receive(self):
handler = IgnoreEventHandler()
route = RecordingRoute()
self.stream = StreamBase("jabber:client", route, [])
self.start_transport([handler])
self.stream.initiate(self.transport)
self.connect_transport()
logger.debug("-- waiting for connect")
self.wait_short(0.25)
self.wait_short(0.25)
logger.debug("-- checking connected")
self.assertTrue(self.stream.is_connected())
self.server.write(C2S_SERVER_STREAM_HEAD)
self.server.write(b"<message><body>Test</body></message>")
#.........這裏部分代碼省略.........
示例4: TestInitiator
# 需要導入模塊: from pyxmpp2.streambase import StreamBase [as 別名]
# 或者: from pyxmpp2.streambase.StreamBase import initiate [as 別名]
class TestInitiator(InitiatorSelectTestCase):
def test_auth(self):
handler = EventRecorder()
settings = XMPPSettings({
"username": "user",
"password": "secret",
})
self.stream = StreamBase("jabber:client", None,
[StreamSASLHandler(settings), handler], settings)
self.start_transport([handler])
self.stream.initiate(self.transport)
self.connect_transport()
self.server.write(C2S_SERVER_STREAM_HEAD)
self.server.write(AUTH_FEATURES)
xml = self.wait(expect = re.compile(br".*(<auth.*</auth>)"))
self.assertIsNotNone(xml)
element = ElementTree.XML(xml)
self.assertEqual(element.tag, "{urn:ietf:params:xml:ns:xmpp-sasl}auth")
mech = element.get("mechanism")
self.assertEqual(mech, "PLAIN")
data = binascii.a2b_base64(element.text.encode("utf-8"))
self.assertEqual(data, b"\000user\000secret")
self.server.rdata = b""
self.server.write(
b"<success xmlns='urn:ietf:params:xml:ns:xmpp-sasl'/>")
stream_start = self.wait(expect = re.compile(
br"(<stream:stream[^>]*>)"))
self.assertIsNotNone(stream_start)
self.assertTrue(self.stream.authenticated)
self.server.write(C2S_SERVER_STREAM_HEAD)
self.server.write(BIND_FEATURES)
self.server.write(b"</stream:stream>")
self.server.disconnect()
self.wait()
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [ConnectingEvent, ConnectedEvent,
StreamConnectedEvent, GotFeaturesEvent,
AuthenticatedEvent, StreamRestartedEvent, GotFeaturesEvent,
DisconnectedEvent])
def test_auth_fail(self):
handler = EventRecorder()
settings = XMPPSettings({
"username": "user",
"password": "bad",
})
self.stream = StreamBase("jabber:client", None,
[StreamSASLHandler(settings), handler], settings)
self.start_transport([handler])
self.stream.initiate(self.transport)
self.connect_transport()
self.server.write(C2S_SERVER_STREAM_HEAD)
self.server.write(AUTH_FEATURES)
xml = self.wait(expect = re.compile(br".*(<auth.*</auth>)"))
self.assertIsNotNone(xml)
element = ElementTree.XML(xml)
self.assertEqual(element.tag, "{urn:ietf:params:xml:ns:xmpp-sasl}auth")
mech = element.get("mechanism")
self.assertEqual(mech, "PLAIN")
data = binascii.a2b_base64(element.text.encode("utf-8"))
self.assertNotEqual(data, b"\000user\000secret")
self.server.write(b"""<failure xmlns='urn:ietf:params:xml:ns:xmpp-sasl'>
<not-authorized/></failure>""")
with self.assertRaises(SASLAuthenticationFailed):
self.wait()
self.assertFalse(self.stream.authenticated)
self.server.disconnect()
self.wait()
event_classes = [e.__class__ for e in handler.events_received]
self.assertEqual(event_classes, [ConnectingEvent, ConnectedEvent,
StreamConnectedEvent, GotFeaturesEvent, DisconnectedEvent])