views:

82

answers:

2

I wanted to design a simple site where one person can upload a file, and pass off the random webaddress to someone, who can then download it.

At this point, I have a webpage where someone can successfully upload a file which gets stored under /files/ on my webserver.

The python script also generates a unique, random 5 letter code that gets stored in a database identifying the file

I have another page called retrieve, where a person should go, put in the 5 letter code, and it should pop up a filebox asking where to save the file.

My Problem is that: 1) How do I retrieve the file for download? At this point my retrieve script, takes the code, gets the location of the file on my server, but how do I get the brower to start downloading?

2)How do I stop people from directly going to the file? Should I change permissions on the file?

+1  A: 

How do you serve the file-upload page, and how do you let your users upload files?
If you are using Python's built-in HTTP server modules you shouldn't have any problems.
Anyway, here's how the file serving part is done using Python's built-in modules (just the basic idea).

Regarding your second question, if you were using these modules in the first place you probably wouldn't have asked it because you'd have to explicitly serve specific files.

import SocketServer
import BaseHTTPServer


class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):

    def do_GET(self):
        # The URL the client requested
        print self.path

        # analyze self.path, map the local file location...

        # open the file, load the data
        with open('test.py') as f: data = f.read()

        # send the headers
        self.send_response(200)
        self.send_header('Content-type', 'application/octet-stream') # you may change the content type
        self.end_headers()
        # If the file is not found, send error code 404 instead of 200 and display a message accordingly, as you wish.

        # wfile is a file-like object. writing data to it will send it to the client
        self.wfile.write(data)

        # XXX: Obviously, you might want to send the file in segments instead of loading it as a whole


if __name__ == '__main__':

    PORT = 8080 # XXX

    try:
        server = SocketServer.ThreadingTCPServer(('', 8080), RequestHandler)
        server.serve_forever()
    except KeyboardInterrupt:
        server.socket.close()
hello
Thanks for the reply. As I have just begun to learn python, i dont really get this. I ran this code locally on my machine, replacing test.py with a5.pdf and went to the web address 127.0.0.1:8080. The script prompted to download a file but downloaded a file with the extension .dmz.part. How do I get it to download the whole file?Can you point me towards a good tutorial on the subject matter?
Ali
A: 

You should send the right HTTP Response, containing the binary data and making the browser react on it.

Try this (I haven't) if you're using Django:

response = HttpResponse()
response['X-Sendfile'] = os.path.join(settings.MEDIA_ROOT, file.file.path)
content_type, encoding = mimetypes.guess_type(file.file.read())            
if not content_type:
    content_type = 'application/octet-stream'            
response['Content-Type'] = content_type            
response['Content-Length'] = file.file.size            
response['Content-Disposition'] = 'attachment; filename="%s"' % file.file.name
return response

Source: http://www.chicagodjango.com/blog/permission-based-file-serving/

Tommy Jakobsen
Thanks for the reply. I really havent started django, so cant say if this will work. Could you rather point me to a good tutorial on the subject matter? will urllib.urlretrieve help?
Ali
What web framework are you using?
Tommy Jakobsen