You appear to have a good start there, and some good suggestions as well.
You'll probably want to create a miniature command interpreter, one command for file length, and another command for each of the other things you need to be able to do. A big if/elif/elif/.../else right after your main socket.recv() should do it.
However, when you write a socket program, you leave the possibility of subtle errors cropping up if you assume a one to one relationship between your socket.send()'s and your socket.recv()'s. TCP sockets reserve the right to aggregate multiple socket.send()'s into one packet, or to split a packet into multiple recv's, and about any other size alteration it can think of for performance or reliability.
For this reason, I like to use my bufsock.py module, to make the length rearrangements a nonissue, and simply retrieving data at the same time:
http://stromberg.dnsalias.org/~dstromberg/bufsock.html
BTW, depending on what kind of info you need, it might be easier to just sshfs or CIFS or NFS the filesystem, and access it as though it's local.
Anyway, in case you want or need to use sockets, here's an example in Python 3. It's based on SJP's post:
#!/usr/bin/python3
import os
import sys
import time
import socket as socket_mod
sys.path.insert(0, '/usr/local/lib')
import bufsock as bufsock_mod
if sys.argv[1:] and sys.argv[1] == 'server':
# Echo server program
HOST = 'localhost' # Symbolic name meaning the local host
PORT = 50007 # Arbitrary non-privileged port
socket = socket_mod.socket(socket_mod.AF_INET, socket_mod.SOCK_STREAM)
socket.bind((HOST, PORT))
socket.listen(1)
connection, address = socket.accept()
print('Connected to from {}'.format(address))
bufsock = bufsock_mod.bufsock(connection)
while True:
print('Reading command')
sys.stdout.flush()
command = bufsock.readto(b'\0')[:-1]
print('Got {} command'.format(command))
sys.stdout.flush()
if command == b'stat':
filename = bufsock.readto(b'\0')[:-1]
length = os.stat(filename).st_size
length_bytes = bytes(str(length), 'ISO-8859-1')
bufsock.send(length_bytes + b'\0')
bufsock.flush()
elif command == b'date':
bufsock.send(bytes(time.ctime(), 'ISO-8859-1') + b'\0')
bufsock.flush()
elif command == b'exit':
sys.stderr.write('{}: terminating on exit command\n'.format(sys.argv[0], ))
bufsock.close()
sys.exit(0)
else:
sys.stderr.write('{}: Illegal command received: {}\n'.format(sys.argv[0], command))
bufsock.close()
sys.exit(1)
elif sys.argv[1:] and sys.argv[1] == 'client':
# Echo client program
HOST = 'localhost' # The remote host
PORT = 50007 # The same port as used by the server
socket = socket_mod.socket(socket_mod.AF_INET, socket_mod.SOCK_STREAM)
socket.connect((HOST, PORT))
bufsock = bufsock_mod.bufsock(socket)
# Get the length of /etc/services
bufsock.send(b'stat\0/etc/services\0')
bufsock.flush()
length = bufsock.readto(b'\0')
print('length of /etc/services is {}'.format(length))
bufsock.send(b'date\0')
bufsock.flush()
date = bufsock.readto(b'\0')
print('date is {}'.format(date))
bufsock.send(b'exit\0')
bufsock.close()
else:
sys.stderr.write('{}: Must call with "server" or "client" argument\n'.format(sys.argv[0]))
sys.exit(1)
HTH :)