简体   繁体   English

Python Web服务器和文件下载

[英]Python Web Server and File Download

I've made this simple HTTP server in Python : 我已经在Python中制作了这个简单的HTTP服务器

import SimpleHTTPServer
import SocketServer

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()

What I want to do now, but I don't know how, is the following: 我现在想做的,但我不知道怎么做的是:

When I visit http://localhost:8000/my/path/download?documentID=100 , a document (particularly a PDF file) will start being downloaded automatically. 当我访问http://localhost:8000/my/path/download?documentID=100 ,文档(尤其是PDF文件)将开始自动下载。 Of course, I have more documents but I want them to be identified by the get paramater documentID 当然,我有更多文档,但我希望通过get参数documentID进行标识

What is the easiest way to achieve this? 最简单的方法是什么? thanks 谢谢

You have to write a new handle. 您必须编写一个新的句柄。 http://fragments.turtlemeat.com/pythonwebserver.php shows a simple example. http://fragments.turtlemeat.com/pythonwebserver.php显示了一个简单的示例。

You have to: 你必须:

  • determinate the PDF-File out of the ID 根据ID确定PDF文件
  • read the pdf file 阅读pdf文件
  • send the right content-flag 发送正确的内容标志
  • send the pdf file 发送PDF文件

alternative: 替代方案:

  • write a script saving the pdfs with the ids like 1.pdf, 2.pdf 编写一个脚本来保存ID为1.pdf,2.pdf之类的pdf
  • use your example code 使用您的示例代码
  • and simple use links like http://localhost:8000/my/path/1.pdf 和简单易用的链接,例如http://localhost:8000/my/path/1.pdf

Greets Kuishi 向奎石致意

PS: English isn't my native language, sorry PS:英语不是我的母语,对不起

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM