當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。