简体   繁体   中英

Problem with sending data from Python to PHP using POST

I am trying to send data from python to my localhost on rasberry pi but localhost doesn't want to display it. The server responds in python shell but it seems that php doesn't see the data.

python:

import requests
import time
        
while True:
    
    test = {"1": "4444", "1": "3333"}
    responde = requests.post('http://localhost/index.php', headers={'Content-Type': 'application/x-www-forum-urlencoded'}, params = test)
    print(responde.url)
    print(responde.text)    
    time.sleep(10)

php(localhost):

While (True){       
    if (isset($_POST['1'])){
        $data = $_POST['1'];
        echo $data;
    }
    else{
        echo "no_data";
        exit;
    }
}

Infinite loop in php code will hang your webserver. Please try removing while(true). Also due to this response is not returning to python code.

Also if requests are coming from different domain then you need to consider CORS as well.

I have romoved the loop and exit at the end of php code. I think that the problem is with tagging the data that commes to the localhost.

python - no changes

php:

  if (isset($_POST['1'])){
        $data = $_POST['1'];
        echo $data;
    }
    else{
        echo "no_data";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM