Add socket timeout (#155)

* Add socket timeout
This commit is contained in:
Ido Szargel 2024-06-10 16:19:01 +02:00 committed by GitHub
parent fc65326370
commit 55ac551a2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 10 additions and 3 deletions

View File

@ -31,11 +31,15 @@ class SocketReader(object):
buffer = b""
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as socket_handle:
socket_handle.settimeout(60)
socket_handle.connect((self.host, self.port))
while True:
buffer += socket_handle.recv(self.BUFFER_SIZE)
try:
buffer += socket_handle.recv(self.BUFFER_SIZE)
except socket.timeout:
logger.error("Socket timeout occurred, exiting")
break
lines = buffer.splitlines(keepends=True)

View File

@ -14,6 +14,9 @@ commands=
[pylama:dsmr_parser/clients/__init__.py]
ignore = W0611
[pylama:dsmr_parser/clients/socket_.py]
ignore = C901
[pylama:dsmr_parser/parsers.py]
ignore = W605
@ -24,4 +27,4 @@ ignore = E501
max_line_length = 120
[pylama:pycodestyle]
max_line_length = 120
max_line_length = 120