pythonhtmltwisted.web

No Such Resource 404 error


I want to run index.html. so when I type localhost:8080 then index.html should be executed in browser. but its giving no such resource. I am specifying the entire path of index.html. please help me out.??

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()

Solution

  • This is related to the difference between a URL ending with a slash and one without. It appears that Twisted considers a URL at the top level (like your http://localhost:8000) to include an implicit trailing slash (http://localhost:8000/). That means that the URL path includes an empty segment. Twisted then looks in the resource for a child named '' (empty string). To make it work, add that name as a child:

    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()
    

    Also, your question has port 8080 but your code has 8000.