前提・実現したいこと
Xserverにてpython3+bottle+cgiのwebアプリケーションを動かしたい
現在進行形で調査中ですが、ここも見てみるといいとか、明らかにここがおかしい等ありましたら、助言をいただきたいです。
発生している問題・エラーメッセージ
ブラウザでアクセスすると、500エラー、Xserverのログは以下の通り
log
1[Tue May 05 15:54:11.878307 2020] [cgid:error] [pid 91295:tid 140227771401984] [client 122.133.103.31:39524] End of script output before headers: main.cgi
そこでindex.cgiを直接実行した結果、以下のエラーが出力される
[xs186537@sv10203 rexia]$ /usr/bin/python3.6 index.cgi Traceback (most recent call last): File "/home/xs186537/xs186537.xsrv.jp/public_html/rexia/bottle.py", line 864, in _handle route, args = self.router.match(environ) File "/home/xs186537/xs186537.xsrv.jp/public_html/rexia/bottle.py", line 419, in match verb = environ['REQUEST_METHOD'].upper() KeyError: 'REQUEST_METHOD' Status: 500 INTERNAL SERVER ERROR Content-Type: text/html; charset=UTF-8 Content-Length: 50 <h1>Critical error while processing request: </h1><h1>Critical error while processing request: </h1>
該当のソースコード
index.cgi
cgi
1#!/usr/bin/python3.6 2from main import app 3app.run(server='cgi')
main.py
python3
1#!~/usr/bin/python3.6 2import bottle 3import smtplib 4from email.mime.text import MIMEText 5import datetime 6 7app = bottle.Bottle() 8 9bottle.TEMPLATE_PATH.append('./view') 10 11@app.route('/static/<filepath:path>') 12def static(filepath): 13 return bottle.static_file(filepath, root='static') 14 15@app.route('/') 16def index(): 17 return bottle.jinja2_template('index.tpl', current_page='philosophy') 18
index.cgi, main.py, bottle.pyはpublic_html/rexia/に配置
ファイルの実行権限等は以下の通り
[xs186537@sv10203 rexia]$ ls -al 合計 244 drwxr-xr-x 7 xs186537 members 287 5月 5 16:27 . drwx--x--x 3 xs186537 members 119 5月 4 16:33 .. -rwx---r-x 1 xs186537 members 93 5月 5 13:57 .htaccess -rw-r--r-- 1 xs186537 members 150580 5月 5 13:46 bottle.py -rwxr-xr-x 1 xs186537 members 64 5月 5 16:14 index.cgi -rw------- 1 xs186537 members 59 5月 5 16:27 main.py drwxr-xr-x 4 xs186537 members 64 5月 3 12:31 static drwxr-xr-x 2 xs186537 members 171 5月 3 12:31 view
試したこと
bottleのドキュメントを確認
GOOD OLD CGI
A CGI server starts a new process for each request. This adds a lot of overhead but is sometimes the only option, especially on cheap hosting packages. The cgi server adapter does not actually start a CGI server, but transforms your bottle application into a valid CGI application:
bottle.run(server='cgi')
参照: https://bottlepy.org/docs/dev/deployment.html
cgiでの動かし方は間違ってなさそうだし、使用条件や制約等もなさそうである
main.pyの中身を削る
python
1#!~/usr/bin/python3.6 2import bottle 3 4app = bottle.Bottle()
ブラウザのエラー、ログ、index.cgiの実行結果ともに変わらず
bottleのインスタンス生成で躓いていそう?
environ['REQUEST_METHOD']を正しく渡す(挫折)
environ['REQUEST_METHOD']がどこでセットされるかを確認し、正しく値を渡すように修正
index.cgiを直接実行したときに出力された、bottle.pyのline419, 864がどこから呼ばれているかを辿った結果、bottle.py line983に辿り着いた
Python
1 def __call__(self, environ, start_response): 2 ''' Each instance of :class:'Bottle' is a WSGI application. ''' 3 return self.wsgi(environ, start_response)
__call__に渡されるenvirionにREQUEST_METHODのキーがないと見てよさそうだ
だが、__call__がどこでどのように呼ばれているのか、よく分からなかった
main.pyの中身を書き換えてみる
python
1#!~/usr/bin/python3.6 2import bottle 3 4# 省略 5 6@app.route('/')
index()を削ってみる
すると、ブラウザのエラーが404エラーに変わり、「Not found: '/'」と出力される
もう少し書き換えてみる
index()の返り値を変更
python
1#!~/usr/bin/python3.6 2 3# 省略 4 5@app.route('/') 6def index(): 7 # return bottle.jinja2_template('index.tpl', current_page='philosophy') 8 return 'OK
ブラウザに「OK」と出力される
index.cgiを直接実行した際、bottleのインスタンス生成で躓いてると推測したが、
ブラウザからアクセスする際は、そこはどうもうまく動いている?
bottle.jinja2_template()が動いていないと思われる
bottle.pyはそのファイルひとつだけを配置しており、bottle.pyでimportしているjinja2のモジュールがない
pip3でモジュールをインストール
jinja2を入れるにあたり、現在、pubulic_html/rexia/にbottle.pyを直接置いているが、pipで管理するように変更
Xserverに元々入っていたpip3でbottleをインストールし、pubulic_html/rexia/bottle.pyを削除
[xs186537@sv10203 rexia]$ pip3 install bottle Collecting bottle Using cached bottle-0.12.18-py3-none-any.whl (89 kB) Installing collected packages: bottle Successfully installed bottle-0.12.18 [xs186537@sv10203 rexia]$ python3.6 index.cgi Traceback (most recent call last): File "index.cgi", line 2, in <module> from main import app File "/home/xs186537/xs186537.xsrv.jp/public_html/rexia/main.py", line 2, in <module> import bottle ModuleNotFoundError: No module named 'bottle' [xs186537@sv10203 rexia]$ pip3 freeze bottle==0.12.18
pip3で入れたものが、/usr/bin/python3.6、index.cgiで参照できない???
補足情報(FW/ツールのバージョンなど)
サーバー: Xserver
OS: Linux
言語: Python3.6
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/05/10 07:11
2020/05/10 07:40