質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

解決済

3回答

1374閲覧

python webアプリで/cgi-bin内にあるpyファイルが正常に動作しない

snowshink

総合スコア138

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2019/06/12 08:57

htmlファイル内のajaxからpythonにpostしてラベル付けしていない画像のパスを取得したいのですが、
ブラウザ側では http://localhost:8000/cgi-bin/web.pyが見つからないと表示され、
コマンドプロンプトではInvalid argmentエラーでhttp://localhost:8000/train.csvが開けません。
\を/にしたり、相対パス(r"..\train.csv")にしても今度はファイルかディレクトリが見つからず、ダメでした。
ocalhost:8000で確認したところtrain.csvは確かにあるのですが、どうしたらいいのでしょうか。

web.html

html

1<html> 2<!DOCTYPE html> 3<head> 4 <meta charset="utf-8"> 5</head> 6<body> 7 <img id="image" src="" width="500px" height="500px"> 8 <div> 9 <input type="button" onclick="select(0)" value="碇シンジ"> 10 <input type="button" onclick="select(1)" value="綾波レイ"> 11 <input type="button" onclick="select(2)" value="アスカ・ラングレー"> 12 <input type="button" onclick="select(3)" value="渚カヲル"> 13 <input type="button" onclick="select(4)" value="カヲシン"> 14 <input type="button" onclick="select(-1)" value="それ以外"> 15 </div> 16 <script src="https://code.jquery.com/jquery-3.2.1.min.js"></script> 17 <script type="text/javascript"> 18 function select(a){ 19 $.ajax({ 20 url: 'cgi-bin/web.py', 21 type: 'post', 22 dataType: 'text', 23 data: { 24 select:a 25 }, 26 }) 27 .done(function(response) { 28 document.getElementById('image').src=response 29 }) 30 .fail(function() { 31 alert("failed"); 32 }); 33 } 34 select(-2) 35 </script> 36</body>

web.py

python3

1#!/usr/bin/env python3 2import os 3import codecs 4import sys 5import cgi 6#cgitb.enable() 7form = cgi.FieldStorage() 8select=form.getfirst("select") 9sys.stdout=codecs.getwriter('utf_8')(sys.stdout.detach()) 10flist= os.listdir(r"../") 11pnglist=[f for f in flist if ".png" in f] 12scanned=[] 13with open(r"http:\localhost:8000\train.csv","r",encoding="utf_8") as file: 14 for line in file: 15 scanned.append(line.split(",").strip()[0]) 16nowindex=0 17for sc in scanned: 18 if pnglist[nowindex]==sc: 19 nowindex+=1 20nowpng=pnglist[nowindex] 21print(pnglist) 22''' 23if select : 24 if select==-2: 25 print(nowpng) 26 else: 27 read=[] 28 with open("train.csv","r",encoding="utf_8") as file: 29 for line in file:read.append(line) 30 with open("train.csv","w",encoding="utf_8") as file: 31 for r in read:file.write(r+"\n") 32 file.write(nowpng+","+str(select)+"\n") 33 nowindex+=1 34 nowpng=pnglist[nowindex] 35 print(nowpng) 36'''

エラー文(コマンドプロンプト)

b'Traceback (most recent call last):\r\n File "C:\Users\itnabi601\Documents\python\scraping\pinterest\evangelion\cgi-bin\web.py", line 13, in <module>\r\n with open(r"http:\localhost:8000\train.csv","r",encoding="utf_8") as file:\r\nOSError: [Errno 22] Invalid argument: 'http:\\localhost:8000\train.csv'\r\n'

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

otn

2019/06/12 14:39

web.html はどこにあるんですか?
snowshink

2019/06/13 00:01 編集

カレントディレクトリにあります http://localhost:8000/に web.html,train.csv,表示したい画像、 そのなかのcgi-bin/にweb.pyがあります。
guest

回答3

0

ファイルの指定はC:/からの絶対パス
pyファイルはhttp://からの絶対パスで指定し、
最初にprint("Content-Type: text/html; charset=UTF-8\n")を送信して直りました。多分最後のが一番の原因だったかと

投稿2019/06/13 07:17

snowshink

総合スコア138

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

ベストアンサー

URLから開こうとするのをやめて、C:\から始まる絶対パスで指定してください。

投稿2019/06/13 00:17

hayataka2049

総合スコア30933

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

パスの指定方法がよく分からない人の場合は、全て絶対パスで指定するのが良いです。

投稿2019/06/12 14:42

otn

総合スコア84555

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問