From 1ea33ddd54d9b30105a33c09ee03b2c4035e600e Mon Sep 17 00:00:00 2001 From: "anthony.boulmier" <anthony.boulmier@hesge.ch> Date: Mon, 19 Sep 2016 16:38:57 +0200 Subject: [PATCH] example SOAP/REST codes --- TP1/rest/rest-client.py | 54 +++++++++++++++++++++++++++++++++++ TP1/rest/rest-server.py | 49 +++++++++++++++++++++++++++++++ TP1/soap/.soap-client.py.swp | Bin 12288 -> 0 bytes TP1/soap/.soap-server.py.swp | Bin 12288 -> 0 bytes TP1/soap/soap-server.py | 10 +++---- 5 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 TP1/rest/rest-client.py create mode 100644 TP1/rest/rest-server.py delete mode 100644 TP1/soap/.soap-client.py.swp delete mode 100644 TP1/soap/.soap-server.py.swp diff --git a/TP1/rest/rest-client.py b/TP1/rest/rest-client.py new file mode 100644 index 0000000..431ec07 --- /dev/null +++ b/TP1/rest/rest-client.py @@ -0,0 +1,54 @@ +import urllib2 +import argparse +import pprint +import os +import json +from os.path import join +# put here your own ip +SERVER_URL = 'http://127.0.0.1:8122' + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Simple REST 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() + + if args.which == 'list': + directory = join(args.which, args.directory) if args.directory != '.' else args.which + req = urllib2.Request(join(SERVER_URL, directory)) + pprint.pprint(join(SERVER_URL, directory)) + req.add_header('Content-Type', 'application/json') + response = json.loads(urllib2.urlopen(req).read()) + dir, sdirs, files = response['dir'], response['subdirs'], response['files'] + 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': + req = urllib2.Request(join(SERVER_URL, join(args.which, args.file))) + req.add_header('Content-Type', 'application/json') + response = json.loads(urllib2.urlopen(req).read()) + directory, filename, data = response['dir'], response['file'], response['data'] + with open(args.destination, 'w+') as f: + f.write(data) + elif args.which == 'put': + req = urllib2.Request(join(SERVER_URL, args.which)) + req.add_header('Content-Type', 'application/json') + with open(args.source, 'r') as src: + data = src.read() + response = urllib2.urlopen(req, json.dumps({'destination': args.destination, 'data': data})).read() + print response + diff --git a/TP1/rest/rest-server.py b/TP1/rest/rest-server.py new file mode 100644 index 0000000..56583a9 --- /dev/null +++ b/TP1/rest/rest-server.py @@ -0,0 +1,49 @@ +from flask import Flask, request, jsonify +import argparse +from os.path import expanduser, join, isdir, abspath +from os import listdir + +app = Flask(__name__) +main_directory = None + +@app.route('/list', defaults={'relative_directory': '.'}) +@app.route('/list/<path:relative_directory>') +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(f)] + return jsonify(dir=target_directory, subdirs=s, files=f) + +@app.route('/get/<path:relative_path>') +def get(relative_path): + global main_directory + with open(join(main_directory,relative_path), 'r') as f: + data = f.read() + return jsonify(dir=main_directory, file=relative_path, data=data) + +@app.route('/put', methods=['POST']) +def put(): + from pprint import pprint + + relative_path = request.json['destination'] + data = request.json['data'] + global main_directory + new_file = join(main_directory, relative_path) + try: + with open(new_file, 'w+') as f: + f.write(data) + except: + return 'Error while writing %s :(' % new_file + return '%s added successfuly!' % new_file + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Simple REST FTP server') + 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') + parser.add_argument('-D', '--debug', action='store_true', help='Activate debug mode') + args = parser.parse_args() + main_directory = abspath(args.directory) + + app.run(host='0.0.0.0', port=args.port, debug=args.debug) diff --git a/TP1/soap/.soap-client.py.swp b/TP1/soap/.soap-client.py.swp deleted file mode 100644 index d9907ad2928ab17b24f37d2f76af56c9bf455001..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmYc?2=nw+FxN9-U|?VnU|^8Ab3gp2oCHIE4g*6)YDr-ZNSpu$tJC!}H82bd$EUOo ztWZDL#S?^eO9~A2i}Mo;z=UpcPG)LeiC#e^J`+aeMnhmU1jq`3(vmb?3tk3eBSQm_ zeq|*^1!1935OWldhQMeDjE2By2#kinXb6mkz-S1JhQMeDjF1p0DPUx%XJBApg8Ek% zN;9I-Q0^!-8UmvsFd71*Aut*OqaiRF0;3@?8UmvsFd71*Aut*OqaiQ^Lm)AQfnhx- z14BI*Wd0x4|NqF(!0?ctf#EVg1H(yv28I*-3=A9j85nx_85p|x85j!q85mUg85k7! z85l0|F)&=<V_@jwV_*p5V_*p7V_*>FV_<mB%fPUSmw{m|F9SmwF9U-aF9QQ3F9QPu zF9X9v9tMW(JPZu8co-Oxco-Pmc^DY<c^DWtco-Nya5FHR=4N16%gw-0%+0{y%+0`{ z$<4sP&CS4YpNoOv02c#8KNkZ-8y5pZD;EPp3m3%wp<E0M+FT3_|2Y{L9&rxA$QX6< zXb6mkz-S1JhQMeDjE2By2v8aV3Pb@01_qcqIHRB_Gp|IUIJKxOwMeg^v_vDZC>^AT zIJLA;S(IL^my%jsl9`uSl9`{UqmYtVlBfxHFvv=fh=Q#`aZ$2fQEFm}hNc2sx;(Qa zLm|H)H4o&GV!h)0(xT*49R>9wbxnoDVvrUqu-ep|%ru2WkX_{&naLRnwzdlD1*IkG z5Lu8VAe^RGUX)pqssXYP;RG~0P#mjXt_`s%4PjDBW>IQ#Nq$kKjzU^yPHJ9aZYsoA zAU{AtK0Os2^2K@}1rVFCxiCEy-Gz|&Q&KI~S1ndjP*u=K$t==QNP`#w_CkJ<LYhKm z9@y$)grh)mP|eX`-LVMmDX7|0K-!B_GK-M3Bea6`rDPU?gB4^d*acwa5dRh<yqS|( z4D}~GOdwWaMg~YJG&UfqQ^8gt*xxarpi&P+1r+61RB8mf28Fo>#fJv@K#T!JsDiCR zL1IxcC^>_uc#xQeCM4-YB;pH7OY{;`Qb1Crxv6<28tO=KsIH?>Qdy8{TU=5EF%+jJ zNaDhyUIFCu(j<rh#b7HTYz=T)(^1Gs%_*=2vB9we3g)zu0(FSB;P8Yfk55k}!@-!E zK#2>NbJJ5x@HiKg@L<jbsRWyZ8mOQM1A8&CC><86u!N7xQji|pVFZ#@*MWo)h^+um zH0n^Ri{XI_ayuljz-~ZDXh8HAmnP{Z=jY}o=B0o`U0p{36z8_;;OvhX<O;T+(gAFu zo+H#l0ig6$q>++ZoLrPy04khp)q^v03vyDyDO15MBtQYOx?Nq9Au~-OJ|2|S<Kw}F zOMHB8VrE`^yt)+w10vZf*ea-Jl#~=$>FXOBnd=$o8R!{WSr{4_t1~Do6qJ@IWTY0Q wDpcl|7AfSH=P6_sFfe517UUO|DC8HzSOws`2;(IdrGs4!<AX9wL8Sr%0Q{vA@Bjb+ diff --git a/TP1/soap/.soap-server.py.swp b/TP1/soap/.soap-server.py.swp deleted file mode 100644 index c8d9d2b208520ea8e793df6f52c8e0c0cb15aab9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 12288 zcmYc?2=nw+FxN9-U|?VnU|`U{azETdU4o%Mhk>CYwWKfyBu)T>)#>_~8W@I!<5OA( zR;VBB;t9gKB?X51#rcT^U_!SzwWut$NUxw0p9!OKqaiRF0_2APFN3j>p#ez0vXY{L zuuv$7If_R^U^E0qLtr!nMnhmU1V%$(Gz3ONU^E0qNC=b^Ff!CLFfcGd{i_e98PRAc zca$0pfzc2c4S~@R7!85Z5Eu=C(GVC7fzc2c4S~@R7!85Z5Ez0XkeI^2u%Cm0VLB&t z{+|H?KJha!Jm+U%xX#bOu!x_5p_`w9p^Kk^p@g4-p_re6A(Wqi!J40e;S?VO!%{v5 zhI&2*hEP5R217mu1_M3@1_?d}hJU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5pnqDPWZ;$SP4)rDPU?6oRq@NHe<C5KGZT2}UQ}>6#$7!95EXR#3E6P}fseNX;ou zMavLix8n{xkV6rv0B#^CyPzg61zQEb{Jc~KhP0ymT!s8%h0I(~)>Qx*kdj#h6V?Nz uJeXW+ML}X-N@;Owkq$V$>Og{2M*&(NFhI?Km5eZcab+=#2`WJfDj5Ku<y-3j diff --git a/TP1/soap/soap-server.py b/TP1/soap/soap-server.py index 77e03fb..c43384a 100644 --- a/TP1/soap/soap-server.py +++ b/TP1/soap/soap-server.py @@ -11,21 +11,21 @@ def list(relative_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)] + f = [f for f in dirs if not isdir(f)] 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) + data = f.read() + return (main_directory, relative_path, data) -def put(relative_path, payload): +def put(relative_path, data): global main_directory new_file = join(main_directory, relative_path) try: with open(new_file, 'w+') as f: - f.write(payload) + f.write(data) except: return 'Error while writing %s :(' % new_file return '%s added successfuly!' % new_file -- GitLab