mirror of
https://github.com/tomaae/homeassistant-mikrotik_router.git
synced 2025-07-05 15:04:30 +02:00
reverted back to customized fork of librouteros
This commit is contained in:
parent
1010a8aa3a
commit
0a58db40cd
8 changed files with 621 additions and 29 deletions
|
@ -0,0 +1,37 @@
|
|||
# -*- coding: UTF-8 -*-
|
||||
|
||||
from .exceptions import ConnectionClosed
|
||||
|
||||
|
||||
class SocketTransport:
|
||||
|
||||
def __init__(self, sock):
|
||||
self.sock = sock
|
||||
|
||||
def write(self, data):
|
||||
"""
|
||||
Write given bytes to socket. Loop as long as every byte in
|
||||
string is written unless exception is raised.
|
||||
"""
|
||||
self.sock.sendall(data)
|
||||
|
||||
def read(self, length):
|
||||
"""
|
||||
Read as many bytes from socket as specified in length.
|
||||
Loop as long as every byte is read unless exception is raised.
|
||||
"""
|
||||
data = bytearray()
|
||||
while len(data) != length:
|
||||
tmp = None
|
||||
try:
|
||||
tmp = self.sock.recv((length - len(data)))
|
||||
except:
|
||||
raise ConnectionClosed('Socket recv failed.')
|
||||
|
||||
data += tmp
|
||||
if not data:
|
||||
raise ConnectionClosed('Connection unexpectedly closed.')
|
||||
return data
|
||||
|
||||
def close(self):
|
||||
self.sock.close()
|
Loading…
Add table
Add a link
Reference in a new issue