Reputation: 1099
Is it possible to configure an Apache webserver to use execute Python in the same way as PHP? Meaning, you could execute python code in a <?python3 ?>
tag just like you can execute PHP in <?php ?>
tags. I am not asking about WSGI or CGI.
Here's an example of what it would look like in Python3:
magic_number.py:
<?python3
import math
number = math.sqrt(42)
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Magic number</title>
</head>
<body>
<h1>The magic number is <?python3 print(number)?></h1>
</body>
</html>
The same code in PHP would look like this:
magicNumber.py:
<?php
$number = sqrt(42);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Magic number</title>
</head>
<body>
<h1>The magic number is <?php echo $number; ?></h1>
</body>
</html>
If this is possible, how could I configure it to work in Apache?
Any help is appreciated!
Upvotes: 1
Views: 155
Reputation: 12043
You could use Castalian, which uses a cgi handler for it, letting you embed python code inside <?cas … ?>
.
Sadly, it is made for use with python2 and I can't find an update that works with python3. If someone knows, please edit or comment.
Another way might be PSP, see this answer.
Upvotes: 1
Reputation: 5788
You can create behavior you want, but I'm not sure it's a perfect way to solve end task you are trying to do.
php
script. It will be your entry point. I think it's Web page itself, which mean that you need to call shell. For ex.<?php
$output = shell_exec("python embedded.py");
?>
<!DOCTYPE html>
<html lang="en">
<head>
....
php
to create file from string. Below complete snippet.<?php
// Create file with Python source
$embedded = <<<EOF
import math
number = math.sqrt(42)
EOF;
// Save file somewhere
file_put_contents("embedded.py", $embedded);
// Execute shell on Server side
$output = shell_exec("python embedded.py");
?>
<!DOCTYPE html>
<html lang="en">
<head>
....
Upvotes: 1