【问题标题】:No Such Resource 404 error没有这样的资源 404 错误
【发布时间】:2014-04-08 07:06:46
【问题描述】:

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

【问题讨论】:

    标签: python html twisted.web


    【解决方案1】:

    根据this support ticket,答案是在调用putChild() 时使用bytes() 对象。在原始帖子中,尝试:

    resource = File(b'/home/venky/python/twistedPython/index.html')

    在第一个答案中,尝试:

    resource = File(b'/home/venky/python/twistedPython/index.html') resource.putChild(b'', resource)

    【讨论】:

      【解决方案2】:

      这与以斜杠结尾的 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

      【讨论】:

        猜你喜欢
        • 2017-06-27
        • 1970-01-01
        • 2015-09-11
        • 2021-06-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-19
        • 1970-01-01
        相关资源
        最近更新 更多