前提・実現したいこと
ページで作成された配列をcsvへ書き込むためにpythonを使用したいです。
発生している問題・エラーメッセージ
その前にcgiの準備ができているか確認しようと公開されているソースを実行しましたが、
遷移後のページでソースが表示されてしまう。
(追記)ソースが表示されることはなくなったが500 Internal Server Errorが出る。
以下url
https://qiita.com/TSKY/items/b041de0572e6586c889c
該当のソースコード
html
1<!DOCTYPE html> 2<html> 3<head><meta charset="utf-8" /></head> 4<body bgcolor="lightyellow"> 5<h1>入力値の積を表示します</h1> 6 <form action="/cgi-bin/formtest1.py" method="POST"> 7 値1を入力してください 8 <input type="text" name="value1"> 9 値2を入力してください 10 <input type="text" name="value2"> 11 <input type="submit" name="submit" value="送信"> 12 </form> 13</body> 14</html>
python
1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3import cgi 4form = cgi.FieldStorage() # cgiオブジェクト作成 5v1 = form.getfirst('value1') # nameがvalue1の値を取得 6v2 = form.getfirst('value2') # nameがvalue2の値を取得 7#入力値が数字ならその積を返す関数 8def times(a, b): 9 try: 10 a=int(a) 11 b=int(b) 12 return str(a*b) 13 except ValueError: 14 return('数値じゃないので計算できません(>_<)') 15 16# ブラウザに戻すHTMLのデータ 17print("Content-Type: text/html") 18print() 19htmlText = ''' 20<!DOCTYPE html> 21<html> 22 <head><meta charset="shift-jis" /></head> 23<body bgcolor="lightyellow"> 24 <h1>こんにちは</h1> 25 <p>入力値の積は %s<br/></p> 26 <hr/> 27 <a href="../form1.html">戻る</a> 28</body> 29</html> 30'''%(times(v1,v2)) # 入力値の積を%sの箇所に埋める 31print( htmlText.encode("cp932", 'ignore').decode('cp932') )
試したこと
正しく配置はしているのですが、サーバーにアップロードしている記事が少なくどうしていいかわからない状態です。
追記
拡張子を.py→.cgiに変更しました。
ご指摘いただいた
(#!/usr/bin/env python)
↓
(#!/usr/local/bin/python)
に変更しました。
現在
.www
-form1.html
-formtest1.cgi
-.htaccess
が同じディレクトリに保存してあり、パーミッションも755に変更してありますが、
500 Internal Server Error が出ています。
エラーログは以下の通りです。
[Thu Sep 30 12:55:14.374649 2021] [cgi:error] [pid 99658] [client 118.***.185.***:0] AH01215: Traceback (most recent call last):: /home/***/www/formtest1.cgi, referer: https://***.ne.jp/form1.html [Thu Sep 30 12:55:14.374745 2021] [cgi:error] [pid 99658] [client 118.***.185.***:0] AH01215: File "formtest1.cgi", line 31, in <module>: /home/***/www/formtest1.cgi, referer: https://***.ne.jp/form1.html [Thu Sep 30 12:55:14.374799 2021] [cgi:error] [pid 99658] [client 118.***.185.***:0] AH01215: print( htmlText.encode("cp932", 'ignore').decode('cp932') ): /home/***/www/formtest1.cgi, referer: https://***.ne.jp/form1.html [Thu Sep 30 12:55:14.374899 2021] [cgi:error] [pid 99658] [client 118.***.185.***:0] AH01215: UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 107: ordinal not in range(128): /home/***/www/formtest1.cgi, referer: https:/***.ne.jp/form1.html [Thu Sep 30 12:55:14.374924 2021] [cgi:error] [pid 99658] [client 118.***.185.***:0] malformed header from script 'formtest1.cgi': Bad header: (), referer: https://***.ne.jp/form1.html
回答2件
あなたの回答
tips
プレビュー