Python(cgi)で簡易掲示板の作り方のソースを表示
←
Python(cgi)で簡易掲示板の作り方
ナビゲーションに移動
検索に移動
あなたには「このページの編集」を行う権限がありません。理由は以下の通りです:
この操作は、以下のグループに属する利用者のみが実行できます:
登録利用者
。
このページのソースの閲覧やコピーができます。
<h1>python簡易掲示版-cgi編-</h1> <p>cgiで実装できる物理的に操作しているものになります。<br> 仕組みは、POSTで送られて来るURLを取得し、分解、辞書型に入れることで、名前とコメントを表示している<br> ちなみに最後にRefresh(更新)しているのは、表示遅延をなくす為です。</p> <h3>0.これが導入したときのやつ</h3> [[ファイル:PythonCGI D.gif|500px|中央]] <h4>-事前準備-</h4> <p>・cgiファイル<br> ・txtファイル(コメント記録用)<br> ※いずれも、chmodでその他に読み込み,実行権限を与えること<br> txtは書き込み権限も与えたほうがよい<br> <code>$chmod 705 hogehoge</code></p> <h2>1.cgi(python-処理側)側</h2> communication.cgi<br> <div class=WordSection1 style='layout-grid:18.0pt'> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#6A9955'>#!/usr/bin/python3</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#6A9955'># coding: utf-8</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#C586C0'>import</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> sys</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#C586C0'>import</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> io</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#C586C0'>import</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> urllib.parse</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#C586C0'>from</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> urllib.parse </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>import</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> urlparse</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>sys.stdout = io.TextIOWrapper(sys.stdout.buffer, </span><span lang=EN-US style='font-family:Consolas;color:#9CDCFE'>encoding</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>=</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'utf-8'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>files = </span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"comment.txt"</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"Content-type: text/html; charset=utf-8"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>()</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"<!DOCTYPE html>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"<html>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"<head>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'<meta charset="utf-8">'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'</head>'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"<body>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#569CD6'>def</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print_msg</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#9CDCFE'>name</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>,</span><span lang=EN-US style='font-family:Consolas;color:#9CDCFE'>text</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>):</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> data = {}</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>with</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>open</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(files,</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"a"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>,</span><span lang=EN-US style='font-family:Consolas;color:#9CDCFE'>encoding</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>=</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'utf-8'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>) </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>as</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> w_data:</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> w_data.write(name+</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>':'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>+text+</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'<br></span><span lang=EN-US style='font-family:Consolas;color:#D7BA7D'>\n</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#569CD6'>def</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>documentsDisplay</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>():</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>with</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>open</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(files,</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"r"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>,</span><span lang=EN-US style='font-family:Consolas;color:#9CDCFE'>encoding</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>=</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'utf-8'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>) </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>as</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> r_data:</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#6A9955'>#html_scan = r_data.read()</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#6A9955'>#print(html_scan)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>for</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> text </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>in</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> r_data:</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(text)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>documentsDisplay()</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>data_scan = {}</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'<form action="communication.cgi" method="POST">'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'</span><span style='font-family: "MS Pゴシック";color:#CE9178'>お名前</span><span lang=EN-US style='font-family:Consolas; color:#CE9178'>:<input type="text" name="id" value="</span><span style='font-family:"MS Pゴシック";color:#CE9178'>名無しさん</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>" size="20"/><br>'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'</span><span style='font-family: "MS Pゴシック";color:#CE9178'>コメント</span><span lang=EN-US style='font-family:Consolas; color:#CE9178'>:<br><textarea style="line-hight: 20px" name="message" row="8" cols="50" wrap="hard" placeholder="</span><span style='font-family:"MS Pゴシック";color:#CE9178'>コメントを記入</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"></textarea>'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'<input type="submit" name="submit" value="</span><span style='font-family:"MS Pゴシック";color:#CE9178'>投稿</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"/>'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"</form>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"</body>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"</html>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#6A9955'>#main </span><span style='font-family:"MS Pゴシック";color:#6A9955'>ここで</span><span lang=EN-US style='font-family:Consolas;color:#6A9955'>URL</span><span style='font-family: "MS Pゴシック";color:#6A9955'>を取込、分解している</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>scan = </span><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>input</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>()</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>scan = urllib.parse.unquote(scan) </span><span lang=EN-US style='font-family:Consolas;color:#6A9955'>#URL</span><span style='font-family:"MS Pゴシック";color:#6A9955'>をデコード</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>scan = scan.split(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"&"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>) </span><span lang=EN-US style='font-family:Consolas;color:#6A9955'>#URL</span><span style='font-family:"MS Pゴシック";color:#6A9955'>の</span><span lang=EN-US style='font-family:Consolas;color:#6A9955'>&</span><span style='font-family: "MS Pゴシック";color:#6A9955'>から属性を分ける</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#C586C0'>for</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> scanner </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>in</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> scan:</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> (key,value)=scanner.split(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"="</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> data_scan[key] = value</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>print_msg(data_scan[</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"id"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>],data_scan[</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"message"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>])</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'<meta http-equiv="Refresh" content="0;URL=">'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal><span lang=EN-US> </span></p> </div> <h3>cgiモジュールを使わない処理はこんな感じ</h3> <p>URLをinputしてそれをutf-8でtxtに書き込み、その後urlのモジュールでデコードしているんだ!<br> [[ファイル:DecodeURL image.png|500px|中央]] </p> <h2>2.mainだけでやるとこんな感じだよ</h2> <div class=WordSection1 style='layout-grid:18.0pt'> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#6A9955'>#!/usr/bin/python3</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#6A9955'># coding: utf-8</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#C586C0'>import</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> sys</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#C586C0'>import</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> io</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#C586C0'>import</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> urllib.parse</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#C586C0'>from</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> urllib.parse </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>import</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> urlparse</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>sys.stdout = io.TextIOWrapper(sys.stdout.buffer, </span><span lang=EN-US style='font-family:Consolas;color:#9CDCFE'>encoding</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>=</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'utf-8'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>data_scan = {}</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>files = </span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"comment.txt"</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"Content-type: text/html; charset=utf-8"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>()</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"<!DOCTYPE html>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"<html>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"<head>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'<meta charset="utf-8">'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'</head>'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"<body>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#C586C0'>with</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>open</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(files,</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"r"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>,</span><span lang=EN-US style='font-family:Consolas;color:#9CDCFE'>encoding</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>=</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'utf-8'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>) </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>as</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> r_data:</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#6A9955'>#html_scan = r_data.read()</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#6A9955'>#print(html_scan)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>for</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> text </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>in</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> r_data:</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(text)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'<form action="kadai13.cgi" method="POST">'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'</span><span style='font-family: "MS Pゴシック";color:#CE9178'>お名前</span><span lang=EN-US style='font-family:Consolas; color:#CE9178'>:<input type="text" name="id" value="</span><span style='font-family:"MS Pゴシック";color:#CE9178'>名無しさん</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'></span><span style='font-family: "MS Pゴシック";color:#CE9178'></span><span lang=EN-US style='font-family:Consolas; color:#CE9178'></span><span style='font-family:"MS Pゴシック";color:#CE9178'></span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>" size="20"/><br>'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'</span><span style='font-family: "MS Pゴシック";color:#CE9178'>コメント</span><span lang=EN-US style='font-family:Consolas; color:#CE9178'>:<br><textarea style="line-hight: 20px" name="message" row="8" cols="50" wrap="hard" placeholder="</span><span style='font-family:"MS Pゴシック";color:#CE9178'>コメントを記入</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"></textarea>'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'<input type="submit" name="submit" value="</span><span style='font-family:"MS Pゴシック";color:#CE9178'>投稿</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"/>'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"</form>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"</body>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"</html>"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#6A9955'>#</span><span style='font-family:"MS Pゴシック";color:#6A9955'>ここで</span><span lang=EN-US style='font-family:Consolas;color:#6A9955'>URL</span><span style='font-family: "MS Pゴシック";color:#6A9955'>を取込、分解している</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>scan = </span><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>input</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>()</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>scan = urllib.parse.unquote(scan) </span><span lang=EN-US style='font-family:Consolas;color:#6A9955'>#URL</span><span style='font-family:"MS Pゴシック";color:#6A9955'>をデコード</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>scan = scan.split(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"&"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>) </span><span lang=EN-US style='font-family:Consolas;color:#6A9955'>#URL</span><span style='font-family:"MS Pゴシック";color:#6A9955'>の</span><span lang=EN-US style='font-family:Consolas;color:#6A9955'>&</span><span style='font-family: "MS Pゴシック";color:#6A9955'>から属性を分ける</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#C586C0'>for</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> scanner </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>in</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> scan:</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> (key,value)=scanner.split(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"="</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> data_scan[key] = value</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>data = {}</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#C586C0'>with</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>open</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(files,</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"a"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>,</span><span lang=EN-US style='font-family:Consolas;color:#9CDCFE'>encoding</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>=</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'utf-8'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>) </span><span lang=EN-US style='font-family:Consolas;color:#C586C0'>as</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> w_data:</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> w_data.write(data_scan[</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"id"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>]+</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>':'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>+data_scan[</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>"message"</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>]+</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'<br></span><span lang=EN-US style='font-family:Consolas;color:#D7BA7D'>\n</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'> </span></p> <p class=MsoNormal align=left style='text-align:left;line-height:14.25pt; background:#1E1E1E'><span lang=EN-US style='font-family:Consolas;color:#DCDCAA'>print</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>(</span><span lang=EN-US style='font-family:Consolas;color:#CE9178'>'<meta http-equiv="Refresh" content="0;URL=">'</span><span lang=EN-US style='font-family:Consolas;color:#D4D4D4'>)</span></p> <p class=MsoNormal><span lang=EN-US> </span></p> </div>
Python(cgi)で簡易掲示板の作り方
に戻る。
案内メニュー
個人用ツール
アカウント作成
ログイン
名前空間
ページ
議論
日本語
表示
閲覧
ソースを閲覧
履歴表示
その他
検索
案内
メインページ
最近の更新
おまかせ表示
MediaWikiについてのヘルプ
コード系
Program
PHP
Java
Python
Swift
JavaScript
HTML
CSS
C言語
MySQL
サーバー系
Apache2
OS系
Debian
kail
アプリケーション系
Xcode
Android Studio
計算系
ファイル系
速度系
メモ・業務
My命名規則
ツール
リンク元
関連ページの更新状況
特別ページ
ページ情報
編集
左メニュー
動画挿入について
動画・音声を追加する
JavaScriptを埋め込む
ページにリダイレクトを指定する