pythonpython-2.7twistedtwisted.web

Twisted html File listing gives "Request did not return bytes" error


I have a Twisted web application. I want to call a Html file for a GET request.(my Html file is in the same folder where my Twisted app runs)

    class Root(resource.Resource):
        isLeaf = False


        def render_GET(self, request):
            return self.returnResponse(request)

        def returnResponse(self, request):
            request.setHeader(b"content-type", b"text/html")
            return File("Info.html")

site = Root()
site.putChild('cache', NetworkCacheManager())
endpoints.serverFromString(reactor, "tcp:port=8080:interface=0.0.0.0").listen(server.Site(site))
reactor.run()

When I run the server, Im getting 500 Error;

Request did not return bytes

Request:

<Request at 0x10b042b48 method=GET uri=/ clientproto=HTTP/1.1>

Resource:
<__main__.Root instance at 0x10b0302d8>

Value:
FilePath('/Users/ratha/projects/TestPython/com/lob/Info.html')

What is wrong here?


Solution

  • I fixed it like;

    def returnResponse(self, request):
        f = open('Info.html', 'r')
        request.setHeader(b"content-type", b"text/html")
        return f.read()