Python file sharing site

I wanted to create a simple site where one person can upload a file and transfer a random web address to someone who can upload it.

At this point, I have a web page where someone can successfully upload a file that is stored in / files / on my web server.

The python script also generates a unique random 5-letter code that is stored in a database that identifies the file

I have another page called retrieve, where a person should go, insert a 5-letter code, and he should open a file file asking to save the file.

My problem is that: 1) How to upload a file to download? At this point I get the script, take the code, get the file location on my server, but how do I get the bootloader to start the download?

2) How to get people to directly access the file? Should I change file permissions?

+3
source share
2 answers

How do you maintain the file upload page and how do you allow users to upload files?
If you use the built-in modules of the Python HTTP server, you should not have any problems.
In any case, here, as a part serving the file, it is executed using Python built-in modules (only the main idea).

As for your second question, if you used these modules in the first place, you probably would not ask about this because you will have to explicitly serve certain 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()
+2
source

HTTP-, , .

( ), 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

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

0

Source: https://habr.com/ru/post/1746916/


All Articles