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


Python SAPRoutedStreamSocket.sr方法代码示例

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


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

示例1: PySAPRoutedStreamSocketTest

# 需要导入模块: from pysap.SAPRouter import SAPRoutedStreamSocket [as 别名]
# 或者: from pysap.SAPRouter.SAPRoutedStreamSocket import sr [as 别名]
class PySAPRoutedStreamSocketTest(unittest.TestCase):

    test_port = 8005
    test_address = "127.0.0.1"
    test_string = "TEST" * 10

    def start_server(self, handler_cls):
        self.server = SAPNIServerThreaded((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_saproutedstreamsocket(self):
        """Test SAPRoutedStreamSocket"""
        self.start_server(SAPRouterServerTestHandler)

        sock = socket.socket()
        sock.connect((self.test_address, self.test_port))

        route = [SAPRouterRouteHop(hostname=self.test_address,
                                   port=self.test_port),
                 SAPRouterRouteHop(hostname="10.0.0.1",
                                   port="3200")]

        self.client = SAPRoutedStreamSocket(sock, route=route,
                                            router_version=40)
        packet = self.client.sr(self.test_string)

        self.assertIn(SAPNI, packet)
        self.assertEqual(packet[SAPNI].length, len(self.test_string) + 4)
        self.assertEqual(unpack("!I", packet[SAPNI].payload.load[:4]), (len(self.test_string), ))
        self.assertEqual(packet[SAPNI].payload.load[4:], self.test_string)

        self.client.close()
        self.stop_server()

    def test_saproutedstreamsocket_route_error(self):
        """Test SAPRoutedStreamSocket throwing of SAPRouteException if
        a route denied return error is received"""
        self.start_server(SAPRouterServerTestHandler)

        sock = socket.socket()
        sock.connect((self.test_address, self.test_port))

        route = [SAPRouterRouteHop(hostname=self.test_address,
                                   port=self.test_port),
                 SAPRouterRouteHop(hostname="10.0.0.2",
                                   port="3200")]

        with self.assertRaises(SAPRouteException):
            self.client = SAPRoutedStreamSocket(sock, route=route,
                                                router_version=40)

        self.stop_server()

    def test_saproutedstreamsocket_error(self):
        """Test SAPRoutedStreamSocket throwing of Exception if an invalid
        or unexpected packet is received"""
        self.start_server(SAPRouterServerTestHandler)

        sock = socket.socket()
        sock.connect((self.test_address, self.test_port))

        with self.assertRaises(Exception):
            self.client = SAPRoutedStreamSocket(sock, route=None,
                                                router_version=40)

        self.stop_server()

    def test_saproutedstreamsocket_getnisocket(self):
        """Test SAPRoutedStreamSocket get nisocket class method"""
        self.start_server(SAPRouterServerTestHandler)

        # Test using a complete route
        route = [SAPRouterRouteHop(hostname=self.test_address,
                                   port=self.test_port),
                 SAPRouterRouteHop(hostname="10.0.0.1",
                                   port="3200")]
        self.client = SAPRoutedStreamSocket.get_nisocket(route=route,
                                                         router_version=40)

        packet = self.client.sr(self.test_string)
        self.assertIn(SAPNI, packet)
        self.assertEqual(packet[SAPNI].length, len(self.test_string) + 4)
        self.assertEqual(unpack("!I", packet[SAPNI].payload.load[:4]), (len(self.test_string), ))
        self.assertEqual(packet[SAPNI].payload.load[4:], self.test_string)

        # Test using a route and a target host/port
        route = [SAPRouterRouteHop(hostname=self.test_address,
                                   port=self.test_port)]
        self.client = SAPRoutedStreamSocket.get_nisocket("10.0.0.1",
#.........这里部分代码省略.........
开发者ID:,项目名称:,代码行数:103,代码来源:


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