====== File upload in Bottle (Python web framwork) ====== Autore: **//Fabio Di Matteo//** \\ Ultima revisione: **// 22/10/2022 - 20:48 //** // // ===== Lato server ===== #!/usr/bin/env python3 import os,urllib3 from bottle import Bottle,route, run, template, static_file, request,redirect, response,get, post app=Bottle() SAVEPATH="/home/fabio/Desktop/" PORT=3498 def start_server(): run(app , host='0.0.0.0', port=PORT) #import bjoern #bjoern.run(app, "0.0.0.0", PORT) @app.route('/js/') def send_js(filename): return static_file(filename, root=os.path.dirname(os.path.realpath(__file__))+'/tpl/js/') @app.route('/css/') def send_css(filename): return static_file(filename, root=os.path.dirname(os.path.realpath(__file__))+'/tpl/css/') @app.route('/img/') def send_img(filename): return static_file(filename, root=os.path.dirname(os.path.realpath(__file__))+'/tpl/img/') @app.route ("/") def home(): homepage=open(os.path.dirname(os.path.realpath(__file__))+"/tpl/uploadForm.html").read() yield template(homepage) def getPublicIpRaw(): url = "https://www.freemedialab.org/myip/rawip.php" http = urllib3.PoolManager() r = http.request('GET', url) return r.data @app.error(404) def error404(error): return ''' Invia a Fabio Di Matteo

La pagina che stai cercando non esiste!

Smettila di andare in giro per il server.


Questo è un server temporaneo. Fra poco Fabio lo spegne.

Ritorna alla home

''' @app.route('/error') def error(): c='' yield(c) @app.route('/success') def success(): c='' yield(c) @app.route('/upload',method='POST') def upload(): myfiles = request.files.getall('myfile') for myfile in myfiles: save_path = os.path.join(SAVEPATH, myfile.filename) myfile.save(save_path, overwrite=True) redirect("/success") if __name__=='__main__': ip=getPublicIpRaw().decode('utf-8') print("\nIl Tuo ip: http://%s:%s\n\n" % (ip,PORT)) start_server()
===== Il template html ===== Includere nella struttura delle cartelle del progetto sia bootstrap che jquery. Seguendo lo schema delle rotte generate dallo script python. Invia a Fabio Di Matteo

Inviami un file

===== Aggiungere anche una pagina per condividere una cartella ===== #!/usr/bin/env python3 import os,urllib3 from bottle import Bottle,route, run, template, static_file, request,redirect, response,get, post #Edit if you want SAVEPATH="/home/fabio/Desktop/" SHAREPATH="/home/fabio/Progetti/" PORT=5500 #Not edit WEBURL='' ip='' app=Bottle() def start_server(): run(app , host='0.0.0.0', port=PORT, reloader=True) #import bjoern #bjoern.run(app, "0.0.0.0", PORT) @app.route('/js/') def send_js(filename): return static_file(filename, root=os.path.dirname(os.path.realpath(__file__))+'/tpl/js/') @app.route('/css/') def send_css(filename): return static_file(filename, root=os.path.dirname(os.path.realpath(__file__))+'/tpl/css/') @app.route('/img/') def send_img(filename): return static_file(filename, root=os.path.dirname(os.path.realpath(__file__))+'/tpl/img/') @app.route ("/") def home(): homepage=open(os.path.dirname(os.path.realpath(__file__))+"/tpl/uploadForm.html").read() yield template(homepage) @app.route('/share') def homeshare(): redirect("/share/") @app.route('/share/') def fromRootShare(): line='' flist = os.listdir(SHAREPATH+os.sep) for i in range(len(flist)): if os.path.isdir(SHAREPATH+os.sep+os.sep+flist[i]): img='folder.png' line=line + '
  • {onlyFileName}
  • '.format(f=flist[i],onlyFileName=flist[i],WEBURL=WEBURL, img=img ); for i in range(len(flist)): if not os.path.isdir(SHAREPATH+os.sep+os.sep+flist[i]): img='file.png' line=line + '
  • {onlyFileName}
  • '.format(f=flist[i],onlyFileName=flist[i],WEBURL=WEBURL, img=img ); page=open(os.path.dirname(os.path.realpath(__file__))+"/tpl/share.html").read() page=page.format(line=line, WEBURL=WEBURL) return page @app.route('/share/') def send_file(filename): line='' if not os.path.isdir(SHAREPATH+os.sep+filename): return static_file(filename, SHAREPATH) else: flist = os.listdir(SHAREPATH+os.sep+filename) for i in range(len(flist)): if os.path.isdir(SHAREPATH+os.sep+filename+os.sep+flist[i]): img='folder.png' line=line + '
  • {onlyFileName}
  • '.format(f=filename+os.sep+flist[i],onlyFileName=flist[i],WEBURL=WEBURL, img=img ); for i in range(len(flist)): if not os.path.isdir(SHAREPATH+os.sep+filename+os.sep+flist[i]): img='file.png' line=line + '
  • {onlyFileName}
  • '.format(f=filename+os.sep+flist[i],onlyFileName=flist[i],WEBURL=WEBURL, img=img ); page=open(os.path.dirname(os.path.realpath(__file__))+"/tpl/share.html").read() page=page.format(line=line, WEBURL=WEBURL) return page def getPublicIpRaw(): url = "https://www.freemedialab.org/myip/rawip.php" http = urllib3.PoolManager() r = http.request('GET', url) return r.data @app.error(404) def error404(error): return ''' Invia a Fabio Di Matteo

    La pagina che stai cercando non esiste!

    Smettila di andare in giro per il server.


    Questo è un server temporaneo. Fra poco Fabio lo spegne.

    Ritorna alla home

    ''' @app.route('/error') def error(): c='' yield(c) @app.route('/success') def success(): c='' yield(c) ''' #upload singolo file @app.route('/upload',method='POST') def upload(): myfile = request.files.myfile save_path = os.path.join(SAVEPATH, myfile.filename) myfile.save(save_path, overwrite=True) print('File salvato correttamente.') redirect("/success") ''' @app.route('/upload',method='POST') def upload(): myfiles = request.files.getall('myfile') for myfile in myfiles: save_path = os.path.join(SAVEPATH, myfile.filename) myfile.save(save_path, overwrite=True) redirect("/success") if __name__=='__main__': ip=getPublicIpRaw().decode('utf-8') print("---------------------------------------------") print("\nIl Tuo ip: http://%s:%s\n\n" % (ip,PORT)) print("---------------------------------------------") if request.url.__contains__("127.0.0.1"): ip="localhost" WEBURL="http://{ip}:{PORT}/".format(ip=ip,PORT=PORT) start_server()