On 9/22/14, Aldo Bassanini <abassan...@gmail.com> wrote:
> Hello everyone.
> Is there any way to control the final status of the response.stream
> function?
>
> I need to serve a small file to a client application (the client app uses
> wget, not a browser), but I wish to know if the client successfully
> downloaded the file, in order to update some server-side data.
>
> The way that I am thinking to achieve this, is programming the client
> application (a shell script) to GET a second URL (sending a session-id or
> something like that) from the server, after downloading the file, but this
> means, that I have to develop some file validation logic to verifiy the
> correct downloading (md5suming? or alikes) in the client, that I prefer to
> avoid, since the client has a very small environment (OpenWrt's Busybox
> Shell) and very small CPU and RAM.


If I understood correctly, you only need to know if the file was
successfuly downloaded
or not on the server side.

Don't know if there are better ways in web2py but...
...this code, that goes into a controller, should do the trick:

import os, sys

def abort(status_code, message=None):
    raise HTTP(404, message)

def index():
    filename = request.args(0) or abort(404)
    fullpath = os.path.join(request.folder, 'uploads', filename)

    def stream():
        try:
            with open(fullpath, 'rb') as f:
                while True:
                    data = f.read(1024)
                    if not data:
                        break
                    yield data
        except:
            # with any error, assume file not fully downloaded
            print >>sys.stderr, 'failed'
        else:
            # if you reach this point the file was successfully
            # downloaded. Do what you need on the backend side.
            print >>sys.stderr, 'done'

    headers = {'Content-Type': 'application/octet-stream'}
    raise HTTP(200, stream(), **headers)


PS: You dont need to do any checksum anywhere. TCP is reliable.
It guarantees that everything that is transmitted is error free and ordered.


Ricardo

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to