Here is the server side:
#!/usr/local/bin/python2.4
import cgitb; cgitb.enable()
import sys
import web, os, random, time
def cgidebugerror():
_wrappedstdout = sys.stdout
sys.stdout = web._oldstdout
cgitb.handler()
sys.stdout = _wrappedstdout
web.internalerror = cgidebugerror
urls = ('/', 'upload',
'/progress+', 'progress',
'/file+', 'fileServe',
'/admin', 'admin')
web.config.debug = False
db = web.database(dbn='postgres', user='oetzi', pw='6a5f5f70',
db='oetzi')
store = web.session.DBStore(db, 'session_data')
sessiond = {'progress': 0, 'finished' : False, 'title' : "", 'path' :
""}
app = web.application(urls, globals(), autoreload=False)
session = web.session.Session(app, store, initializer=sessiond)
render = web.template.render('/home/oetzi/webapps/mlb/htdocs/
templates/')
class fileServe:
def GET(self):
arg = web.input(arg=None)
keys = {}
keys['key'] = arg.key
path = db.select('files', keys, what='path', where='key=$key')
#if not path:
# return "This file doesn't seem to exist..."
try:
download = open(path[0].path, 'r')
except IndexError:
return "This file doesn't seem to exist..."
web.header('Content-type', 'application/file')
return download.read()
class upload:
paths = {}
def GET(self):
#render html file
return render.index(0)
def POST(self):
#upload file from form
submit = web.input(myfile={})
code = submit.code
fileU = submit.myfile
filepath = fileU.filename.replace('\\','/')
name = filepath.split('/')[-1]
path = '/home/oetzi/webapps/mlb/htdocs/files/' + name
upload.paths[code] = "/mlb.py/file?key=" + name
out = open(path, 'wb', 1000)
c = 0
while (True):
packet = fileU.file.read(1000)
if not packet:
break
else:
out.write(packet)
c += 1
if (c % 5 == 0):
session.progress += c
session._save()
out.close()
session.progress = -1
session._save()
#block until client has been updated
while not session.finished:
pass
#commit to db
title = session.title
db.insert('files', key=name, title=title, path=path)
#clean up
session.kill()
path = "http://oetzi.webfactional.com" + upload.paths.pop(code)
return title + ": " + path
class progress:
def GET(self):
#take in current title and status for user with code and return
progress
arg = web.input(arg=None)
code = arg.code
if int(arg.status) > 0:
session.title = arg.title
path = session.path
session.finished = True
return path
x = session.progress
return x
def POST(self):
pass
application = app.wsgifunc()
Basically my when I upload it just does a standard html form upload
but my you'll see from my urls that that maps to upload.POST(). While
this is happening (as I send the form upload result to an iframe) I
send a progress.GET every 2 seconds from my javascript. Ignore the
references to 'code' - its just the old way I did sessions.
The problem seems to be that the gets take about 10 or 20 seconds to
return so the upload progress is so far behind it doesn't seem to do
anything for maybe 30 seconds.
Also, where I block the POST request you'll see I do a check for a
variable that I change in the progress.GET. I have a feeling this
doesn't work as the session.finished value isn't updated. Or is it?
Thanks again for all the help guys!
On Feb 9, 4:18 pm, andrei <[email protected]> wrote:
> Yep, show your code.
>
> On Feb 9, 6:24 pm, Oetzi <[email protected]> wrote:
>
>
>
>
>
>
>
> > Ah right. That was a stupid question haha. The problem at the moment
> > is mainly just that it seems to be very slow on GET requests and the
> > whole thing seems very temperamental. Should I post my code up?
>
> > On Feb 9, 9:58 am, andrei <[email protected]> wrote:
>
> > > I use it with mysql, I think postgres is ok. You can always subclass
> > > Store class and implement your storage.
>
> > > What do you mean by using cookies with sessions? Webpy's Session saves
> > > session id in cookies automatically for you.
>
> > > If you need to manually set specific cookies, then you should read
> > > thishttp://webpy.org/cookbook/cookies
>
> > > On Feb 9, 12:10 pm, Oetzi <[email protected]> wrote:
>
> > > > I'm using postgres. Is there a faster way to do it? Also, how do I use
> > > > cookies with sessions?
>
> > > > On Feb 8, 11:06 pm, andrei <[email protected]> wrote:
>
> > > > > It adds a couple of sql requests per each http request (if you use
> > > > > database backend to store session) Loads the session data, updates
> > > > > the access time and saves the session data.
>
> > > > > On Feb 9, 1:28 am, Oetzi <[email protected]> wrote:
>
> > > > > > Thanks for the comments guys. Will definitely try that out Branko.
> > > > > > One
> > > > > > thing that is weird is that it kinda works now but the GETS for
> > > > > > progress have crazy high ping when using servers instead of a global
> > > > > > variable. Does it do a database lookup on each variable access? Or
> > > > > > does it just load at the begininng of the request?
>
> > > > > > On Feb 8, 9:58 pm, Branko Vukeliæ <[email protected]> wrote:
>
> > > > > > > Also, try disabling debug if you haven't already.
>
> > > > > > > web.config.debug = False # if I remember correctly
>
> > > > > > > On Tue, Feb 8, 2011 at 8:50 PM, Oetzi <[email protected]> wrote:
> > > > > > > > I am now but it still doesn't seem to be working. Should I call
> > > > > > > > _save() on the session when I increment to keep everything
> > > > > > > > sycned?
>
> > > > > > > --
> > > > > > > Branko Vukelic
>
> > > > > > > [email protected]http://www.brankovukelic.com/
--
You received this message because you are subscribed to the Google Groups
"web.py" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/webpy?hl=en.