本文整理汇总了Python中stem.OperationFailed方法的典型用法代码示例。如果您正苦于以下问题:Python stem.OperationFailed方法的具体用法?Python stem.OperationFailed怎么用?Python stem.OperationFailed使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stem
的用法示例。
在下文中一共展示了stem.OperationFailed方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: save_conf
# 需要导入模块: import stem [as 别名]
# 或者: from stem import OperationFailed [as 别名]
def save_conf(self):
"""
Saves the current configuration options into the active torrc file.
:raises:
* :class:`stem.ControllerError` if the call fails
* :class:`stem.OperationFailed` if the client is unable to save
the configuration file
"""
response = self.msg('SAVECONF')
stem.response.convert('SINGLELINE', response)
if response.is_ok():
return True
elif response.code == '551':
raise stem.OperationFailed(response.code, response.message)
else:
raise stem.ProtocolError('SAVECONF returned unexpected response code')
示例2: map_address
# 需要导入模块: import stem [as 别名]
# 或者: from stem import OperationFailed [as 别名]
def map_address(self, mapping):
"""
Map addresses to replacement addresses. Tor replaces subseqent connections
to the original addresses with the replacement addresses.
If the original address is a null address, i.e., one of '0.0.0.0', '::0', or
'.' Tor picks an original address itself and returns it in the reply. If the
original address is already mapped to a different address the mapping is
removed.
:param dict mapping: mapping of original addresses to replacement addresses
:raises:
* :class:`stem.InvalidRequest` if the addresses are malformed
* :class:`stem.OperationFailed` if Tor couldn't fulfill the request
:returns: **dict** with 'original -> replacement' address mappings
"""
mapaddress_arg = ' '.join(['%s=%s' % (k, v) for (k, v) in list(mapping.items())])
response = self.msg('MAPADDRESS %s' % mapaddress_arg)
stem.response.convert('MAPADDRESS', response)
return response.entries
示例3: _attach_stream
# 需要导入模块: import stem [as 别名]
# 或者: from stem import OperationFailed [as 别名]
def _attach_stream(self, event):
""" Attach stream to circuit. """
try:
self._controller.attach_stream(event.id, self._cid)
except (OperationFailed, InvalidRequest), error:
error = str(error)
# If circuit is already closed, close stream too.
if error in (('Unknown circuit "%s"' % self._cid),
"Can't attach stream to non-open origin circuit"):
self._controller.close_stream(event.id)
# Ignore the rare cases (~5*10^-7) where a stream has already been
# closed almost directly after its NEW-event has been received.
elif error == 'Unknown stream "%s"' % event.id:
sys.stderr.write('Stream %s has already been ' +
'closed.\n' % event.id)
else:
raise
示例4: attach_stream
# 需要导入模块: import stem [as 别名]
# 或者: from stem import OperationFailed [as 别名]
def attach_stream(self, stream_id, circuit_id, exiting_hop = None):
"""
Attaches a stream to a circuit.
Note: Tor attaches streams to circuits automatically unless the
__LeaveStreamsUnattached configuration variable is set to '1'
:param str stream_id: id of the stream that must be attached
:param str circuit_id: id of the circuit to which it must be attached
:param int exiting_hop: hop in the circuit where traffic should exit
:raises:
* :class:`stem.InvalidRequest` if the stream or circuit id were unrecognized
* :class:`stem.UnsatisfiableRequest` if the stream isn't in a state where it can be attached
* :class:`stem.OperationFailed` if the stream couldn't be attached for any other reason
"""
query = 'ATTACHSTREAM %s %s' % (stream_id, circuit_id)
if exiting_hop:
query += ' HOP=%s' % exiting_hop
response = self.msg(query)
stem.response.convert('SINGLELINE', response)
if not response.is_ok():
if response.code == '552':
raise stem.InvalidRequest(response.code, response.message)
elif response.code == '551':
raise stem.OperationFailed(response.code, response.message)
elif response.code == '555':
raise stem.UnsatisfiableRequest(response.code, response.message)
else:
raise stem.ProtocolError('ATTACHSTREAM returned unexpected response code: %s' % response.code)
示例5: get_microdescriptors
# 需要导入模块: import stem [as 别名]
# 或者: from stem import OperationFailed [as 别名]
def get_microdescriptors(self, default = UNDEFINED):
"""
get_microdescriptors(default = UNDEFINED)
Provides an iterator for all of the microdescriptors that tor currently
knows about.
**Tor does not expose this information via the control protocol**
(:trac:`8323`). Until it does this reads the microdescriptors from disk,
and hence won't work remotely or if we lack read permissions.
:param list default: items to provide if the query fails
:returns: iterates over
:class:`~stem.descriptor.microdescriptor.Microdescriptor` for relays in
the tor network
:raises: :class:`stem.ControllerError` if unable to query tor and no
default was provided
"""
try:
data_directory = self.get_conf('DataDirectory')
except stem.ControllerError as exc:
raise stem.OperationFailed(message = 'Unable to determine the data directory (%s)' % exc)
cached_descriptor_path = os.path.join(data_directory, 'cached-microdescs')
if not os.path.exists(data_directory):
raise stem.OperationFailed(message = "Data directory reported by tor doesn't exist (%s)" % data_directory)
elif not os.path.exists(cached_descriptor_path):
raise stem.OperationFailed(message = "Data directory doens't contain cached microescriptors (%s)" % cached_descriptor_path)
with stem.descriptor.reader.DescriptorReader([cached_descriptor_path]) as reader:
for desc in reader:
# It shouldn't be possible for these to be something other than
# microdescriptors but as the saying goes: trust but verify.
if not isinstance(desc, stem.descriptor.microdescriptor.Microdescriptor):
raise stem.OperationFailed(message = 'BUG: Descriptor reader provided non-microdescriptor content (%s)' % type(desc))
yield desc
示例6: connect_tor_http
# 需要导入模块: import stem [as 别名]
# 或者: from stem import OperationFailed [as 别名]
def connect_tor_http(controller,
circuit_path,
job,
conn_timeout,
curlopts=None,
curlinfos=None):
"""
This helper function will perform an HTTP request over Tor. It will not
perform any special action in the event that this is the experimental flow,
but can be customised on a per-call basis through the curlopts argument.
"""
if curlopts is None:
curlopts = {}
curlopts[pycurl.PROXY] = "localhost"
curlopts[pycurl.PROXYPORT] = 9050
curlopts[pycurl.PROXYTYPE] = pycurl.PROXYTYPE_SOCKS5_HOSTNAME
attach_error = []
try:
if circuit_path is not None:
circuit_path = circuit_path.split(",")
circuit_id = controller.new_circuit(circuit_path, await_build=True)
except stem.CircuitExtensionFailed:
return {"spdr_state": CONN_DISCARD}
def attach_stream(stream):
try:
if stream.status == 'NEW':
if (stream.target_address == job['dip'] and
stream.target_port == job['dp']):
controller.attach_stream(stream.id, circuit_id)
except stem.OperationFailed:
attach_error.append(None)
controller.add_event_listener(attach_stream, stem.control.EventType.STREAM) # pylint: disable=no-member
result = connect_http(None, job, conn_timeout, curlopts, curlinfos)
controller.remove_event_listener(attach_stream)
if len(attach_error) > 0:
return {"spdr_state": CONN_DISCARD}
return result