未找到资源 404 错误
我想运行index.html文件。所以当我在浏览器里输入localhost:8080的时候,应该能打开index.html。但是它显示没有这个资源。我已经指定了index.html的完整路径。请帮帮我。??
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.static import File
resource = File('/home/venky/python/twistedPython/index.html')
factory = Site(resource)
reactor.listenTCP(8000, factory)
reactor.run()
2 个回答
5
根据这个支持票,解决方法是使用bytes()
对象来调用putChild()
。在最初的帖子中,可以试试:
resource = File(b'/home/venky/python/twistedPython/index.html')
而在第一个回答中,可以试试:
resource = File(b'/home/venky/python/twistedPython/index.html')
resource.putChild(b'', resource)
1
这段内容讲的是网址(URL)后面有没有斜杠的区别。看起来,Twisted这个框架会把顶级网址(比如你的 http://localhost:8000
)当作是包含一个隐含的斜杠的,也就是 http://localhost:8000/
。这意味着这个网址的路径里有一个空的部分。然后,Twisted会在 resource
中查找一个名为 ''
(空字符串)的子项。为了让它正常工作,你需要把这个名字作为一个子项添加进去:
from twisted.internet import reactor
from twisted.web.server import Site
from twisted.web.static import File
resource = File('/home/venky/python/twistedPython/index.html')
resource.putChild('', resource)
factory = Site(resource)
reactor.listenTCP(8000, factory)
reactor.run()
另外,你的问题提到的是端口 8080
,但你的代码里用的是 8000
。