Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _transmit_error(self):
"""Transmits an error to the client and terminates the exchange."""
fmt = str(
"!HH%dsx" % (len(self._stats.error["error_message"].encode("latin-1")))
)
packet = struct.pack(
fmt,
constants.OPCODE_ERROR,
self._stats.error["error_code"],
bytes(self._stats.error["error_message"].encode("latin-1")),
)
self._get_listener().sendto(packet, self._peer)
"""
# Note that we use blocking socket, because it has its own dedicated
# process. We read only 512 bytes.
try:
listener = self._get_listener()
listener.settimeout(self._timeout)
data, peer = listener.recvfrom(constants.DEFAULT_BLKSIZE)
listener.settimeout(None)
except socket.timeout:
return
if peer != self._peer:
logging.error("Unexpected peer: %s, expected %s" % (peer, self._peer))
self._should_stop = True
return
code, block_number = struct.unpack("!HH", data[:4])
if code == constants.OPCODE_ERROR:
# When the client sends an OPCODE_ERROR#
# the block number is the ERR codes in constants.py
self._stats.error = {
"error_code": block_number,
"error_message": data[4:-1].decode("ascii", "ignore"),
}
# An error was reported by the client which terminates the exchange
logging.error(
"Error reported from client: %s" % self._stats.error["error_message"]
)
self._transmit_error()
self._should_stop = True
return
if code != constants.OPCODE_ACK:
logging.error(
"Expected an ACK opcode from %s, got: %d" % (self._peer, code)