3

在使用 python twisted 生成的 Web 服务器上可以运行 php 页面吗?

查看文档似乎是可能的: http: //twistedmatrix.com/documents/current/api/twisted.web.twcgi.CGIScript.html

我被困在这一点上,我该如何渲染文件?

    class service(resource.Resource):
def getChild(self, name, request):
    self._path = request.prepath[:]
                # Is this an php
    elif request.prepath[0] == 'php':
        return _ShowPHP

    elif (len(self._path) != 1):
        _ServiceError.SetErrorMsg('Invalid path in URL: %s' % self._path)
        return _ServiceError

显示PHP:

class ShowPHP(resource.Resource):

isLeaf = True   # This is a resource end point.
def render(self, request):
  #return "Hello, world! I am located at %r." % (request.prepath,)

  class PythScript(twcgi.FilteredScript):
    filter="/usr/bin/php"
    resource = static.File("php") # Points to the perl website
    resource.processors = {".php": ShowPHP} # Files that end with .php
    resource.indexNames = ['index.php']


############################################################
_ShowPHP = ShowPHP()

但是当将浏览器指向 php 页面时得到这个:请求没有返回字符串

要求:

    <GET /php/index.php HTTP/1.1>

资源:

      <service.ShowPHP instance at 0x2943878>

价值:

没有任何

4

1 回答 1

2

至少在我的机器上,有必要使用php-cgi而不是普通php的可执行文件。不同之处在于php-cgi,解释器确保它发出正确的标题集以成为正确的 cgi 响应:

from twisted.internet import reactor
from twisted.web.server import Site

from twisted.web.twcgi import FilteredScript

class PhpPage(FilteredScript):
    filter = "/usr/bin/php-cgi"
    #                  ^^^^^^^

    # deal with cgi.force_redirect parameter by setting it to nothing.
    # you could change your php.ini, too.
    def runProcess(self, env, request, qargs=[]):
        env['REDIRECT_STATUS'] = ''
        return FilteredScript.runProcess(self, env, request, qargs)

resource = PhpPage('./hello.php')
factory = Site(resource)

reactor.listenTCP(8880, factory)
reactor.run()
于 2013-05-23T01:59:07.727 回答