如何使用 Python 的 BaseHTTPRequestHandler 提供任何文件类型

发布于 2024-07-26 03:44:31 字数 1155 浏览 7 评论 0原文

考虑以下示例:

import string,cgi,time
from os import curdir, sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class MyHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        try:
            if self.path.endswith(".html"):
                f = open(curdir + sep + self.path) #self.path has /test.html
#note that this potentially makes every file on your computer readable by the internet

                self.send_response(200)
                self.send_header('Content-type',    'text/html')
                self.end_headers()
                self.wfile.write(f.read())
                f.close()
                return

        except IOError:
            self.send_error(404,'File Not Found: %s' % self.path)


def main():
    try:
        server = HTTPServer(('', 80), MyHandler)
        print 'started httpserver...'
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C received, shutting down server'
        server.socket.close()

if __name__ == '__main__':
    main()

如果我还想服务器 ZIP 文件怎么办...我该怎么做? 我认为这条线行不通?

self.wfile.write(f.read())

Consider the following example:

import string,cgi,time
from os import curdir, sep
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

class MyHandler(BaseHTTPRequestHandler):

    def do_GET(self):
        try:
            if self.path.endswith(".html"):
                f = open(curdir + sep + self.path) #self.path has /test.html
#note that this potentially makes every file on your computer readable by the internet

                self.send_response(200)
                self.send_header('Content-type',    'text/html')
                self.end_headers()
                self.wfile.write(f.read())
                f.close()
                return

        except IOError:
            self.send_error(404,'File Not Found: %s' % self.path)


def main():
    try:
        server = HTTPServer(('', 80), MyHandler)
        print 'started httpserver...'
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C received, shutting down server'
        server.socket.close()

if __name__ == '__main__':
    main()

What if I want to server a ZIP file also... how would I do that?
I don't think this line would work right?

self.wfile.write(f.read())

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浪菊怪哟 2024-08-02 03:44:31

将二进制文件作为参数传递给 open()。 这个:

f = open(curdir + sep + self.path, 'rb')

而不是这个:

f = open(curdir + sep + self.path)

UNIX 不区分二进制和文本,但 Windows 区分。 但如果脚本在 UNIX 上执行,“b”将被忽略,因此您是安全的。

Pass binary as a parameter to open(). This:

f = open(curdir + sep + self.path, 'rb')

Instead of this:

f = open(curdir + sep + self.path)

UNIX doesn't distinguish between binary and text, but windows does. But if the script executes on UNIX, the "b" will just be ignored so you're safe.

倾其所爱 2024-08-02 03:44:31

你的线路会工作得很好。 问题在于适当地设置Content-type。 您需要将其设置为 application/zip 而不是 text/html

Your line would work just fine. The problem would be setting the Content-type appropriately. You'd want to set it to application/zip instead of text/html.

手心的温暖 2024-08-02 03:44:31

如果您想共享任何类型的文件夹中的文件,那么您也可以尝试键入命令

python -m SimpleHTTPServer

这将在端口 8000 启动服务器,您可以浏览文件(通过目录列表)

If you want to share files in a folder of any type, then you can also try typing the command

python -m SimpleHTTPServer

This will start the server at port 8000 and you can browse the files (via directory listing)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文