diff --git a/TP1/soap/.soap-client.py.swp b/TP1/soap/.soap-client.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..d9907ad2928ab17b24f37d2f76af56c9bf455001 Binary files /dev/null and b/TP1/soap/.soap-client.py.swp differ diff --git a/TP1/soap/.soap-server.py.swp b/TP1/soap/.soap-server.py.swp new file mode 100644 index 0000000000000000000000000000000000000000..c8d9d2b208520ea8e793df6f52c8e0c0cb15aab9 Binary files /dev/null and b/TP1/soap/.soap-server.py.swp differ diff --git a/TP1/soap/soap-client.py b/TP1/soap/soap-client.py new file mode 100644 index 0000000000000000000000000000000000000000..b3ea35e4d635e02543312e195052519aa20b1951 --- /dev/null +++ b/TP1/soap/soap-client.py @@ -0,0 +1,45 @@ +import SOAPpy +import argparse +import pprint +import os + +# put here your own ip +SERVER_URL = 'http://127.0.0.1:8123' + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Simple SOAP FTP client') + + subparsers = parser.add_subparsers(help='sub-command help', dest='which') + + parser_list = subparsers.add_parser('list',help='list directory') + parser_list.add_argument('directory', type=str) + + parser_get = subparsers.add_parser('get', help='get file') + parser_get.add_argument('file', type=str) + parser_get.add_argument('destination', type=str) + + parser_put = subparsers.add_parser('put', help='put file in ftp') + parser_put.add_argument('source', type=str) + parser_put.add_argument('destination', type=str) + + args = parser.parse_args() + server = SOAPpy.SOAPProxy(SERVER_URL) + if args.which == 'list': + dir, sdirs, files = server.list(args.directory) + print "[%s]" % dir + for d in sdirs: + print "[%s/%s]" % (dir, d) + for f in files: + print "%s/%s" % (dir, f) + elif args.which == 'get': + directory, filename, data = server.get(args.file) + with open(args.destination, 'w+') as f: + f.write(data) + elif args.which == 'put': + with open(args.source, 'r') as src: + data = src.read() + print server.put(args.destination, data) + + + + diff --git a/TP1/soap/soap-server.py b/TP1/soap/soap-server.py new file mode 100644 index 0000000000000000000000000000000000000000..77e03fb7499ad50c12c53634ebb611a88054c72b --- /dev/null +++ b/TP1/soap/soap-server.py @@ -0,0 +1,47 @@ +import SOAPpy +import sys +import argparse +from os.path import expanduser, join, isdir, abspath +from os import listdir + +main_directory = None + +def list(relative_directory): + global main_directory + target_directory = join(main_directory, relative_directory) if relative_directory != '.' else main_directory + dirs = listdir(target_directory) + s = [d for d in dirs if isdir(d)] + f = [f for f in dirs if not isdir(d)] + return (target_directory, s, f) + +def get(relative_path): + global main_directory + with open(join(main_directory,relative_path), 'r') as f: + payload = f.read() + return (main_directory, relative_path, payload) + +def put(relative_path, payload): + global main_directory + new_file = join(main_directory, relative_path) + try: + with open(new_file, 'w+') as f: + f.write(payload) + except: + return 'Error while writing %s :(' % new_file + return '%s added successfuly!' % new_file + +def start_ftp(**kwargs): + global main_directory + main_directory = kwargs.get('directory') + server = SOAPpy.SOAPServer(('0.0.0.0', kwargs.get('port'))) + server.registerFunction(list) + server.registerFunction(get) + server.registerFunction(put) + server.serve_forever() + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Simple FTP server using SOAP') + parser.add_argument('-p', '--port', type=int, required=True, help='Listening port') + parser.add_argument('-d', '--directory', type=str, required=True, help='Home directory of the FTP') + args = parser.parse_args() + start_ftp(port=args.port, directory=abspath(args.directory))