0

I am trying to send data from my python file to a php file and show it inside the browser.

I've read the following question: Sending data using POST in Python to PHP and I am basing my code on the answer of the question.

My python code thus far is :

import urllib2, urllib

def main():
    mydata = [('one', '1'), ('two', '2')]  # The first is the var name the second is the value
    mydata = urllib.urlencode(mydata)
    path = 'http://localhost/test2.php'  # the url you want to POST to
    req = urllib2.Request(path, mydata)
    req.add_header("Content-type", "application/x-www-form-urlencoded")
    page = urllib2.urlopen(req).read()
    print page


if __name__ == "__main__":
    main()

My php code is:

<meta http-equiv="refresh" content="1" /> <!-- Updates the whole page each second -->
<?php
echo $_POST['one'];
echo "\n";
echo $_POST['two'];
?>

I am updating the page but it never shows 1 and 2 in the browser, it does show the 1 and 2 printed in python. Is there a way how I can update the php file so it shows everything I send to it?

Thank you!

1
  • You have to take a look back again to see how things work. A php file that is used in a http server environment will have its output sent to the requesting client. That is the browser for the refreshed page view and the python script for the requests from that script. Those two different requests are completely independent. There is no mechanism that somehow makes the values used in one script run magically appear in a completely independent script run. You want to use a database for persistent storage of such values most likely. Commented Dec 14, 2016 at 11:49

1 Answer 1

0

Look your are not invoking your connection through python if your are using browser. Browser by default is not sending any data to your php page.

If you are invoking python code through terminal, you will get results there, not in the browser. Because browser is not sending that data.

You cant just use var_dump to get every post data in php

<?php
var_dump($_POST);
?>

If you can elaborate a little may be i can help you.

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

7 Comments

I am making a smart mirror and I have a heartrate variable and a breathingrate variable that gets updated every few ms in my application. I am trying to send this data to a php file that will be shown on the smart mirror screen.
If you want to do this in real time, my bet would be on using raw sockets. You will get a bunch of socket libraries on both platforms.
If you dont want to use sockets, you can poll your server. I do not recommend this though. You can store the values from python in a file or a data base. Make a php script to read data from there and display it wherever you want to.
I am doing this with sockets now, but I have some trouble getting it to work perfectly, do you have any experience with it?
Yes i can help you with it, you can ping me on mail
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.