使用Lighttpd和CGI执行Python脚本

2024-09-27 23:16:37 发布

您现在位置:Python中文网/ 问答频道 /正文

在Lighttpd和cgi-bin中执行python脚本时遇到问题。我在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启用/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>"

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

谢谢你!


Tags: pymodbinservervarusrconfhtml
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

你到底有什么问题?导航到目录时,文件是否尝试下载?如果没有更多信息,这很难进行故障排除。

OP提到将脚本放在/var/www/cgi-bin/flash.py中,而在我的系统(Ubuntu trusty)中,我将脚本放在/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

相关问题 更多 >

    热门问题