Question

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()
Was it helpful?

Solution 2

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.

OTHER TIPS

As per this support ticket, the answer is to use bytes() objects when calling putChild(). In the original post, try:

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

And in the first answer, try:

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top