문제

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()
도움이 되었습니까?

해결책 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.

다른 팁

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)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top