Actually code:

def app(environ, start_response): start_response('200 OK', [('Content-type', 'text/html')]) with codecs.open("template.html", 'r', 'utf8') as template_file: template_content = template_file.read() return template_content 

And an empty server response

 GET / => generated 0 bytes in 1 msecs (HTTP/1.1 200) 1 headers in 44 bytes (1105 switches on core 0) 

The document is empty. If template_content is replaced with u "Hello" - the same. "Hello" - the answer is normal.

return str (template_content) gives an encoding error

 UnicodeEncodeError: 'ascii' codec can't encode characters in position 82-86: ordinal not in range(128) 

Checked on uwsgi and wsgiref.simple_server

  • Static files are best left to http servers such as nginx. If you still want to send yourself, you can send the file without transcoding (open in 'rb' mode and with chunks of yield). It’s not clear why you should create a wsgi application. Try micro-framework type bottle - jfs

1 answer 1

 return [template_content.encode('utf-8')] 
  • one
    Yes of course. I missed that unicode strings are not exactly "byte" and need to be converted. What is even sadder, day danced with a tambourine around this thought and never checked it out. Thank. If to find fault, then return [template_content.encode ('utf-8')] is more exact, since WSGI should return an iterable object whose elements are bytes. - Kamo Petrosyan
  • @KamoPetrosyan if you find fault, then in Python 2 byte strings are iterable objects whose elements are byte strings :) - andreymal
  • @andreymal application = wsgiref.validate.validator(app) will tell you not to return the string directly. It should be wrapped (in the list). - jfs
  • @jfs KamoPetrosyan your truth corrected. - Sergey Gornostaev