0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Python.HTMLfoam受信・表示UTF-8

Posted at

Python でブラウザに日本語で表示させる。

ポイント

        value0 = urllib.parse.unquote(value0)

code

#!C:/Python/python -X utf8
# coding=utf-8

# This is a python header. 
################## python(anconda header) ###################
#!C:/anaconda/python -X utf8
# coding=utf-8
#############################################################
# 2024/01/23
#      information.       
# File      my_python.cgi
# address1  http://localhost/python_test/my_python.cgi
# address2  http://127.0.0.1/python_test/my_python.cgi
# environment  xampp:xampp/htdocs/python_test/
# outside:Python/python 3.12.1

## script Start
this_script = 'http://localhost/python_test/my_python.cgi'

point_err = bool(1)
point_return = bool(0)
settig_msgA = 'NO Settig.'
settig_msgB = 'NO Settig.'
error_detail = 'No Error.'
def my_error(error_detail):
    if point_err:
        print ("Content-type:text/html\n\n")
        print ('<!DOCTYPE html><html lang=\"ja\"><head><meta charset=\"UTF-8\"><title>Error</title></head><body>')
    print ('<h1>エラー発生(An error has occurred.)</h1><h2>detail :'+error_detail+'</h2><div style=\"background-color: #000066;color: #cccccc;font-size: 12px;margin: 3px;padding: 3px;\">'+settig_msgA+'</div><div style=\"background-color: #006600;color: #cccccc;font-size: 12px;margin: 3px;padding: 3px;\">'+settig_msgB+'</div><br>')
    if point_return:
        return
    print ('</body></html>\n')
    exit()
    
# my_error('オープン前エラー Occurrence before opening <b>test.</b>')

print ('Content-type:text/html'+'\n'+'\n')
print ('<!DOCTYPE html><html lang=\"ja\"><head><meta charset=\"UTF-8\"><title>my pyton</title></head><BODY BGCOLOR=\"#ffffff\">\n')
point_err = bool(0)
# ここからはErrorでヘッダーが要らない。
point_return = bool(1)
# ここからはError表示後戻ってくる。
print ('<h1>test system</h1>'+'\n')
print ('<p>日本語を話せる、システムです。「This is a system that allows you to speak Japanese.」<br>\n')
print ('カンニングペーパー「コピペオンライン英和翻訳 『Multilingal Online Translation』」はMicrosoftストアーで。<br>\n')
print ('The cheat sheet \"Copy and Paste Online English-Japanese Translation 『Multilingal Online Translation』\" is available at the Microsoft Store.<br>\n')
print ('ブラウザ、拡張機能もお忘れなく。わからない時は、質問AIですよ。「Don\'t forget your browser and extensions. When you don\'t understand, there is a question AI.」</p><br>\n')

import datetime
now = datetime.datetime.now()
formatted_date = now.strftime("%Y/%m/%d %H:%M:%S")
it_day = now.strftime("%Y年%m月%d日")
it_time = now.strftime("%H時%M分%S秒")

# 受信
import re
import sys
import os

req_met = os.environ.get('REQUEST_METHOD')
if req_met == 'POST':
    for query in sys.stdin:
        os.environ.get('CONTENT_LENGTH')
        pass
else :
    query = os.environ.get('QUERY_STRING')
    if len(query) > 0 and req_met != 'POST':
        tmp_q_m = '<b>not post.</b>:'+str(len(query))+'<br>query:'+query
        settig_msgB = tmp_q_m
        # 受信したアドレスバーに付いている情報をBに入れておく。2行は最終的に要らない。
if len(query) > 10 :
    language = len(query)
    
tmp_q_m = '<b>get or post.</b>:'+str(len(query))+'<br>query:'+query
settig_msgB = tmp_q_m
my_error('test Test query.<b>test.</b>')

my_hash = {}
#my_hkey_list0 = list(my_hash.keys())
my_hash['name'] = ''
my_hash['subm'] = ''
my_hash['comment'] = ''
my_hash['url'] = ''
my_hash['l_tim'] = formatted_date
my_hash['to_day'] = it_day
# 一応、使うhash(連想配列)のkeyとValue(値)を初期化して置く。
come = ''

