Python中的文件共享站点 [英] File Sharing Site in Python

查看:141
本文介绍了Python中的文件共享站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设计一个简单的网站,让一个人可以上传一个文件,然后把随机的网络地址传给某个人,然后他就可以下载它。



点,我有一个网页,有人可以成功地上传一个文件存储在/文件/我的网络服务器上。

python脚本还生成一个唯一的随机5个字母存储在数据库中的代码标识文件

我有另外一个叫做retrieve的页面,一个人应该放入5个字母的代码中,然后弹出一个文件夹询问保存文件的位置。



我的问题是:1)如何检索文件以供下载?在这一点上我的检索脚本,需要的代码,得到我的服务器上的文件的位置,但我怎么让浏览器开始下载?



2)如何我是否阻止人们直接去文件?我应该改变文件的权限吗?


如果你正在使用Python内置的HTTP服务器模块,你不应该有任何问题。

无论如何,这里是文件服务部分是使用Python的内置模块完成的(只是基本的想法)。

关于第二个问题,如果你首先使用这些模块,因为你必须显式地提供特定的文件。

pre $ import $ Socket $ $ b $ import BaseHTTPServer

$ b $ class RequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):

def do_GET(self):
#客户请求的URL
print self.path

#分析self.path,映射本地文件位置...

#打开文件,加载数据
w ith open('test.py')作为f:data = f.read()

#发送头文件
self.send_response(200)
self.send_header(' Content-type','application / octet-stream')#你可以改变内容类型
self.end_headers()
#如果找不到文件,发送错误码404而不是200,并显示一个相应的信息,如你所愿。

#wfile是一个类似文件的对象。写数据将它发送到客户端
self.wfile.write(data)

#XXX:显然,您可能希望将文件分段发送,而不是将其作为整个


如果__name__ =='__main__':

PORT = 8080#XXX

try:
server = SocketServer.ThreadingTCPServer(('',8080),RequestHandler)
server.serve_forever()
,除了KeyboardInterrupt:
server.socket.close()


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?

解决方案

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

这篇关于Python中的文件共享站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