Python:将模块导入CGI脚本

时间:2017-08-19 10:09:11

标签: python apache centos cgi

我希望有人能够向我解释为什么会发生这种情况?

我使用Python 2.7在Centos 7上运行httpd

我在/home/user/Path/to/module.py中有一个python模块。让我们说,例如,它打印“Hello World!”

print("Hello World!")

然后我尝试将其导入文件/var/www/cgi-bin/index.py,如下所示:

#!/usr/bin/python
import sys
sys.path.append('/home/user/Path/to/')

print('Content-type: text/html\n\n')
print('<!DOCTYPE html>')
print('<html lang="en">')
print('<head>')
print('</head>')
print('<body>')
print('<h1>')
import module
print('</h1>')
print('</body>')
print('</html>')

如果我从终端运行这个,我得到以下内容:

Content-type: text/html


<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<h1>
Hello World!
</h1>
</body>
</html>

但是,当我卷曲此网址curl http://127.0.0.1/cgi-bin/index.py时,我收到以下消息:

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>500 Internal Server Error</title>
</head><body>
<h1>Internal Server Error</h1>
<p>The server encountered an internal error or
misconfiguration and was unable to complete
your request.</p>
<p>Please contact the server administrator at
root@localhost to inform them of the time this error occurred,
and the actions you performed just before this error.</p>
<p>More information about this error may be available
in the server error log.</p>
</body></html>

Httpd错误日志显示此错误?

[Sat Aug 19 10:38:16.107635 2017] [cgi:error] [pid 4061] [client 127.0.0.1:39608] AH01215: Traceback (most recent call last):
[Sat Aug 19 10:38:16.107684 2017] [cgi:error] [pid 4061] [client 127.0.0.1:39608] AH01215:   File "/var/www/cgi-bin/index.py", line 7, in <module>
[Sat Aug 19 10:38:16.107691 2017] [cgi:error] [pid 4061] [client 127.0.0.1:39608] AH01215:     import module
[Sat Aug 19 10:38:16.107702 2017] [cgi:error] [pid 4061] [client 127.0.0.1:39608] AH01215: ImportError: No module named module
[Sat Aug 19 10:38:16.109117 2017] [cgi:error] [pid 4061] [client 127.0.0.1:39608] End of script output before headers: index.py

/etc/httpd/conf/httpd.conf的内容:

ServerRoot "/etc/httpd"
Listen 80
Include conf.modules.d/*.conf

User apache
Group apache

ServerAdmin root@localhost

<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot "/var/www/html"

<Directory "/var/www">
    AllowOverride None
    Require all granted
</Directory>

<Directory "/var/www/html">
    Options Indexes FollowSymLinks
    AllowOverride None
    Require all granted
</Directory>

<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/error_log"
LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common
    <IfModule logio_module>
        LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>

    CustomLog "logs/access_log" combined
</IfModule>

<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"
</IfModule>

<Directory "/var/www/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>

<IfModule mime_module>
    TypesConfig /etc/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

AddDefaultCharset UTF-8

<IfModule mime_magic_module>
    MIMEMagicFile conf/magic
</IfModule>

EnableSendfile on
IncludeOptional conf.d/*.conf

任何建议都将不胜感激!

更新

尝试通过删除此行来解决此问题:

sys.path.append('/home/user/Path/to/')

而是在/var/www/cgi-bin/中为模块创建一个符号链接,它似乎可以正常工作但我的模块访问文件夹/home/user/Path/to/folder,这会在httpd日志中抛出拒绝访问错误。

更新

使用Django工作了。然而,对我来说,为什么这不能使用Apache?

仍然是个谜

0 个答案:

没有答案