key_myh = list(my_hash.keys())

import urllib.parse
key0=''
value0=''
buf_arrey = query.split("&")
from urllib.parse import parse_qs

if "" != buf_arrey:
    for my_buf in buf_arrey:
        mytem_buf = my_buf
        mytem_buf += '='
        key_val_arry = mytem_buf.split("=")
        key0 = key_val_arry[0]
        value0 =key_val_arry[1]
        key0 = re.sub(r'\+', ' ', key0)
        key0 =  urllib.parse.unquote(key0)        
        key0 = re.sub(r'&', '&amp;', key0)
        key0 = re.sub(r'<', '&lt;', key0)
        key0 = re.sub(r'>', '&gt;', key0)
        key0 = re.sub(r'\r\n', '<br>', key0)
        key0 = re.sub(r'\r|\n', '<br>', key0)
        my_key = key0
        value0 = re.sub(r'\+', ' ', value0)
        value0 = urllib.parse.unquote(value0)        
        value0 = re.sub(r'&', '&amp;', value0)
        value0 = re.sub(r'<', '&lt;', value0)
        value0 = re.sub(r'>', '&gt;', value0)
        value0 = re.sub(r'\r\n', '<br>', value0)
        value0 = re.sub(r'\r|\n', '<br>', value0)
        my_val = value0
        if my_key == 'comment':
            come = re.sub(r'<br>', '\n', value0)
        
        key_c=0
        for key_myh_s in key_myh:
            if key0 == key_myh_s:
                key_c=1
        if key_c==0:
            if "" != my_val:
                my_val =  value0

        my_hash[my_key] = my_val

def my_key_value():
    key_list0 = list(my_hash.keys())
    print(key_list0)
    print ('<br>\n')
    for i, val0 in enumerate(key_list0):
        if i == len(key_list0) - 1:
            print('last: '+val0+' :'+my_hash[val0]+'<br>\n')
        else:
            print('middle '+val0+' :'+my_hash[val0]+'<br>\n')

my_key_value()

print('<br>\n')
print("現在の日付と時刻: ", now)
print('<br>'+formatted_date+' Japanise style. きょうは、'+it_day+'で時計は'+it_time+'<br>\n')
print ('これをComment欄に貼り付けでご利用してください。\"%E6%97%A5%E6%9C%AC%E8%AA%9E%E9%9D%9E%E5%AF%BE%E5%BF%9C\"<br>\n')
#print ('<form action=\"'+this_script+'\" method=\"GET\">\n')
print ('<form action=\"'+this_script+'\" method=\"POST\">\n')
print ('<table border=0 cellspacing=1>\n')
print ('<tr><td><b>name</b></td><td><input type=text name=name size=28 value=\"'+my_hash['name']+'\" maxlength=\"12\" class=f></td></tr>\n')
print ('<tr><td><b>submit</b></td><td><input type=text name=subm size=100 value=\"'+my_hash['subm']+'\" maxlength=\"100\"class=f></td></tr>'+'\n')
print ('<tr><td colspan=2><b>comment</b><br><textarea cols=70 rows=7 name=comment wrap=\"soft\" class=f>'+come+'</textarea></td></tr>'+'\n')
print ('<tr><td><b>url</b></td><td><input type=text size=52 name=url value=\"'+my_hash['url']+'\" maxlength=\"60\" class=f></td></tr>'+'\n')
print ('<tr><td><input type=submit value=\"submit\"></td><td><input type=reset value=\"reset\"></td></tr>\n')
print ('</form></table><br>\n')
print('<b><font color="#f0f">receives</font></b><br>\n')
print ('name='+my_hash['name']+'<br>Submit='+my_hash['subm']+'<br>comment<br>'+my_hash['comment']+'<br>It OK?<br>\n')
print ('<br><a href="'+this_script+'">reload</a><br>\n')

# my_error('test message.all end!!')
print ('<br><br><br></body></html>\n')

exit()

urllib.parse.unquote(value0)を使ってやる。perlの手法、代入置換にこだわらない事。pythonはpythonなのだ。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?