2
Contributors
2
Replies
9
Views
3 Years
Discussion Span
Last Post by cereal
0

The correct solution would be to serve web.py as fastcgi together with PHP and ASP, this should be managed by a server as Apache or Nginx. But if you want to try the built in server you could do something like this:

#!/usr/bin/env python

import web
from subprocess import check_output

urls = (
    '/', 'index',
    '/reverse', 'reverse'
)

class index:
    def GET(self):
        return "Hello, world!"

class reverse:
    def GET(self):
        x = web.input(arg1='')
        r = check_output(["./reverse.php", "%s" % x.arg1])
        return "Reverse of " + x.arg1 + " is " + r

if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

Then as PHP script:

#!/usr/bin/env php
<?php

    echo strrev($argv[1]);

Make the php script executable:

chmod 755 reverse.php

Otherwise change this line:

r = check_output(["./reverse.php", "%s" % x.arg1])

With:

r = check_output(["/usr/bin/env php reverse.php", "%s" % x.arg1])

And then try the url:

http://localhost/reverse?arg1=hello 

It should output olleh. Note mine is just a test, this is not good for production usage.

Docs:

Edited by cereal

0

Sorry for the update, I did a little error in my previous post, regarding the alternative to call the PHP executable. The correct version is this:

r = check_output(["/usr/bin/env", "php", "./reverse.php", "%s" % x.arg1])

Otherwise it will output an exception, I haven't noticed it before because I forgot to restart the server.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.