以下のサイトを参考に、HTMLからアップロードされたfileを受け取り保存するCGIスクリプトをpythonで作成したいと考えております。
http://www.gesource.jp/programming/python/cgi/0115.html
しかしながらいざ実行してみると、HTMLはちゃんと作成できファイル送信フォームも生成できたのですが、送信すると以下のエラーが出てしまいます。
Error response
Error code: 501
Message: Can only POST to CGI scripts.
Error code explanation: 501 - Server does not support this operation.
test15.html
html
1<html> 2<head> 3 <meta http-equiv="Content-Type" content="text/html" charset="UTF-8" /> 4 <title>ファイルをアップロードする</title> 5</head> 6<body> 7<h1>ファイルをアップロードする</h1> 8<p>%s</p> 9<form action="test15.cgi" method="post" enctype="multipart/form-data"> 10 <input type="file" name="file" /> 11<input type="submit" /> 12</form> 13</body> 14</html>
test15.cgi
python
1import cgi 2import os, sys 3 4try: 5 import msvcrt 6 msvcrt.setmode(0, os.O_BINARY) 7 msvcrt.setmode(1, os.O_BINARY) 8except ImportError: 9 pass 10 11result = '' 12form = cgi.FieldStorage() 13if form.has_key('file'): 14 item = form['file'] 15 if item.file: 16 fout = file(os.path.join('/tmp', item.filename), 'wb') 17 while True: 18 chunk = item.file.read(1000000) 19 if not chunk: 20 break 21 fout.write(chunk) 22 fout.close() 23 result = 'アップロードしました。' 24 25print html % result 26print("upload is done") 27 28```---------------------------- 29 30 31当方、CGIについて最近勉強したばかりで筋違いな質問であれば大変申し訳ないです。 32HTMLで送信したファイルがtmpフォルダに保存されるようなCGIを作りたいと考えております。 33何卒、よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/01 03:40 編集
2017/10/01 03:43
2017/10/01 03:48
2017/10/01 04:22
2017/10/01 07:00
2017/10/01 07:34
2017/10/01 08:56
2017/10/01 09:26
2017/10/01 09:31
2017/10/01 09:39