前提・実現したいこと
ローカルサーバーを使い、簡単なWebアプリを作る課題
test_server.py:ローカルサーバーの構築
index.html:「お名前はなんですか?」というメッセージと入力フォームの表示
name.py:入力された値を元に、「あなたは〇〇さんです!」と表示
なお、name.pyはcgi-bin.pyというフォルダに入れ、
test_server.py、index.htmlよりも一つ下の階層にある。
といった物を作ろうとしています。
発生している問題・エラーメッセージ
name.pyに飛ぶときに、ブラウザは真っ白で何も表示されず、
以下のエラーがターミナルに表示されます。
127.0.0.1 - - [23/Jan/2019 00:26:43] "GET /index.html HTTP/1.1" 200 - 127.0.0.1 - - [23/Jan/2019 00:28:21] "GET /cgi-bin/name.py?name=%E3%81%8A%E4%BE%BF%E6%89%80%E5%A4%AA%E9%83%8E HTTP/1.1" 200 - ---------------------------------------- Exception happened during processing of request from ('127.0.0.1', 52743) Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/http/server.py", line 1170, in run_cgi os.execve(scriptfile, args, env) FileNotFoundError: [Errno 2] No such file or directory: '/Users/Rin/Desktop/python/cgi-bin/name.py' ---------------------------------------- 127.0.0.1 - - [23/Jan/2019 00:28:21] CGI script exit status 0x7f00
該当のソースコード
test_server.py
python3
1#encoding: utf-8 2 3import http.server 4http.server.test(HandlerClass=http.server.CGIHTTPRequestHandler) 5
index.html
python3
1<!DOCTYPE html> 2<html lang="ja"> 3<head> 4 <meta charset="UTF-8"> 5 <title>初めてのWebアプリケーション!</title> 6 <style type="text/css"> 7 h1{ 8 background:#eee; 9 border: 1px solid #ccc; 10 border-left:7px solid #e3297d; 11 padding:15px 20px; 12 } 13 </style> 14</head> 15<body> 16 <h1>あなたのお名前はなんというのですか?</h1> 17 <form action="cgi-bin/name.py" method="get"> 18 <p>お名前:<input type="text", name="name"/></p> 19 <button type="subit">送信</button> 20 </form> 21</body> 22</html> 23
name.py
python3
1#!usr/bin/env python 2# -*- coding: utf-8 -*- 3 4import cgi 5 6html_body = """ 7<html> 8<head><meta charset="UTF-8"></head> 9<body> 10<p>あなたのお名前は<span style="font-size:48px"> %s </span>さんです!</p> 11</body></html> 12""" 13 14 15form = cgi.FieldStorage() 16 17print("Content-type: text/html") 18print(html_body % form['name'].value) 19
試したこと
name.pyの実行権限は755にしてあります。
「No such file or directory」とありますが、
間違いなく教科書通りにコードを書いてますし、
プログラム通りのファイルの位置関係になっているとも思うのですが・・・
補足情報(FW/ツールのバージョンなど)
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/01/22 16:17