commit 77481da93b23a54d339133ba379c0a6f3741f799 Author: Sean Robinson seankrobinson@gmail.com Date: Sat Dec 8 19:46:56 2012 -0700
Add a close_stream method to Controller
Signed-off-by: Sean Robinson seankrobinson@gmail.com --- stem/control.py | 26 ++++++++++++++++++++++++++ 1 files changed, 26 insertions(+), 0 deletions(-)
diff --git a/stem/control.py b/stem/control.py index 3572987..cdcbe29 100644 --- a/stem/control.py +++ b/stem/control.py @@ -32,6 +32,7 @@ providing its own for interacting at a higher level. |- new_circuit - create new circuits |- extend_circuit - create new circuits and extend existing ones |- close_circuit - close a circuit + |- close_stream - close a stream |- repurpose_circuit - change a circuit's purpose |- map_address - maps one address to another such that connections to the original are replaced with the other |- get_version - convenience method to get tor version @@ -1477,6 +1478,31 @@ class Controller(BaseController): else: raise stem.ProtocolError("CLOSECIRCUIT returned unexpected response code: %s" % response.code)
+ def close_stream(self, stream_id, reason = stem.RelayEndReason.MISC, flag = ''): + """ + Closes the specified stream. + + :param int stream_id: id of the stream to be closed + :param int reason: reason the stream is closing (see RelayEndReason) + :param str flag: not currently used + + :raises: :class:`stem.InvalidArguments` if the stream or reason are not recognized + :raises: :class:`stem.InvalidRequest` if the stream and/or reason are missing + """ + + response = self.msg("CLOSESTREAM %s %s %s"% (str(stream_id), str(stem.RelayEndReason.index_of(reason)), flag)) + stem.response.convert("SINGLELINE", response) + + if not response.is_ok(): + if response.code in ('512', '552'): + if response.message.startswith("Unknown stream "): + raise stem.InvalidArguments(response.code, response.message, [stream_id]) + elif response.message.startswith("Unrecognized reason "): + raise stem.InvalidArguments(response.code, response.message, [reason]) + raise stem.InvalidRequest(response.code, response.message) + else: + raise stem.ProtocolError("CLOSESTREAM returned unexpected response code: %s" % response.code) + def map_address(self, mapping): """ Map addresses to replacement addresses. Tor replaces subseqent connections