gpt4 book ai didi

php - Python 扭曲渲染 PHP

转载 作者:搜寻专家 更新时间:2023-10-31 21:13:15 25 4
gpt4 key购买 nike

在用 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>

值:

最佳答案

至少在我的机器上,有必要使用 php-cgi 而不是普通的 php 作为可执行文件。不同之处在于,对于 php-cgi,解释器确保它发出正确的 header 集以作为正确的 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()

关于php - Python 扭曲渲染 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14541813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com