uploading to server with a cgi script

uploading to server with a cgi script

Postby metulburr » Sun Mar 31, 2013 1:24 am

There were a couple questions i had regarding this. Firstly, i know a framework is better, but i just wanted to add code to the cgi i had already in place. Secondly, i tried uploading from localhost, but without thinking about it...so i went to another location today, and found out to upload idk maybe 250 MB took a LONG time, about 30-40 minutes. And thirdly, i have the program sending the user to another page upon completion of the upload (which if it takes 30-40 minutes, i would need to "notify" the user what is going on, like maybe a progress bar. But the current layout, you would click upload, and nothing happens until its completed, and then you switch over to the page thats says "hey your done". Which i noticed as i led someone through the upload process today, for impatient users, this doesnt work as all you get is a laoding mouse with nothing telling you what is going on. For all you know the server crashed, or something, but no i told em to just let it sit for awhile, and i did finally
upload, but if i wasnt there, they would of closed it out thinking it was broken.

I am not sure if 1) its upload speed is slow because of my programming, or because its cgi, etc. and 2) I am not sure how to implement a progress bar into it *without using javascript*, or is that not possible?


the upload .html file portion.
Code: Select all
<h3>Online Wedding Album</h3>
<div class="sudo_box3">
<p>Took pictures or videos at the wedding and want to submit them to us?</p>
<form enctype="multipart/form-data" action="cgi-bin/uploadfile.py" method="POST">
Choose File(s): <input name="userfile" type="file" multiple/><br />

Upload one or many at a time <br />
<input type="submit" value="Upload" />
</form>


the upload file
Code: Select all
#!/usr/bin/env python3
import cgi, os
import cgitb; cgitb.enable()

style_ = '''
body{
    background-image:url(../image/wed_pic.png);
    background-attachment:fixed;
    background-position:center;
    background-repeat:no-repeat;

    /*cover entire screen regardless of size of window*/
    -moz-background-size: cover;
    -webkit-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}
'''

try:
   import msvcrt
   msvcrt.setmode (0, os.O_BINARY)
   msvcrt.setmode (1, os.O_BINARY)
except ImportError:
   pass

form = cgi.FieldStorage()
message = ''
try:
   try:
   #   for upload in form:
      for upload in form['userfile']:
   #      upload = form['userfile']
         if upload.filename:
            name = os.path.basename(upload.filename)
            out = open('/var/www/wedding_album/' + name, 'wb', 1000)
            filepath = "http://www.metulburr.com/wedding_album/{}".format(name)
            message += "The file '" + name + "' was uploaded successfully<br>"
         else:
            #message += "There was an error in uploading the file {}<br>".format(name)
            pass
         while True:
            packet = upload.file.read(1000)
            if not packet:
               break

            out.write(packet)
         out.close()

   except TypeError:
    #   message = 'Keyerror'
            upload = form['userfile']
            if upload.filename:
                name = os.path.basename(upload.filename)
                out = open('/var/www/wedding_album/' + name, 'wb', 1000)
                filepath = "http://www.metulburr.com/wedding_album/{}".format(name)
                message += "The file '" + name + "' was uploaded successfully<br>"
            else:
                #message += "There was an error in uploading the file {}<br>".format(name)
                pass
            while True:
                packet = upload.file.read(1000)
                if not packet:
                    break

                out.write(packet)
            out.close()
except NameError: #user left field blank
    filepath = ''
    message = 'You did not submit anything! <br> Click Browse to select files'



html = '''Content-type: text/html\n

<html>
<title>Wedding Pictures</title>
<style>{STYLE}</style>

<body>
<center>
<a href="http://www.metulburr.com/wedding.html">Return</a>
<h3>{msg}</h3>
Thank you for participating in our wedding. <br /><br />
<!--<a href="{filepath}">{filepath}</a>-->
<a href="http://www.metulburr.com/cgi-bin/weddingpics.py">View Wedding Album</a>
</center>

</body>
</html>
'''.format(msg=message, filepath=filepath, STYLE=style_)
#html.format(msg=message, filepath=filepath)
print(html)



this was pretty much the process of uploading:

Code: Select all
...
 out = open('/var/www/wedding_album/' + name, 'wb', 1000)
...
            while True:
                packet = upload.file.read(1000)
                if not packet:
                    break

                out.write(packet)
...

Not sure if there is a faster way of doing htis or if i am doing something particulor wrong.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: uploading to server with a cgi script

Postby stranac » Mon Apr 01, 2013 5:16 pm

metulburr wrote:1) its upload speed is slow because of my programming, or because its cgi

My guess would be it's neither of those.
250MB is quite big, the limiting factor is probably the connection speed.

metulburr wrote:2) I am not sure how to implement a progress bar into it *without using javascript*, or is that not possible?

No, that's impossible to do without using javascript.
But there are javascript(mostly jQuery) scripts/plugins that make file upload progress bars quite simple to implement, even with very basic knowledge of javascript.
https://github.com/blueimp/jQuery-File-Upload seems to be the currently recommended thing to use.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1144
Joined: Thu Feb 07, 2013 3:42 pm

Re: uploading to server with a cgi script

Postby snippsat » Tue Apr 02, 2013 12:48 pm

its upload speed is slow because of my programming, or because its cgi

Why do you use CGI?
The reason i ask is because CGI is outdated and inefficient spam a new process for each call/request.
Now is WSGI the new Python standard,as pointed out in pep-0333
HOWTO Use Python in the web
Writing CGI programs, while still possible, is no longer recommended.
With WSGI, a topic covered later in this document, it is possible to write programs that emulate CGI, so they can be run as CGI if no better option is available.

For more on the difference read this.
http://stackoverflow.com/questions/2191 ... t-together

A good way is to look into micro framework like Flask,Bottle they are build on WSGI and support WSGI 100%.
Uploading files Flask Uploading files Bottle
User avatar
snippsat
 
Posts: 221
Joined: Thu Feb 21, 2013 12:04 am

Re: uploading to server with a cgi script

Postby metulburr » Tue Apr 02, 2013 1:26 pm

prob because it was the first exp from a book. Plus it seemed quick and easy and short. I used WSGI once, but i really didnt see the difference in it, except the complexity to get it working in a server as opposed to cgi.

I havent really tinkered yet too far into anything other than cgi. CGI makes sense to me, where i havnet got too far into a framework to grasp it yet. I so far have tried to jump in flask, bottle, django, and even WSGI, but i ended up just not using it and reverting to cgi as it was the only thing that made sense to me.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1471
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: uploading to server with a cgi script

Postby snippsat » Tue Apr 02, 2013 6:07 pm

For me is simple,we as Python programmers should be very happy when the Python community comes together as set a good new standard as WSGI.
WSGI is 100% Python.
CGI is really low level and not Python,it just dumps stuff into the process environment as standard in/out.
CGI is annoying and forks a subprocess for every request.
prob because it was the first exp from a book

Some book are old and outdated,the web and tool we need are not.
User avatar
snippsat
 
Posts: 221
Joined: Thu Feb 21, 2013 12:04 am


Return to Web Development

Who is online

Users browsing this forum: No registered users and 3 guests