Python服务器无法访问URL中包含转义字符的文件

2024-09-29 23:27:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个用于mp3流的python服务器。使用扭曲矩阵库

如果我尝试访问文件a.mp3,它会正常工作

但是这个文件,例如衝.mp3不起作用,它说“找不到文件”

此URL转义文件名为%E8%A1%9D.mp3,但无法访问它

如果我尝试使用unicode而不是符号来访问它,比如\u885d.mp3,它仍然会显示“未找到文件”

下面是代码,请注意,我必须放置request.path = request.path.replace('%20', ' '),因为这是它访问路径中有空格的文件的唯一方法。我认为这不应该是正常的行为

class playMP3(Resource):
    isLeaf = True
    def render_GET(self, request):
        this=urlparse.urlparse(request.path)#scheme,netloc,path,query
        root,ext=os.path.splitext(this.path)
        filename=os.path.basename(request.path)
        fileFolder=request.path.replace(filename,"")
        self.serverRoot=os.getcwd()
        print (request.path)
        if ext==".mp3":
        request.path = request.path.replace('%20', ' ')
            thisFile=File(self.serverRoot+request.path)
            return File.render_GET(thisFile,request)

resource = playMP3()
factory = Site(resource)
reactor.listenTCP(8880, factory)
reactor.run()

我也试着把request.path = urllib.unquote(request.path)放进去,但不是把它解码成衝.mp3,而是变成了ÞíØ.mp3。奇怪


Tags: 文件pathselfgetosrequestrenderfilename

热门问题