Python:将模块导入CGI脚本

Python:将模块导入CGI脚本,python,apache,centos,cgi,Python,Apache,Centos,Cgi,我希望有人能向我解释为什么会这样 我正在Centos 7上用Python 2.7运行httpd 我在/home/user/Path/to/module.py中有一个python模块。比如说,它会打印“Hello World!” 然后我尝试将其导入文件/var/www/cgi-bin/index.py,如下所示: #!/usr/bin/python import sys sys.path.append('/home/user/Path/to/') print('Content-type: tex

我希望有人能向我解释为什么会这样

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

我在
/home/user/Path/to/module.py
中有一个python模块。比如说,它会打印“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>')
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
如有任何建议,将不胜感激

更新:

试图通过删除此行来绕过此问题:

sys.path.append('/home/user/Path/to/')
而是在
/var/www/cgi-bin/
中创建一个指向模块的符号链接,该符号链接似乎可以工作,但我的模块访问文件夹
/home/user/Path/to/folder
,该文件夹在httpd日志中抛出一个拒绝访问错误

更新:

用Django来代替它。然而,对于我来说,这仍然是一个谜,为什么它在使用Apache时不起作用

<!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>
[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
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/')