本文整理汇总了Python中stem.control.Controller.get_network_status方法的典型用法代码示例。如果您正苦于以下问题:Python Controller.get_network_status方法的具体用法?Python Controller.get_network_status怎么用?Python Controller.get_network_status使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stem.control.Controller
的用法示例。
在下文中一共展示了Controller.get_network_status方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestControl
# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_network_status [as 别名]
#.........这里部分代码省略.........
@patch('stem.socket.ControlSocket.is_localhost', Mock(return_value = True))
@patch('stem.control.Controller.get_info', Mock(return_value = '321'))
def test_get_pid_by_getinfo(self):
"""
Exercise the get_pid() resolution via its getinfo option.
"""
self.assertEqual(321, self.controller.get_pid())
@patch('stem.socket.ControlSocket.is_localhost', Mock(return_value = True))
@patch('stem.control.Controller.get_conf')
@patch('stem.control.open', create = True)
def test_get_pid_by_pid_file(self, open_mock, get_conf_mock):
"""
Exercise the get_pid() resolution via a PidFile.
"""
get_conf_mock.return_value = '/tmp/pid_file'
open_mock.return_value = io.BytesIO(b'432')
self.assertEqual(432, self.controller.get_pid())
open_mock.assert_called_once_with('/tmp/pid_file')
@patch('stem.socket.ControlSocket.is_localhost', Mock(return_value = True))
@patch('stem.util.system.get_pid_by_name', Mock(return_value = 432))
def test_get_pid_by_name(self):
"""
Exercise the get_pid() resolution via the process name.
"""
self.assertEqual(432, self.controller.get_pid())
@patch('stem.control.Controller.get_info')
def test_get_network_status(self, get_info_mock):
"""
Exercises the get_network_status() method.
"""
# build a single router status entry
nickname = "Beaver"
fingerprint = "/96bKo4soysolMgKn5Hex2nyFSY"
desc = "r %s %s u5lTXJKGsLKufRLnSyVqT7TdGYw 2012-12-30 22:02:49 77.223.43.54 9001 0\ns Fast Named Running Stable Valid\nw Bandwidth=75" % (nickname, fingerprint)
router = stem.descriptor.router_status_entry.RouterStatusEntryV3(desc)
# always return the same router status entry
get_info_mock.return_value = desc
# pretend to get the router status entry with its name
self.assertEqual(router, self.controller.get_network_status(nickname))
# pretend to get the router status entry with its fingerprint
hex_fingerprint = stem.descriptor.router_status_entry._base64_to_hex(fingerprint, False)
self.assertEqual(router, self.controller.get_network_status(hex_fingerprint))
# mangle hex fingerprint and try again
hex_fingerprint = hex_fingerprint[2:]
self.assertRaises(ValueError, self.controller.get_network_status, hex_fingerprint)
# raise an exception in the get_info() call
get_info_mock.side_effect = InvalidArguments
示例2: TestControl
# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_network_status [as 别名]
#.........这里部分代码省略.........
@patch("stem.socket.ControlSocket.is_localhost", Mock(return_value=True))
@patch("stem.control.Controller.get_info", Mock(return_value="321"))
def test_get_pid_by_getinfo(self):
"""
Exercise the get_pid() resolution via its getinfo option.
"""
self.assertEqual(321, self.controller.get_pid())
@patch("stem.socket.ControlSocket.is_localhost", Mock(return_value=True))
@patch("stem.control.Controller.get_conf")
@patch("stem.control.open", create=True)
def test_get_pid_by_pid_file(self, open_mock, get_conf_mock):
"""
Exercise the get_pid() resolution via a PidFile.
"""
get_conf_mock.return_value = "/tmp/pid_file"
open_mock.return_value = io.BytesIO(b"432")
self.assertEqual(432, self.controller.get_pid())
open_mock.assert_called_once_with("/tmp/pid_file")
@patch("stem.socket.ControlSocket.is_localhost", Mock(return_value=True))
@patch("stem.util.system.pid_by_name", Mock(return_value=432))
def test_get_pid_by_name(self):
"""
Exercise the get_pid() resolution via the process name.
"""
self.assertEqual(432, self.controller.get_pid())
@patch("stem.control.Controller.get_info")
def test_get_network_status_for_ourselves(self, get_info_mock):
"""
Exercises the get_network_status() method for getting our own relay.
"""
# when there's an issue getting our fingerprint
get_info_mock.side_effect = ControllerError("nope, too bad")
try:
self.controller.get_network_status()
self.fail("We should've raised an exception")
except ControllerError as exc:
self.assertEqual("Unable to determine our own fingerprint: nope, too bad", str(exc))
self.assertEqual("boom", self.controller.get_network_status(default="boom"))
# successful request
desc = NS_DESC % ("moria1", "/96bKo4soysolMgKn5Hex2nyFSY")
get_info_mock.side_effect = lambda param, **kwargs: {
"fingerprint": "9695DFC35FFEB861329B9F1AB04C46397020CE31",
"ns/id/9695DFC35FFEB861329B9F1AB04C46397020CE31": desc,
}[param]
self.assertEqual(
stem.descriptor.router_status_entry.RouterStatusEntryV3(desc), self.controller.get_network_status()
)
@patch("stem.control.Controller.get_info")
def test_get_network_status_when_raising_descriptor_unavailable(self, get_info_mock):
"""
示例3: TestControl
# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_network_status [as 别名]
#.........这里部分代码省略.........
'"127.0.0.1:991112"', # invalid port
)
for response in invalid_responses:
mocking.mock_method(Controller, "get_info", mocking.return_value(response))
self.assertRaises(stem.ProtocolError, self.controller.get_socks_listeners)
def test_get_protocolinfo(self):
"""
Exercises the get_protocolinfo() method.
"""
# Use the handy mocked protocolinfo response.
mocking.mock(stem.connection.get_protocolinfo, mocking.return_value(
mocking.get_protocolinfo_response()
))
# Compare the str representation of these object, because the class
# does not have, nor need, a direct comparison operator.
self.assertEqual(str(mocking.get_protocolinfo_response()), str(self.controller.get_protocolinfo()))
# Raise an exception in the stem.connection.get_protocolinfo() call.
mocking.mock(stem.connection.get_protocolinfo, mocking.raise_exception(ProtocolError))
# Get a default value when the call fails.
self.assertEqual(
"default returned",
self.controller.get_protocolinfo(default = "default returned")
)
# No default value, accept the error.
self.assertRaises(ProtocolError, self.controller.get_protocolinfo)
def test_get_network_status(self):
"""
Exercises the get_network_status() method.
"""
# Build a single router status entry.
nickname = "Beaver"
fingerprint = "/96bKo4soysolMgKn5Hex2nyFSY"
desc = "r %s %s u5lTXJKGsLKufRLnSyVqT7TdGYw 2012-12-30 22:02:49 77.223.43.54 9001 0\ns Fast Named Running Stable Valid\nw Bandwidth=75" % (nickname, fingerprint)
router = stem.descriptor.router_status_entry.RouterStatusEntryV2(desc)
# Always return the same router status entry.
mocking.mock_method(Controller, "get_info", mocking.return_value(desc))
# Pretend to get the router status entry with its name.
self.assertEqual(router, self.controller.get_network_status(nickname))
# Pretend to get the router status entry with its fingerprint.
hex_fingerprint = stem.descriptor.router_status_entry._decode_fingerprint(fingerprint, False)
self.assertEqual(router, self.controller.get_network_status(hex_fingerprint))
# Mangle hex fingerprint and try again.
hex_fingerprint = hex_fingerprint[2:]
self.assertRaises(ValueError, self.controller.get_network_status, hex_fingerprint)
# Raise an exception in the get_info() call.
mocking.mock_method(Controller, "get_info", mocking.raise_exception(InvalidArguments))
# Get a default value when the call fails.
self.assertEqual(
"default returned",
self.controller.get_network_status(nickname, default = "default returned")
示例4: TestControl
# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_network_status [as 别名]
#.........这里部分代码省略.........
@patch('stem.socket.ControlSocket.is_localhost', Mock(return_value = True))
@patch('stem.control.Controller.get_info', Mock(return_value = '321'))
def test_get_pid_by_getinfo(self):
"""
Exercise the get_pid() resolution via its getinfo option.
"""
self.assertEqual(321, self.controller.get_pid())
@patch('stem.socket.ControlSocket.is_localhost', Mock(return_value = True))
@patch('stem.control.Controller.get_conf')
@patch('stem.control.open', create = True)
def test_get_pid_by_pid_file(self, open_mock, get_conf_mock):
"""
Exercise the get_pid() resolution via a PidFile.
"""
get_conf_mock.return_value = '/tmp/pid_file'
open_mock.return_value = io.BytesIO(b'432')
self.assertEqual(432, self.controller.get_pid())
open_mock.assert_called_once_with('/tmp/pid_file')
@patch('stem.socket.ControlSocket.is_localhost', Mock(return_value = True))
@patch('stem.util.system.pid_by_name', Mock(return_value = 432))
def test_get_pid_by_name(self):
"""
Exercise the get_pid() resolution via the process name.
"""
self.assertEqual(432, self.controller.get_pid())
@patch('stem.control.Controller.get_info')
def test_get_network_status_for_ourselves(self, get_info_mock):
"""
Exercises the get_network_status() method for getting our own relay.
"""
# when there's an issue getting our fingerprint
get_info_mock.side_effect = ControllerError('nope, too bad')
exc_msg = 'Unable to determine our own fingerprint: nope, too bad'
self.assertRaisesRegexp(ControllerError, exc_msg, self.controller.get_network_status)
self.assertEqual('boom', self.controller.get_network_status(default = 'boom'))
# successful request
desc = NS_DESC % ('moria1', '/96bKo4soysolMgKn5Hex2nyFSY')
get_info_mock.side_effect = lambda param, **kwargs: {
'fingerprint': '9695DFC35FFEB861329B9F1AB04C46397020CE31',
'ns/id/9695DFC35FFEB861329B9F1AB04C46397020CE31': desc,
}[param]
self.assertEqual(stem.descriptor.router_status_entry.RouterStatusEntryV3(desc), self.controller.get_network_status())
@patch('stem.control.Controller.get_info')
def test_get_network_status_when_unavailable(self, get_info_mock):
"""
Exercises the get_network_status() method.
"""
get_info_mock.side_effect = InvalidArguments(None, 'GETINFO request contained unrecognized keywords: ns/id/5AC9C5AA75BA1F18D8459B326B4B8111A856D290')
exc_msg = "Tor was unable to provide the descriptor for '5AC9C5AA75BA1F18D8459B326B4B8111A856D290'"