Skip to content

Instantly share code, notes, and snippets.

@HaruhiroTakahashi
Last active July 11, 2021 03:12
Show Gist options
  • Save HaruhiroTakahashi/fab77e99708ff218af4a to your computer and use it in GitHub Desktop.
Save HaruhiroTakahashi/fab77e99708ff218af4a to your computer and use it in GitHub Desktop.
PythonでWebアプリ(文字化け解決)
import http.server
server_address = ("", 80)
handler_class = http.server.CGIHTTPRequestHandler
server = http.server.HTTPServer(server_address, handler_class)
server.serve_forever()
#!/usr/bin/env python
# coding: utf-8
#2.xでは .encode("utf-8")、.encode("shift-jis")などとすることで、出力するHTMLのエンコーディングを変更出来たのですが、
#3.xではこのテクニックは使えず、出力のエンコーディングはsys.stdout.encodingに固定です。
#では、3.xではどうすれば良いかというと、sys.stdoutの値自体を変えてしまえば良いのです。
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
#自動でutf-8にエンコードされて出力される
import cgi
from datetime import datetime
html_body="""
<html><meta charset="utf-8"><body>
%s
</body></html>"""
content=''
form=cgi.FieldStorage()
year_str=form.getvalue('year','')
if not year_str.isdigit():
content="西暦を入力してください"
else:
year=int(year_str)
friday13=0
for month in range(1,13):
date=datetime(year, month, 13)
if date.weekday()==4:
friday13+=1
content+="%d年%d月13日は金曜日です" % (year, date.month)
content+="<br>"
if friday13:
content+="%d年には合計%d個の13日の金曜日があります" % (year, friday13)
else:
content+="%d年には13日の金曜日がありません"
print ("Content-type: text/html; charset=utf-8\n")
print (html_body % content)
#!/usr/bin/env python
# coding: utf-8
import cgi
from datetime import datetime
html_body="""
<html><meta charset="utf-8"><body>
%s
</body></html>"""
content=''
form=cgi.FieldStorage()
year_str=form.getvalue('year','')
if not year_str.isdigit():
content='西暦を入力してください'
else:
year=int(year_str)
friday13=0
for month in range(1,13):
date=datetime(year, month, 13)
if date.weekday()==4:
friday13+=1
content+=u"%d年%d月13日は金曜日です" % (year, date.month)
content+=u"<br>"
if friday13:
content+=u"%d年には合計%d個の13日の金曜日があります" % (year, friday13)
else:
content+=u"%d年には13日の金曜日がありません"
print ("Content-type: text/html; charset=utf-8\n")
print (html_body % content.encode('utf-8'))
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
<form action="/cgi-bin/find13f.py" method="GET"> 13日の金曜日が何日にあるかを探します。 <br /> 西暦を入力してください。 :
<input type="text" name="year" />
<input type="submit" name="submit" />
</form>
</body>
</html>
#!/usr/bin/env python
# coding: utf-8
#2.xでは .encode("utf-8")、.encode("shift-jis")などとすることで、出力するHTMLのエンコーディングを変更出来たのですが、
#3.xではこのテクニックは使えず、出力のエンコーディングはsys.stdout.encodingに固定です。
#では、3.xではどうすれば良いかというと、sys.stdoutの値自体を変えてしまえば良いのです。
import sys
import io
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
#自動でutf-8にエンコードされて出力される
import cgi
from datetime import datetime
html_body="""
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form method="POST" action="/cgi-bin/find13f.py">
西暦を選んでください: <select name="year">
%s
</select>
<input type="submit" />
</form>
%s </body>
</html>"""
options=''
content=''
now=datetime.now()
for y in range(now.year-10, now.year+10):
if y!=now.year:
select=''
else:
select=' selected="selected"'
options+="<option%s>%d</option>" % (select, y)
form=cgi.FieldStorage()
year_str=form.getvalue('year','')
if year_str.isdigit():
year=int(year_str)
friday13=0
for month in range(1,13):
date=datetime(year, month, 13)
if date.weekday()==4:
friday13+=1
content+="%d年%d月13日は金曜日です" % (year, date.month)
content+="<br />"
if friday13:
content+="%d年には合計%d個の13日の金曜日があります" % (year, friday13)
else:
content+="%d年には13日の金曜日がありません"
print ("Content-type: text/html; charset=utf-8\n")
print (html_body % (options, content))
@ytakashina
Copy link

文字化けしていたので助かりました。ありがとうございます。
もう少し調べてみたところ、エンコーディングの変更は、 3.7 以降では sys.stdout.reconfigure(encoding='utf-8') だけでもよいようです。
https://stackoverflow.com/questions/4374455/how-to-set-sys-stdout-encoding-in-python-3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment