Friday, July 31, 2015

Python Eventlet - Examples

Python Eventlets - Snippets

Python Subprocess - Run Command in Background

Python : Read JSON

RabbitMQ and AMQP

Learn RabbitMQ

CURL / File Upload / Update Database / PHP

 curl -i -F userfile=@orders1.png http://www.some-domain.in/giridharmb/upload.php
 

upload.php

<?php

    $uploaddir = '/home/user123/public_html/giridharmb/uploads/';

    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
    

    $operation_successful = array("upload" => "succcessful");
    $operation_not_successful = array("upload" => "failed");

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
    {
        echo json_encode($operation_successful);
    }
    else
    {
       echo json_encode($operation_not_successful);
    }
?>

Fetch Data from DB

curl -X GET "http://some-domain.in/giridharmb/fetch_mysql_json.php”

[{"varname":"dal4.con1.cpuload","varvalue":"55"},{"varname":"dal4.con2.cpuload","varvalue":"55"},{"varname":"dal4.con555.cpuload","varvalue":"55"},{"varname":"dal5","varvalue":"59"},{"varname":"dal5.con555.cpuload","varvalue":"55”}]

Push Data into DB with values passed via CURL

curl -X GET "http://some-domain.in/giridharmb/push_into_mysql.php?variable=dal5.con555.cpuload&value=55”

Output:

{"operation":"successful”}

push_into_mysql.php


<?php

    $varname = $_GET["variable"];
    $value = $_GET["value"];

    $my_return_array = array();

    if( empty($varname) == TRUE )
    {
        $my_return_array = array("operation" => "failed");
        echo json_encode($my_return_array);
        return;
    }

    if( empty($value) == TRUE )
    {
        $my_return_array = array("operation" => "failed");
        echo json_encode($my_return_array);
        return;
    }

    $servername = "localhost";
    $username = "dbuser123";
    $password = "dbpassword123";
    $dbname = "mydb1";

    // Create connection
    mysql_connect($servername, $username, $password ) or die(mysql_error());

    mysql_select_db($dbname) or die(mysql_error());


    $my_query = "INSERT INTO keyvalue (varname,varvalue) VALUES('" . $varname . "','" . $value . "') ON DUPLICATE KEY UPDATE varvalue='" . $value . "'";

    $retval = mysql_query( $my_query );


    if(! $retval )
    {
        $my_return_array = array("operation" => "failed");
    }
    else
    {
        $my_return_array = array("operation" => "successful");
    }
    
    mysql_close($conn);

    echo json_encode($my_return_array);
    
?>  

File Upload

curl -i -F userfile=@ceph.osd.tree.json http://server1.company.com/fups/up.php

Output


HTTP/1.1 100 Continue

HTTP/1.1 200 OK
Date: Tue, 07 Apr 2015 09:21:02 GMT
Server: Apache/2.4.7 (Ubuntu)
X-Powered-By: PHP/5.5.9-1ubuntu4.3
Vary: Accept-Encoding
Content-Length: 335
Content-Type: text/html

<p>File is valid, and was successfully uploaded.
</p><pre>Here is some more debugging info:Array
(
[userfile] => Array
(
[name] => ceph.osd.tree.json
[type] => application/octet-stream
[tmp_name] => /tmp/phpbNXqb8
[error] => 0
[size] => 26145
)

)

up.php


<?php
$uploaddir = '/var/www/html/fups/data/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo "<p>";

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile))
{
echo "File is valid, and was successfully uploaded.\n";
}
else
{
echo "Upload failed";
}

echo "</p>";
echo '<pre>';
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
?>