本文整理汇总了Python中pysap.SAPNI.SAPNIStreamSocket.sr方法的典型用法代码示例。如果您正苦于以下问题:Python SAPNIStreamSocket.sr方法的具体用法?Python SAPNIStreamSocket.sr怎么用?Python SAPNIStreamSocket.sr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pysap.SAPNI.SAPNIStreamSocket
的用法示例。
在下文中一共展示了SAPNIStreamSocket.sr方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PySAPNIStreamSocketTest
# 需要导入模块: from pysap.SAPNI import SAPNIStreamSocket [as 别名]
# 或者: from pysap.SAPNI.SAPNIStreamSocket import sr [as 别名]
class PySAPNIStreamSocketTest(unittest.TestCase):
test_port = 8005
test_address = "127.0.0.1"
test_string = "TEST" * 10
def start_server(self, handler_cls):
self.server = ThreadingTCPServer((self.test_address, self.test_port),
handler_cls,
bind_and_activate=False)
self.server.allow_reuse_address = True
self.server.server_bind()
self.server.server_activate()
self.server_thread = Thread(target=self.server.serve_forever)
self.server_thread.start()
def stop_server(self):
self.server.shutdown()
self.server.server_close()
self.server_thread.join()
def test_sapnistreamsocket(self):
"""Test SAPNIStreamSocket"""
self.start_server(SAPNITestHandler)
sock = socket.socket()
sock.connect((self.test_address, self.test_port))
self.client = SAPNIStreamSocket(sock)
packet = self.client.sr(self.test_string)
packet.decode_payload_as(Raw)
self.client.close()
self.assertIn(SAPNI, packet)
self.assertEqual(packet[SAPNI].length, len(self.test_string))
self.assertEqual(packet.payload.load, self.test_string)
self.stop_server()
def test_sapnistreamsocket_base_cls(self):
"""Test SAPNIStreamSocket handling of custom base packet classes"""
self.start_server(SAPNITestHandler)
class SomeClass(Packet):
fields_desc = [StrField("text", None)]
sock = socket.socket()
sock.connect((self.test_address, self.test_port))
self.client = SAPNIStreamSocket(sock, base_cls=SomeClass)
packet = self.client.sr(self.test_string)
self.client.close()
self.assertIn(SAPNI, packet)
self.assertIn(SomeClass, packet)
self.assertEqual(packet[SAPNI].length, len(self.test_string))
self.assertEqual(packet[SomeClass].text, self.test_string)
self.stop_server()
def test_sapnistreamsocket_getnisocket(self):
"""Test SAPNIStreamSocket get nisocket class method"""
self.start_server(SAPNITestHandler)
self.client = SAPNIStreamSocket.get_nisocket(self.test_address,
self.test_port)
packet = self.client.sr(self.test_string)
packet.decode_payload_as(Raw)
self.client.close()
self.assertIn(SAPNI, packet)
self.assertEqual(packet[SAPNI].length, len(self.test_string))
self.assertEqual(packet.payload.load, self.test_string)
self.stop_server()
def test_sapnistreamsocket_without_keep_alive(self):
"""Test SAPNIStreamSocket without keep alive"""
self.start_server(SAPNITestHandlerKeepAlive)
sock = socket.socket()
sock.connect((self.test_address, self.test_port))
self.client = SAPNIStreamSocket(sock, keep_alive=False)
packet = self.client.sr(self.test_string)
packet.decode_payload_as(Raw)
self.client.close()
# We should receive a PING instead of our packet
self.assertIn(SAPNI, packet)
self.assertEqual(packet[SAPNI].length, len(SAPNI.SAPNI_PING))
self.assertEqual(packet.payload.load, SAPNI.SAPNI_PING)
self.stop_server()
def test_sapnistreamsocket_with_keep_alive(self):
"""Test SAPNIStreamSocket with keep alive"""
self.start_server(SAPNITestHandlerKeepAlive)
#.........这里部分代码省略.........