[Web development with Python] Measures against garbled characters when outputting html

I've tried many things, but this method is the best. Characters are not garbled when started as a web server or locally.

import io
import os
import sys

html_body = 'HTML format'

if type(sys.stdout) == io.TextIOWrapper:
    sys.stdout = io.TextIOWrapper(
        sys.stdout.buffer, 
        encoding='utf-8'
print ('Content-type: text/html; charset=UTF-8\r\n')
print (html_body)

Recommended Posts

[Web development with Python] Measures against garbled characters when outputting html
[Web development with Python] Precautions when saving cookies
[Web development with Python] query is also redirect
Explosive speed with Python (Bottle)! Web API development
[EC2] Prevent garbled characters when capturing with selenium
Segfault Python with 33 characters
[Ipdb] Web development beginners tried to summarize debugging with Python
[Linux] How to deal with garbled characters when viewing files
Web application development with Flask
Web API with Python + Falcon
Error when playing with python
Web application with Python + Flask ② ③
Web scraping beginner with python
Streamline web search with python
Web application with Python + Flask ④
Half-width katakana characters are not garbled when using python + selenium execute_script
Proxy measures when using WEB API
Web scraping with Python ① (Scraping prior knowledge)
Getting Started with Python Web Applications
Web scraping with Python First step
I tried web scraping with python.
Monitor Python web apps with Prometheus
Get web screen capture with python
[Python] One-liner Stalin sort with 50 characters
When matplotlib doesn't work with python2.7
Prepare Python development environment with Atom
Application development with Docker + Python + Flask
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
[Python] Format when to_csv with pandas
How to deal with the problem that Japanese characters are garbled when outputting logs using JSON log formatter