1

我想运行 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()
4

2 回答 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)

于 2017-11-02T14:58:58.707 回答
1

这与以斜杠结尾的 URL 与不以斜杠结尾的 URL 之间的区别有关。Twisted 似乎认为顶级 URL(如您的http://localhost:8000)包含隐式尾随斜杠(http://localhost:8000/)。这意味着 URL 路径包含一个空段。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.

于 2014-04-25T15:51:55.960 回答