I have basic twister app and i keep getting errors like that:
Request did not return bytes
Request:
Resource:
<main.MainPageDispatcher object at 0x7f049fa62be0>
Value:
'hello'
Everywhere, even in official docs' examples I see that string is returned and yet it not works for me. If I comment out first return and send bytes instead of string it is working. Can anyone help me understand how it works? If it has to be in bytes then why official guides are returning strings?
My code:
from twisted.web.server import Site
from twisted.web.static import File
from twisted.web.resource import Resource
from twisted.internet import reactor
class MainPageDispatcher(Resource):
isLeaf = True
def __init__(self):
super().__init__()
def render_GET(self, request):
request.setHeader(b"content-type", b"text/html")
return "hello"
return bytes("hello", "utf-8")
root = MainPageDispatcher()
factory = Site(root)
reactor.listenTCP(8888, factory)
reactor.run()
In python3 I'm using:
def render_GET(self, request):
request.setHeader("Content-Type", "text/html; charset=utf-8")
return "<html>Hello, world!</html>".encode('utf-8')
str.encode('utf-8')
returns a bytes representation of the Unicode string
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With