diff options
Diffstat (limited to 'asciifarmclient/connection.py')
| -rw-r--r-- | asciifarmclient/connection.py | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/asciifarmclient/connection.py b/asciifarmclient/connection.py new file mode 100644 index 0000000..370ef8a --- /dev/null +++ b/asciifarmclient/connection.py @@ -0,0 +1,30 @@ + +import socket + +from asciifarmclient.common.tcommunicate import send, receive + +class Connection: + + def __init__(self, socketType): + if socketType == "abstract" or socketType == "unix": + sockType = socket.AF_UNIX + elif socketType == "inet": + sockType = socket.AF_INET + else: + raise ValueError("Invalid socket type: %r" % (socketType,)) + self.sock = socket.socket(sockType, socket.SOCK_STREAM) + + def connect(self, address): + self.sock.connect(address) + + def listen(self, callback, onError): + while True: + try: + data = receive(self.sock) + except Exception as err: + onError(err) + else: + callback(data) + + def send(self, message): + send(self.sock, message) |
