Commits

Dustin Chapman committed dadf778

Start of client.

  • Participants
  • Parent commits 5e7f23b

Comments (0)

Files changed (2)

src/totus/__init__.py

+import client
+import re
+import sys
+
+
+def main(argv=None):
+
+    if not argv:
+        argv = sys.argv
+
+    with client.Client('localhost', 873) as c:
+        motd = c.exchange()
+        print motd
+        if re.search(r'@RSYNCD: EXIT', motd):
+            return 1
+        modules = c.list_modules()
+        print modules
+        if re.search(r'@RSYNCD: EXIT', motd):
+            return 0
+
+    return 99
+
+
+if __name__ == '__main__':
+    sys.exit(main())

src/totus/client.py

+import socket
+
+
+class Client(object):
+    """Client to connect to an rsync daemon."""
+
+    def __init__(self, host, port):
+        self.host = host
+        self.port = port
+        self.sock = socket.socket()
+
+    def __enter__(self):
+        self.connect()
+        return self
+
+    def __exit__(self, type, value, traceback):
+        self.close()
+
+    def connect(self):
+        self.sock.connect((self.host, self.port))
+
+    def close(self):
+        self.sock.shutdown(socket.SHUT_RDWR)
+        self.sock.close()
+
+    def exchange(self, files=[]):
+        self.sock.send('@RSYNCD: 30.0\n')
+        remote_protocol = self.sock.recv(1024)
+        self.sock.send('%s\n' % ','.join(files))
+        motd = self.sock.recv(1024)
+        return motd
+
+    def list_modules(self):
+        print 'list_modules'
+        self.sock.send('#list\n')
+        module_list = self.sock.recv(1024)
+        return module_list