You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让Twisted HTTP服务器忽略索引文件并展示目录内容

解决Twisted HTTP服务器忽略索引文件展示目录内容的方法

默认情况下,Twisted的File资源会自动检测目录中的index、index.html等索引文件,并优先显示这些文件的内容,而非目录列表。要让服务器忽略索引文件,直接展示目录内容,只需要修改File实例的indexNames属性:

修改后的代码如下:

from twisted.web.server import Site
from twisted.web.static import File
from twisted.internet import reactor

resource = File('./')
# 将索引文件名列表设为空,禁用自动加载索引文件的逻辑
resource.indexNames = []
factory = Site(resource)
reactor.listenTCP(8888, factory)
reactor.run()

原理说明

indexNames是File类内置的属性,用来定义服务器优先查找的索引文件名(默认包含index、index.html等)。把这个列表设置为空后,Twisted就不会尝试加载任何索引文件,当访问目录时会直接返回目录下的文件和子目录列表。

内容的提问来源于stack exchange,提问作者pro neon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 08:40:48