0

I'm trying to send data from the client to the server. The client runs a simple python script that uses the 'request' library. The server side consists of another simple php script using the $_POST.

I need the webpage to update depending on the data that is given through the client program.

Here is the python script:

import requests url = "http://xxxxxxx.com/php_files/text_data.php" d = {'test': 'It works!'} r = requests.post(url, data = d) print r.status_code, r.reason print r.text 

And here is the php code:

<!DOCTYPE = html> <html> <head> <h1> <?php $txt = $_POST['test']; echo $txt; ?> </h1> </head> </html> 

I need the php page to display 'It works!' on h1 as this is the value that is being passed. But for some reason, it does not display anything

r.text prints the required format with 'It works!' in the < h1 > tags, but the same does not get displayed in the actual website.

I've also tried var_dump($txt). It gives me a NULL value.

Any help would be gladly appreciated.

3
  • how are you posting it to the website when you visit the page? Commented Jun 15, 2015 at 19:43
  • typically you store things you recieve somehow (in a database or a file of some sort ... ) that way you can retrieve the data later to display as you like ... Commented Jun 15, 2015 at 19:45
  • I am running the python script first, and expecting the data to be posted to the webpage. My aim is to transfer the data from the python script to the web server. .... So according to you I should store $txt in a text file or MySql database in the php script? Commented Jun 16, 2015 at 11:15

1 Answer 1

1

It seems to me that you are asking a separate instance to update your current instance. The PHP that you are accessing in your browser knows nothing about the python script. It doesn't call the python script at all. In the second session the python script calls the PHP and receives the correct response.

These are two different sessions, the browser window will see nothing from the python script unless it calls it.

Here is what is happening:

Session 1

  1. Run Python script on local machine
  2. Python calls PHP on server
  3. PHP returns output to local machine
  4. Python prints result

Session 2

  1. Open web browser on local machine
  2. Web browser calls PHP on server
  3. PHP returns results to web browser
  4. Web browser displays results

There is no persistence in the first session to save the information for the second session. They are two completely separate actions. A more typical way would be to set up a database (or just quick and dirty a text file) on the server to save the information. You need to create a second PHP file to save the information to a database or text file on the server. You then need to modify your previous PHP file to read the information from the database or the text file. The sessions would then be set up the following way.

Session 1

  1. Run Python script on local machine
  2. Python calls PHP (new file) on server
  3. PHP writes information from python script to database (or text file)
  4. PHP returns status message to local machine
  5. Python prints status

Session 2

  1. Open web browser on local machine
  2. Web browser calls PHP (original file) on server
  3. PHP reads desired information from database (or text file)
  4. PHP displays information read from server on web browser
  5. Web browser displays results

If you really want to use the results from the python script in the PHP without a database or text file, you will need to upload the python script to your server, and use one of the methods suggested in Calling Python in PHP

Sign up to request clarification or add additional context in comments.

4 Comments

What I'm trying to do is to run the python script on my computer first. I expected it to send a request to the php and then the server executing the php to display the data I sent to the webpage. I'm not too sure if this is the right way to go about doing this. Any other suggestions would be helpful.
No, you have no persistence, please see my updated answer as to how you should attack the problem.
It seems as though you are missing some fundamental concepts about how the internet and webpages in particular function... there are many many ways to create persistence. (assuming you actually want persistence... are you trying to create some sort of socket server that broadcasts to every one? )
Yeah i guess persistence is what i need. I'm kinda new to this, so I guess my concepts were wayward. Thanks a lot for your posts . It really helped a lot. I have a fair idea on how to go about doing this now. Thank you :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.