A simple file upload web UI in Python



            Often times we may have to come up with upload utility for business users to be able to share files with technical group. This can be done by simply letting business user upload his file using WinSCP or similar ftp tool , but what if your business user is not comfortable doing it. In such scenarios you can use this utility to come up with a simple and real quick web UI which accepts file from user and puts them into your Linux server.

python-upload.py
#!/usr/bin/env python
import cgi
import urllib,urllib2,json
import os
from subprocess import Popen, PIPE
print 'Content-Type: text/html\n\n'
print ''
user.name=cmluat&op=LISTSTATUS"


def log_banner(title,message):
print("------------------------- "+title+" ----------------------------------------------------")
print( message     )
print("-----------------------------------------------------------------------------")

def list_files(env,path):
webhdfs_path=webhdfs_url+path+"?user.name="+env_info[env]['HDFS_USER']+"&op="+env_info[env]['listfiles']
#log_banner("final webhdfs url",webhdfs_path)
response = urllib.urlopen(webhdfs_path)
data = json.loads(response.read())
if 'FileStatuses' in data:
file_lists=data['FileStatuses']['FileStatus']
return file_lists;
#print(file_lists)
'''
if len(file_lists) > 0:
print(file_lists)
else:
print("Zero files")
'''
else:
print("Error : "+str(data))

def home_page():
files=list_files('DEV',"/")
print """<html>
<head>
 <title>Feature information</title>
</head>
<body>
<table border="1">"""

print "<tr><th>Name</th><th>Size</th><th>User</th><th>Group</th><th>Permissions</th><th>Date</th></tr>"

for file in files:
print("<tr>")
print("<td>")
print(file['pathSuffix'])
print("</td>")
                print("<td>")
                print(file['blockSize'])
                print("</td>")
                print("<td>")
                print(file['owner'])
                print("</td>")
                print("<td>")
                print(file['group'])
                print("</td>")
                print("<td>")
                print(file['permission'])
                print("</td>")
                print("<td>")
                print(file['modificationTime'])
                print("</td>")
print("</tr>")

print """</table>
</body></html>"""

def file_upload_form():
print"""<html>
<head>
 <title>File Upload Util</title>
</head>
<body>
<form action="/cgi-bin/python-upload.py"
enctype="multipart/form-data" method="post">

<p>
<h2>
Please specify a file, or a set of files to be uploaded:</h2>
<input type="file" name="datafile" size="40">
</p>
<div>
<input type="submit" value="Upload">
</div>
</form>
</body></html>"""

def uploadfile(fileitem):
if fileitem.filename:
#fn = os.path.basename(fileitem.filename)
fn = os.path.basename(fileitem.filename.replace("\\", "/" ))
open('/tmp/' + fn, 'wb').write(fileitem.file.read())
message = 'The file "' + fn + '" was uploaded successfully'
print "%s." % message 
else:
message = 'No file was uploaded'

print """
<html>
<body>
   <p>%s</p>
</body>
</html>
""" % (message,)

#home_page()
#file_upload_form()
form = cgi.FieldStorage();

if form.getvalue("datafile"):
fileitem=form["datafile"]
uploadfile(fileitem)
else:
file_upload_form()


Save this file somewhere  in cgi-bin folder

To bring up the simple webserver on port 8000 , run this

1. Switch to the parent folder
2. ls cgi-bin  #you should be able to list the python-upload.py file from here
3. chmod =R 755 cgi-bin
4. nohup python -m CGIHTTPServer 8000 &


Comments

Popular Posts