Python twisted PHP render
Is it possible to run php pages on a webserver generated with python twisted?
A look at the documentation seems possible: http://twistedmatrix.com/documents/current/api/twisted.web.twcgi.CGIScript.html
I'm stuck at this step, how can I render the files?
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
ShowPHP:
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()
but when the browser point to the php page came out: Request did not return string
Request:
<GET /php/index.php HTTP/1.1>
Resource:
<service.ShowPHP instance at 0x2943878>
Value:
None
+3
source to share
1 answer
on my machine at least needs to be used php-cgi
and not just php
as an executable. the difference is that when the php-cgi
interpreter ensures that it fixes the correct set of headers as the correct cgi response:
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()
+2
source to share