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!

Community
  • 1
  • 1
user3801533
  • 321
  • 1
  • 6
  • 15
  • 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. – arkascha Dec 14 '16 at 11:49

1 Answers1

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.

tango bango
  • 181
  • 2
  • 13
  • 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. – user3801533 Dec 14 '16 at 12:01
  • 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. – tango bango Dec 14 '16 at 12:25
  • 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. – tango bango Dec 14 '16 at 12:27
  • I am doing this with sockets now, but I have some trouble getting it to work perfectly, do you have any experience with it? – user3801533 Dec 14 '16 at 16:03
  • Yes i can help you with it, you can ping me on mail – tango bango Dec 14 '16 at 20:21
  • What is your e-mail address? I can send the data I want to send once, but i'm having trouble to keep the server listening in a while, it won't echo anything I send to it. – user3801533 Dec 14 '16 at 23:10
  • tangobango53@gmail.com – tango bango Dec 15 '16 at 12:41