G-Coder
G-Coder

Reputation: 87

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

Upvotes: 1

Views: 4187

Answers (2)

Will Charlton
Will Charlton

Reputation: 922

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)

Upvotes: 5

Peter Westlake
Peter Westlake

Reputation: 5036

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.

Upvotes: 2

Related Questions