0

I'm want to post a binary data string from a Python script to a webserver where a PHP script should pick it up and store it. I receive the whatever I echo in my POST part of the php script on the Python side so I assume, the actual POST works. However, I'm posting 23 Bytes and strlen($_POST['data']) stays 0. My PHP script to pickj up the data looks like this:

if (isset($_REQUEST["f"]) && $_REQUEST["f"]=="post_status") {
    $fname = "status/".time().".bin";
    if (file_exists($fname)) {
            $fname = $fname."_".rand();
              if (file_exists($fname)) {
                $fname = $fname."_".rand();
              }
    }
    echo strlen($_POST['data'])." SUCCESS!";

}

and the Python POST script looks like this:

    data = statusstr
    host = HOST
    func = "post_status"
    url = "http://{0}{1}?f={2}".format(host,URI,func)
    print url
    r = urllib2.Request(url, data,{'Content-Type': 'application/octet-stream'})
    r.get_method = lambda: 'PUT'
    response = urllib2.urlopen(r)
    print "RESPONSE " + response.read()

Why does my data not seem to get through, I'm wondering?

Thank you,

1 Answer 1

4

PHP will only populate posted values into the $_POST/REQUEST arrays for data that is sent as one of the form data content types. In your case, you need to read in the binary data directly from standard in like this:

$postdata = file_get_contents("php://input"); 
Sign up to request clarification or add additional context in comments.

1 Comment

Sadly, the file_get_contents does not support the "offset" parameter in this case :/

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.