本文整理汇总了Python中stem.control.Controller.get_streams方法的典型用法代码示例。如果您正苦于以下问题:Python Controller.get_streams方法的具体用法?Python Controller.get_streams怎么用?Python Controller.get_streams使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stem.control.Controller
的用法示例。
在下文中一共展示了Controller.get_streams方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestControl
# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_streams [as 别名]
#.........这里部分代码省略.........
self.assertEqual(
"default returned",
self.controller.get_network_status(nickname, default = "default returned")
)
# no default value, accept the error
self.assertRaises(InvalidArguments, self.controller.get_network_status, nickname)
@patch('stem.control.Controller.is_authenticated', Mock(return_value = True))
@patch('stem.control.Controller._attach_listeners', Mock(return_value = ([], [])))
@patch('stem.control.Controller.get_version')
def test_event_listening(self, get_version_mock):
"""
Exercises the add_event_listener and remove_event_listener methods.
"""
# set up for failure to create any events
get_version_mock.return_value = stem.version.Version('0.1.0.14')
self.assertRaises(InvalidRequest, self.controller.add_event_listener, Mock(), EventType.BW)
# set up to only fail newer events
get_version_mock.return_value = stem.version.Version('0.2.0.35')
# EventType.BW is one of the earliest events
self.controller.add_event_listener(Mock(), EventType.BW)
# EventType.SIGNAL was added in tor version 0.2.3.1-alpha
self.assertRaises(InvalidRequest, self.controller.add_event_listener, Mock(), EventType.SIGNAL)
def test_get_streams(self):
"""
Exercises the get_streams() method.
"""
# get a list of fake, but good looking, streams
valid_streams = (
("1", "NEW", "4", "10.10.10.1:80"),
("2", "SUCCEEDED", "4", "10.10.10.1:80"),
("3", "SUCCEEDED", "4", "10.10.10.1:80")
)
response = "".join(["%s\r\n" % " ".join(entry) for entry in valid_streams])
with patch('stem.control.Controller.get_info', Mock(return_value = response)):
streams = self.controller.get_streams()
self.assertEqual(len(valid_streams), len(streams))
for index, stream in enumerate(streams):
self.assertEqual(valid_streams[index][0], stream.id)
self.assertEqual(valid_streams[index][1], stream.status)
self.assertEqual(valid_streams[index][2], stream.circ_id)
self.assertEqual(valid_streams[index][3], stream.target)
def test_attach_stream(self):
"""
Exercises the attach_stream() method.
"""
# Response when the stream is in a state where it can't be attached (for
# instance, it's already open).
response = stem.response.ControlMessage.from_str("555 Connection is not managed by controller.\r\n")
示例2: TestControl
# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_streams [as 别名]
#.........这里部分代码省略.........
# 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")
)
# No default value, accept the error.
self.assertRaises(InvalidArguments, self.controller.get_network_status, nickname)
def test_event_listening(self):
"""
Exercises the add_event_listener and remove_event_listener methods.
"""
# set up for failure to create any events
mocking.mock_method(Controller, "is_authenticated", mocking.return_true())
mocking.mock_method(Controller, "_attach_listeners", mocking.return_value(([], [])))
mocking.mock_method(Controller, "get_version", mocking.return_value(stem.version.Version('0.1.0.14')))
self.assertRaises(InvalidRequest, self.controller.add_event_listener, mocking.no_op(), EventType.BW)
# set up to only fail newer events
mocking.mock_method(Controller, "get_version", mocking.return_value(stem.version.Version('0.2.0.35')))
# EventType.BW is one of the earliest events
self.controller.add_event_listener(mocking.no_op(), EventType.BW)
# EventType.SIGNAL was added in tor version 0.2.3.1-alpha
self.assertRaises(InvalidRequest, self.controller.add_event_listener, mocking.no_op(), EventType.SIGNAL)
def test_get_streams(self):
"""
Exercises the get_streams() method.
"""
# get a list of fake, but good looking, streams
valid_streams = (
("1", "NEW", "4", "10.10.10.1:80"),
("2", "SUCCEEDED", "4", "10.10.10.1:80"),
("3", "SUCCEEDED", "4", "10.10.10.1:80")
)
responses = ["%s\r\n" % " ".join(entry) for entry in valid_streams]
mocking.mock_method(Controller, "get_info", mocking.return_value(
"".join(responses)
))
streams = self.controller.get_streams()
self.assertEqual(len(valid_streams), len(streams))
for index, stream in enumerate(streams):
self.assertEqual(valid_streams[index][0], stream.id)
self.assertEqual(valid_streams[index][1], stream.status)
self.assertEqual(valid_streams[index][2], stream.circ_id)
self.assertEqual(valid_streams[index][3], stream.target)
def test_parse_circ_path(self):
"""
Exercises the _parse_circ_path() helper function.
"""
# empty input
self.assertEqual([], _parse_circ_path(None))
self.assertEqual([], _parse_circ_path(''))
# check the pydoc examples
pydoc_examples = {
'$999A226EBED397F331B612FE1E4CFAE5C1F201BA=piyaz':
[('999A226EBED397F331B612FE1E4CFAE5C1F201BA', 'piyaz')],
'$E57A476CD4DFBD99B4EE52A100A58610AD6E80B9,hamburgerphone,PrivacyRepublic14':
[
('E57A476CD4DFBD99B4EE52A100A58610AD6E80B9', None),
(None, 'hamburgerphone'),
(None, 'PrivacyRepublic14'),
],
}
for test_input, expected in pydoc_examples.items():
self.assertEqual(expected, _parse_circ_path(test_input))
# exercise with some invalid inputs
malformed_inputs = [
'=piyaz', # no fingerprint
'999A226EBED397F331B612FE1E4CFAE5C1F201BA=piyaz', # fingerprint missing prefix
'$999A226EBED397F331B612FE1E4CFAE5C1F201BAA=piyaz', # fingerprint too long
'$999A226EBED397F331B612FE1E4CFAE5C1F201B=piyaz', # fingerprint too short
'$999A226EBED397F331B612FE1E4CFAE5C1F201Bz=piyaz', # invalid character in fingerprint
'$999A226EBED397F331B612FE1E4CFAE5C1F201BA=', # no nickname
]
for test_input in malformed_inputs:
self.assertRaises(ProtocolError, _parse_circ_path, test_input)
示例3: TestControl
# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_streams [as 别名]
#.........这里部分代码省略.........
# Again, an old tor, but SocksListenAddress overrides the port number.
mocking.mock_method(Controller, "get_conf", mocking.return_for_args({
("SocksPort",): "9050",
("SocksListenAddress", "multiple=True"): ["127.0.0.1:1112"]
}, is_method = True))
self.assertEqual([('127.0.0.1', 1112)], self.controller.get_socks_listeners())
# Again, an old tor, but multiple listeners
mocking.mock_method(Controller, "get_conf", mocking.return_for_args({
("SocksPort",): "9050",
("SocksListenAddress", "multiple=True"): ["127.0.0.1:1112", "127.0.0.1:1114"]
}, is_method = True))
self.assertEqual([('127.0.0.1', 1112), ('127.0.0.1', 1114)], self.controller.get_socks_listeners())
# Again, an old tor, but no SOCKS listeners
mocking.mock_method(Controller, "get_conf", mocking.return_for_args({
("SocksPort",): "0",
("SocksListenAddress", "multiple=True"): []
}, is_method = True))
self.assertEqual([], self.controller.get_socks_listeners())
# Where tor provides invalid ports or addresses
mocking.mock_method(Controller, "get_conf", mocking.return_for_args({
("SocksPort",): "blarg",
("SocksListenAddress", "multiple=True"): ["127.0.0.1"]
}, is_method = True))
self.assertRaises(stem.ProtocolError, self.controller.get_socks_listeners)
mocking.mock_method(Controller, "get_conf", mocking.return_for_args({
("SocksPort",): "0",
("SocksListenAddress", "multiple=True"): ["127.0.0.1:abc"]
}, is_method = True))
self.assertRaises(stem.ProtocolError, self.controller.get_socks_listeners)
mocking.mock_method(Controller, "get_conf", mocking.return_for_args({
("SocksPort",): "40",
("SocksListenAddress", "multiple=True"): ["500.0.0.1"]
}, is_method = True))
self.assertRaises(stem.ProtocolError, self.controller.get_socks_listeners)
def test_get_socks_listeners_new(self):
"""
Exercises the get_socks_listeners() method as if talking to a newer tor
instance.
"""
# multiple SOCKS listeners
mocking.mock_method(Controller, "get_info", mocking.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
mocking.mock_method(Controller, "get_info", mocking.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:
mocking.mock_method(Controller, "get_info", mocking.return_value(response))
self.assertRaises(stem.ProtocolError, self.controller.get_socks_listeners)
def test_get_streams(self):
"""
Exercises the get_streams() method.
"""
# get a list of fake, but good looking, streams
valid_streams = (
("1", "NEW", "4", "10.10.10.1:80"),
("2", "SUCCEEDED", "4", "10.10.10.1:80"),
("3", "SUCCEEDED", "4", "10.10.10.1:80")
)
responses = ["%s\r\n" % " ".join(entry) for entry in valid_streams]
mocking.mock_method(Controller, "get_info", mocking.return_value(
"".join(responses)
))
streams = self.controller.get_streams()
self.assertEqual(len(valid_streams), len(streams))
for index, stream in enumerate(streams):
self.assertEqual(valid_streams[index][0], stream.id)
self.assertEqual(valid_streams[index][1], stream.status)
self.assertEqual(valid_streams[index][2], stream.circ_id)
self.assertEqual(valid_streams[index][3], stream.target)
示例4: TestControl
# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_streams [as 别名]
#.........这里部分代码省略.........
# get a default value when the call fails
self.assertEqual("default returned", self.controller.get_network_status(nickname, default="default returned"))
# no default value, accept the error
self.assertRaises(InvalidArguments, self.controller.get_network_status, nickname)
@patch("stem.control.Controller.is_authenticated", Mock(return_value=True))
@patch("stem.control.Controller._attach_listeners", Mock(return_value=([], [])))
@patch("stem.control.Controller.get_version")
def test_event_listening(self, get_version_mock):
"""
Exercises the add_event_listener and remove_event_listener methods.
"""
# set up for failure to create any events
get_version_mock.return_value = stem.version.Version("0.1.0.14")
self.assertRaises(InvalidRequest, self.controller.add_event_listener, Mock(), EventType.BW)
# set up to only fail newer events
get_version_mock.return_value = stem.version.Version("0.2.0.35")
# EventType.BW is one of the earliest events
self.controller.add_event_listener(Mock(), EventType.BW)
# EventType.SIGNAL was added in tor version 0.2.3.1-alpha
self.assertRaises(InvalidRequest, self.controller.add_event_listener, Mock(), EventType.SIGNAL)
def test_get_streams(self):
"""
Exercises the get_streams() method.
"""
# get a list of fake, but good looking, streams
valid_streams = (
("1", "NEW", "4", "10.10.10.1:80"),
("2", "SUCCEEDED", "4", "10.10.10.1:80"),
("3", "SUCCEEDED", "4", "10.10.10.1:80"),
)
response = "".join(["%s\r\n" % " ".join(entry) for entry in valid_streams])
with patch("stem.control.Controller.get_info", Mock(return_value=response)):
streams = self.controller.get_streams()
self.assertEqual(len(valid_streams), len(streams))
for index, stream in enumerate(streams):
self.assertEqual(valid_streams[index][0], stream.id)
self.assertEqual(valid_streams[index][1], stream.status)
self.assertEqual(valid_streams[index][2], stream.circ_id)
self.assertEqual(valid_streams[index][3], stream.target)
def test_attach_stream(self):
"""
Exercises the attach_stream() method.
"""
# Response when the stream is in a state where it can't be attached (for
# instance, it's already open).
response = stem.response.ControlMessage.from_str("555 Connection is not managed by controller.\r\n")
示例5: TestControl
# 需要导入模块: from stem.control import Controller [as 别名]
# 或者: from stem.control.Controller import get_streams [as 别名]
#.........这里部分代码省略.........
self.assertEqual(
'default returned',
self.controller.get_network_status(nickname, default = 'default returned')
)
# no default value, accept the error
self.assertRaises(InvalidArguments, self.controller.get_network_status, nickname)
@patch('stem.control.Controller.is_authenticated', Mock(return_value = True))
@patch('stem.control.Controller._attach_listeners', Mock(return_value = ([], [])))
@patch('stem.control.Controller.get_version')
def test_event_listening(self, get_version_mock):
"""
Exercises the add_event_listener and remove_event_listener methods.
"""
# set up for failure to create any events
get_version_mock.return_value = stem.version.Version('0.1.0.14')
self.assertRaises(InvalidRequest, self.controller.add_event_listener, Mock(), EventType.BW)
# set up to only fail newer events
get_version_mock.return_value = stem.version.Version('0.2.0.35')
# EventType.BW is one of the earliest events
self.controller.add_event_listener(Mock(), EventType.BW)
# EventType.SIGNAL was added in tor version 0.2.3.1-alpha
self.assertRaises(InvalidRequest, self.controller.add_event_listener, Mock(), EventType.SIGNAL)
def test_get_streams(self):
"""
Exercises the get_streams() method.
"""
# get a list of fake, but good looking, streams
valid_streams = (
('1', 'NEW', '4', '10.10.10.1:80'),
('2', 'SUCCEEDED', '4', '10.10.10.1:80'),
('3', 'SUCCEEDED', '4', '10.10.10.1:80')
)
response = ''.join(['%s\r\n' % ' '.join(entry) for entry in valid_streams])
with patch('stem.control.Controller.get_info', Mock(return_value = response)):
streams = self.controller.get_streams()
self.assertEqual(len(valid_streams), len(streams))
for index, stream in enumerate(streams):
self.assertEqual(valid_streams[index][0], stream.id)
self.assertEqual(valid_streams[index][1], stream.status)
self.assertEqual(valid_streams[index][2], stream.circ_id)
self.assertEqual(valid_streams[index][3], stream.target)
def test_attach_stream(self):
"""
Exercises the attach_stream() method.
"""
# Response when the stream is in a state where it can't be attached (for
# instance, it's already open).
response = stem.response.ControlMessage.from_str('555 Connection is not managed by controller.\r\n')