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


Python Controller.get_accounting_stats方法代码示例

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


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

示例1: TestControl

# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_accounting_stats [as 别名]

#.........这里部分代码省略.........
    @patch("stem.control.Controller.get_info")
    def test_get_socks_listeners_new(self, get_info_mock):
        """
    Exercises the get_socks_listeners() method as if talking to a newer tor
    instance.
    """

        # multiple SOCKS listeners

        get_info_mock.return_value = '"127.0.0.1:1112" "127.0.0.1:1114"'

        self.assertEqual([("127.0.0.1", 1112), ("127.0.0.1", 1114)], self.controller.get_socks_listeners())

        # no SOCKS listeners

        get_info_mock.return_value = ""
        self.assertEqual([], self.controller.get_socks_listeners())

        # check where GETINFO provides malformed content

        invalid_responses = (
            '"127.0.0.1"',  # address only
            '"1112"',  # port only
            '"5127.0.0.1:1112"',  # invlaid address
            '"127.0.0.1:991112"',  # invalid port
        )

        for response in invalid_responses:
            get_info_mock.return_value = response
            self.assertRaises(stem.ProtocolError, self.controller.get_socks_listeners)

    @patch("stem.control.Controller.get_info")
    @patch("time.time", Mock(return_value=1410723598.276578))
    def test_get_accounting_stats(self, get_info_mock):
        """
    Exercises the get_accounting_stats() method.
    """

        get_info_mock.side_effect = lambda param, **kwargs: {
            "accounting/enabled": "1",
            "accounting/hibernating": "awake",
            "accounting/interval-end": "2014-09-14 19:41:00",
            "accounting/bytes": "4837 2050",
            "accounting/bytes-left": "102944 7440",
        }[param]

        expected = stem.control.AccountingStats(
            1410723598.276578,
            "awake",
            datetime.datetime(2014, 9, 14, 19, 41),
            62,
            4837,
            102944,
            107781,
            2050,
            7440,
            9490,
        )

        self.assertEqual(expected, self.controller.get_accounting_stats())

        get_info_mock.side_effect = ControllerError("nope, too bad")
        self.assertRaises(ControllerError, self.controller.get_accounting_stats)
        self.assertEqual("my default", self.controller.get_accounting_stats("my default"))

    @patch("stem.connection.get_protocolinfo")
开发者ID:Fuzew,项目名称:sharp-stem,代码行数:70,代码来源:controller.py

示例2: TestControl

# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_accounting_stats [as 别名]

#.........这里部分代码省略.........
    Exercises the get_socks_listeners() method as if talking to a newer tor
    instance.
    """

    # multiple SOCKS listeners

    get_info_mock.return_value = '"127.0.0.1:1112" "127.0.0.1:1114"'

    self.assertEqual(
      [('127.0.0.1', 1112), ('127.0.0.1', 1114)],
      self.controller.get_socks_listeners()
    )

    # no SOCKS listeners

    get_info_mock.return_value = ''
    self.assertEqual([], self.controller.get_socks_listeners())

    # check where GETINFO provides malformed content

    invalid_responses = (
      '"127.0.0.1"',         # address only
      '"1112"',              # port only
      '"5127.0.0.1:1112"',   # invlaid address
      '"127.0.0.1:991112"',  # invalid port
    )

    for response in invalid_responses:
      get_info_mock.return_value = response
      self.assertRaises(stem.ProtocolError, self.controller.get_socks_listeners)

  @patch('stem.control.Controller.get_info')
  @patch('time.time', Mock(return_value = 1410723598.276578))
  def test_get_accounting_stats(self, get_info_mock):
    """
    Exercises the get_accounting_stats() method.
    """

    get_info_mock.side_effect = lambda param, **kwargs: {
      'accounting/enabled': '1',
      'accounting/hibernating': 'awake',
      'accounting/interval-end': '2014-09-14 19:41:00',
      'accounting/bytes': '4837 2050',
      'accounting/bytes-left': '102944 7440',
    }[param]

    expected = stem.control.AccountingStats(
      1410723598.276578,
      'awake',
      datetime.datetime(2014, 9, 14, 19, 41),
      62,
      4837, 102944, 107781,
      2050, 7440, 9490,
    )

    self.assertEqual(expected, self.controller.get_accounting_stats())

    get_info_mock.side_effect = ControllerError('nope, too bad')
    self.assertRaises(ControllerError, self.controller.get_accounting_stats)
    self.assertEqual('my default', self.controller.get_accounting_stats('my default'))

  @patch('stem.connection.get_protocolinfo')
  def test_get_protocolinfo(self, get_protocolinfo_mock):
    """
    Exercises the get_protocolinfo() method.
    """
开发者ID:patrickod,项目名称:stem,代码行数:70,代码来源:controller.py


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