Replace tpo git repository URL by gitlab
[stem.git] / stem / response / mapaddress.py
blob698771092a5b5dd061bbf89737c7908e366f39c9
1 # Copyright 2012-2020, Damian Johnson and The Tor Project
2 # See LICENSE for licensing information
4 import stem.response
5 import stem.socket
8 class MapAddressResponse(stem.response.ControlMessage):
9 """
10 MAPADDRESS reply. Responses can contain a mixture of successes and failures,
11 such as...
15 512-syntax error: invalid address '@@@'
16 250 1.2.3.4=tor.freehaven.net
18 This only raises an exception if **every** mapping fails. Otherwise
19 **mapped** enumerates our successes and **failures** lists any
20 failure messages.
22 :var dict mapped: mapping between the original and replacement addresses
23 :var list failures: failure listed within this reply
25 :raises:
26 * :class:`stem.OperationFailed` if Tor was unable to satisfy the request
27 * :class:`stem.InvalidRequest` if the addresses provided were invalid
28 """
30 def _parse_message(self) -> None:
31 if not self.is_ok():
32 for code, _, message in self.content():
33 if code == '512':
34 raise stem.InvalidRequest(code, message)
35 elif code == '451':
36 raise stem.OperationFailed(code, message)
37 else:
38 raise stem.ProtocolError('MAPADDRESS returned unexpected response code: %s', code)
40 self.mapped = {}
41 self.failures = []
43 for code, _, message in self.content():
44 if code == '250':
45 try:
46 key, value = message.split('=', 1)
47 self.mapped[key] = value
48 except ValueError:
49 raise stem.ProtocolError(None, "MAPADDRESS returned '%s', which isn't a mapping" % message)
50 else:
51 self.failures.append(message)