diff options
Diffstat (limited to 'asciifarm/client/connection.py')
| -rw-r--r-- | asciifarm/client/connection.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/asciifarm/client/connection.py b/asciifarm/client/connection.py new file mode 100644 index 0000000..9ca2ccc --- /dev/null +++ b/asciifarm/client/connection.py @@ -0,0 +1,27 @@ + +import socket + +from .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 + 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) + callback(data) + + def send(self, message): + send(self.sock, bytes(message, 'utf-8')) |
