Send PHP data to Python and back

If you want to send PHP data to Python, then you are at the right spot. In this post we will provide you some code, which will allow you to send data from your PHP form to your Python application. The response of your Python application will also be shown in the same PHP page.

You can change the application/script to any type of application you desire. We use Python for this example, but you are certainly not limited to Python only.

Sending PHP data to Python

In order to send PHP data to your Python application, we are going to need, a PHP page which is hosted in your webhosting environment, and we need a Python application, which will receive commands and return back some values.

  • php
  • python script

The PHP page which we are going to use, is made out of the following code:

  • Code which will show a text field
  • Code which will get the data from the text field
  • Code which will send the data from the text field

The PHP code

Copy the PHP code seen below, and host it in your own environment. Change the path of the python application, and continue to the next step which will involve the Python application.

<form action="#" method="post">
Name: <input type="text" name="NAME"><br>
<input type="submit" value="NAME">
    $param1 = $_POST["NAME"];    
    $command = escapeshellcmd("/var/www/html/ $param1 ");
    $resultAsString = shell_exec($command);
    echo $resultAsString;

Python application

This simple Python application, will get the values send from the PHP form, and will start working on the values.

#!/usr/bin/env python
import sys
workvalue = sys.argv[1]
workvalue = workvalue + '\t' + "Hello World, send from"

Copy and save the Python file at your desired location, and make sure to change the Python path in the PHP file to the right location.

Visit your website

Now you can visit the PHP page, and follow the commands, if everything has been performed correctly, you will get your value which has been send via the PHP form, back from your Python application.

Share this information
About Reza Rafati 3136 Articles
Founder of