LoginSignup
2
1

More than 5 years have passed since last update.

エックスサーバーでPython CGIを動かすメモ

Last updated at Posted at 2018-08-20

ここで書いた内容

.htaccess に
AddHandler cgi-script .py
を追加

対象のパーミッションを変更
chmod 705 test.py

コード


#!/usr/bin/python
# -*- coding: utf-8 -*-
print("Content-type: text/html\n")
print("<html><body>Python is awesome !</body></html>")

http://your.domain/dir/test.py
みたいな感じ。

opencvなどの重たいライブラリをimportしたら
internal server error500が発生してしまいます

しかし、コマンドライン上ではライブラリを取り込んでも動きます
このエラー500の回避策として、PHPからPythonを起動させて、
出力結果を取得することでCGI?ではなくPHPで動かせました。

PHP側

test.php

$fullPath = 'python test.py '.$param;
exec($fullPath, $outpara);
echo $outpara;

Python側

test.py

#!/usr/bin/python3.4
# -*- coding: utf-8 -*-

import sys
print("パラメータ1"+sys.argv[1])
print("パラメータ2"+sys.argv[2])
print("パラメータ3"+sys.argv[3])

print([sys.argv[1],sys.argv[2],sys.argv[3]])

問題は、パラメータの長さに限界がありますね。jsonデータで大きいものとか、
画像とかは一旦保存しないと駄目みたい

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1