Skip to content

Commit

Permalink
Add socket timeout (#155)
Browse files Browse the repository at this point in the history
* Add socket timeout
  • Loading branch information
ido-szargel authored Jun 10, 2024
1 parent fc65326 commit 55ac551
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
8 changes: 6 additions & 2 deletions dsmr_parser/clients/socket_.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ def read(self):
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)

Expand Down
5 changes: 4 additions & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -24,4 +27,4 @@ ignore = E501
max_line_length = 120

[pylama:pycodestyle]
max_line_length = 120
max_line_length = 120

0 comments on commit 55ac551

Please sign in to comment.