【问题标题】:Executing Python Scripts with Lighttpd and CGI使用 Lighttpd 和 CGI​​ 执行 Python 脚本
【发布时间】:2013-11-01 03:03:22
【问题描述】:

我在获取 Python 脚本以在 Lighttpd 和 cgi-bin 中执行时遇到问题。我在 stackoverflow(即Lighttpd and cgi python)和其他网站中发现了类似的问题,但没有一个与我的配置完全相关。我可以通过发出“python flash.py”来执行独立的python脚本而没有任何问题。

可能有助于解决这个问题的一个关键点是,在我运行“apt-get update”和“apt-get upgrade”之前一切正常。我尝试过弄乱某些文件的权限,弄乱配置文件,但都没有帮助。

此后,我已将所有内容恢复到运行更新后的状态。这对我来说是一个新领域,我只是没有受过足够的教育,无法找到任何明显的东西。就目前而言,这是我当前的配置。

/etc/lighttpd/lighttpd.conf

server.modules = (
        "mod_access",
        "mod_alias",
        "mod_compress",
        "mod_redirect",
#       "mod_auth",
#       "mod_rewrite",
)

server.document-root        = "/var/www"
server.upload-dirs          = ( "/var/cache/lighttpd/uploads" )
server.errorlog             = "/var/log/lighttpd/error.log"
server.pid-file             = "/var/run/lighttpd.pid"
server.username             = "www-data"
server.groupname            = "www-data"
server.port                 = 80


index-file.names            = ( "index.php", "index.html", "index.lighttpd.html" )
url.access-deny             = ( "~", ".inc" )
static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )

compress.cache-dir          = "/var/cache/lighttpd/compress/"
compress.filetype           = ( "application/javascript", "text/css", "text/html", "text/plain" )

# default listening port for IPv6 falls back to the IPv4 port
include_shell "/usr/share/lighttpd/use-ipv6.pl " + server.port
include_shell "/usr/share/lighttpd/create-mime.assign.pl"
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

#auth.backend = "plain"
#auth.backend.plain.userfile = "/etc/lighttpd/.lighttpdpwd"

#auth.require = ( "/var/www" =>
#(
#.method. => "basic",
#.realm. => "Authentication required",
#.require. => "user=admin"
#)
#)

etc/lighttpd/conf-enabled/10-cgi.conf

# /usr/share/doc/lighttpd/cgi.txt

server.modules += ( "mod_cgi" )

$HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = ( ".py" => "/usr/bin/python" )
}

## Warning this represents a security risk, as it allow to execute any file
## with a .pl/.py even outside of /usr/lib/cgi-bin.
#
#cgi.assign      = (
#       ".pl"  => "/usr/bin/perl",
#       ".py"  => "/usr/bin/python",
#)

/var/www/cgi-bin/flash.py

#Dog Treat Dispenser. Flash Code
import RPIO
import time
import cgi

FLASHER = 22
#ADD CLICKER!

RPIO.setup(FLASHER , RPIO.OUT)  #Set FLASHER pin as OUTPUT

for x in range(0, 5):                   #Flash for 2 seconds

        RPIO.output(FLASHER, True)

        #ADD CLICKER SUBROUTINE
        time.sleep(.500)

        RPIO.output(FLASHER, False)

        #ADD CLICKER SUBROUTINE
        time.sleep(.500)

# reset every channel that has been set up by this program,
# and unexport interrupt gpio interfaces
RPIO.cleanup()

print "Content-Type: text/html"
print "Location: http://10.143.141.164"
print
print "<html><head>"
print "<title>Flash!</title>"
print "</head>"
print "<body>"
print "<h1>Flash!</h1>"
print "</body>"
print "</html>"

在进行了大量研究后,一无所获,我不知所措。您能提供的任何帮助将不胜感激。如果有什么我遗漏的,请告诉我,我会尽我所能把它给你。

谢谢!

【问题讨论】:

    标签: python cgi lighttpd cgi-bin


    【解决方案1】:

    OP 提到将脚本放在 /var/www/cgi-bin/flash.py 中,而在我的系统上(Ubuntu 可信赖),我将脚本放在目录 /usr/lib/cgi-bin/script.py 并通过浏览器使用 URL 访问它们:http://localhost/cgi-bin/script.py

    无论如何,我在尝试使 cgi python 脚本在 lighttpd 中工作时遇到了这个 SO 问题,因此将此信息作为参考 - 否则 @Dolores 的答案就足够了 - 在我的情况下,配置再次是

    $HTTP["remoteip"] =~ "127.0.0.1" {     
      alias.url += ( "/cgi-bin/" => "/usr/lib/cgi-bin/" )
      $HTTP["url"] =~ "^/cgi-bin/" {
        cgi.assign = ( ".sh" => "/bin/sh" )
      }
    }
    

    /etc/lighttpd/conf-enabled/10-cgi.conf , 使用 cmd: lighty-enable-mod cgi 启用 cgi 模块,然后使用 cmd: /etc/init.d/lighttpd force-reload 重新加载 lighttpd

    下面是script.py

    import time
    print("Content-Type: text/html\n\n")  # html markup follows
    
    timeStr = time.strftime("%c") # obtains current time
    
    htmlFormat = """
    <html>
    <Title>The Time Now</Title>
    <body>
    <p>The current Central date and time is:  {timeStr}</p>
    </body>
    </html> """
    
    print(htmlFormat.format(**locals())) # see embedded %s ^ above
    

    【讨论】:

      【解决方案2】:

      这应该可行:

      server.modules += ( "mod_cgi" )
      
      cgi.assign    = ( ".pl"  => "/usr/bin/perl",
                        ".py"  => "/usr/bin/python" )
      

      你运行的是什么版本的python?你升级并安装了python3吗?如果是这样,您还需要安装 python2 并将 /usr/bin/python 更改为 /usr/bin/python2

      您是否在 Lighttpd 的 mimetypes.conf 文件中为 python 设置了 mimetypes?它应该是这样的:

      ".py" => "text/x-python",
      ".pyc" => "application/x-python-code",
      ".pyo" => "application/x-python-code",
      

      您在进行任何更改之前检查过 lighttpd 的错误日志吗?日志存储在/var/log/lighttpd/error.log

      您遇到的具体问题是什么?当您导航到目录时,文件是否会尝试下载?如果没有更多信息,这很难解决。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多