diff options
| author | troido <troido@protonmail.com> | 2020-03-06 11:26:59 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-03-06 11:26:59 +0100 |
| commit | dd07ff4d686f07cdc9736627dd0ef099ef5e4e4f (patch) | |
| tree | f43310b3779a95efd74a40ba8139cf146ad49051 /asciifarmclient/connection.py | |
| parent | c9366616079240cd7ee3d243c9f6897d40b4267d (diff) | |
new directory structure for the separate client repo
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) |
