2

This is my python file:

host = socket.gethostname()

mac = hex(uuid.getnode())

sign = host + mac
message = input('plz enter message')
userdata = { message, sign}
url = "http://localhost:8081/project/server.php"
resp = requests.post(url,  data=str(userdata))

print(resp.text)

This is my PHP Script:

<?php

if (isset($_GET['userdata'])){
    $userdata = $_GET['userdata'];
    echo $userdata;
 }
else{
     echo "Data not received";
 }
echo $userdata ;
?>

Problem my python file is not sending userdata to php script. Any help would be appreciated Thank you.

2
  • I have tried all possible solution available on internet and also tried alternative of requests urllib and urllib2. Commented Mar 10, 2021 at 5:55
  • im not a python dev, but have you tried sending query strings instead? server.php?userdata=hello-world like so? Commented Mar 10, 2021 at 6:10

2 Answers 2

1

The problem is you are sending post data but in PHP script, you are capturing GET data. We can re-write both files like this to send POST data and capture POST data

Python file

sign = host + mac
message = input('plz enter message')
url = 'http://localhost:8081/project/server.php'
postdata = {'sign': sign , 'msg' : message }

x = requests.post(url, data = postdata)

print(x.text)

PHP file

 <?php

if (isset($_POST['sign'])) {
    $myfile = fopen("data", "a");
    $sign = $_POST['sign'];
    $msg = $_POST['msg'];

    echo $sign . " " . $msg ;
    fwrite($myfile , $sign.','.$msg."\n");
    fclose($myfile);
}
else {
    echo "Data not received <br/>";
}

$myfile = fopen("data", "r");

while(!feof($myfile)) {
    echo fgets($myfile). "<br/>";
}
fclose($myfile);

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

4 Comments

browser is still showing "Data not received"
Are you using a browser or python script to send data ? what is the output of python script ?.
python script is showing what i need it is showing correct output but i want to pass this output in php and want to store it in a text file or database from php script.
I edited the PHP code for save and display the saved data
0

requests.post needs dict object, not str.

So posting data with requests,

data = {'userdata': message}
resp = requests.post(url, data=data)

Edit

data = {'userdata': message}
resp = requests.get(url, params=data)

3 Comments

I didn't notice that php script requires "GET" request. Edited code.
again no success.@pu2x
this what i received in browser . " Data not received"

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.