#!/usr/bin/python import cgi, os import cgitb; cgitb.enable() try: # Windows needs stdio set for binary mode. import msvcrt msvcrt.setmode (0, os.O_BINARY) # stdin = 0 msvcrt.setmode (1, os.O_BINARY) # stdout = 1 except ImportError: pass form = cgi.FieldStorage() # Generator to buffer file chunks def fbuffer(f, chunk_size=10000): while True: chunk = f.read(chunk_size) if not chunk: break yield chunk # A nested FieldStorage instance holds the file fileitem = form['file'] # Test if the file was uploaded if fileitem.filename: # strip leading path from file name to avoid directory traversal attacks fn = os.path.basename(fileitem.filename) fn_begin,fn_end = os.path.splitext(fileitem.filename) f = open('files/' + fn, 'wb', 10000) if fn_end == '.xlsx': xrun = 'xlsx2csv /srv/wwww/cgi-bin/files/' + fn + ' | in2csv -f csv | csvsql --db postgresql+psycopg2://mmg:yeahyeah@192.168.100.230:5432/names --table ' + fn_begin + ' --insert' else: xrun = 'in2csv /srv/wwww/cgi-bin/files/' + fn + ' | csvsql --db postgresql+psycopg2://mmg:yeahyeah@192.168.100.230:5432/names --table ' + fn_begin + ' --insert' # Read the file in chunks for chunk in fbuffer(fileitem.file): f.write(chunk) f.close() p = sub.Popen(['/bin/bash', '-c', xrun], stdout=sub.PIPE, stderr=sub.STDOUT) output = urllib.unquote(p.stdout.read()) message = 'The file "' + fn + '" was uploaded successfully' else: message = 'No file was uploaded' print """\ Content-Type: text/html\n <html><body> <p>%s</p> </body></html> """ % (message, xrun, output)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question