How to serve any file type with Python's BaseHTTPRequestHandler

后端 未结 3 1323
醉酒成梦
醉酒成梦 2021-02-13 21:06

Consider the following example:

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

class MyHandler(         


        
3条回答
  •  猫巷女王i
    2021-02-13 21:40

    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.

提交回复
热议问题