0

I have a Virtual Machine/Web Server with MySQL up and running, and want to receive the data from client hardware that is saved in a .csv file, how can i do a python script to send it? i was recommended requests library from Python to send it and receive it through PHP POST. Problem is ive NEVER used PHP and HTML and so im a bit stuck with the interaction between the 2. Thanks a lot!

sample code is:

import csv
import requests

session = requests.Session()
with open('r01a2.csv','rb') as f:
    reader = csv.reader(f)
    for row in reader:
        r = session.post('MyURL', data = {'date: row[0],'value': row[1]})
        print r.text

Code on Server side is:

    <?php
$formato = "INSERT INTO `datos_pruebas`.`Datos_Edison` (`id`, `date`, `pulse`) VALUES (NULL, '%s', '%f')";
$sql=sprintf($formato,$date,$pulse);
$enlace = mysql_connect('localhost','root','clinichub');
$resultado = mysql_query($sql,$enlace);
$id=NULL;
if(!empty($_POST))
{
        echo htmlspecialchars($_POST["date"]);
} else { 
echo "gg";
}

EDIT2: It finally worked after solutions gave by community and feedback, thanks a lot for your recommendations!

2
  • Shouldn't you do $_POST["data"]? Commented Nov 13, 2017 at 13:43
  • I did that but didnt change answer :/ Commented Nov 13, 2017 at 13:59

1 Answer 1

1

Make sure the post in python uses the correct content-type and verify on the serverside php if $_FILES has an entry. (i.e. print_r($_FILES) ) should return your file, you won't find it in $_POST.

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

12 Comments

Sorry, im a total newbie regarding PHP, can you edit with an example code?
<?php print_r($_FILES) ?> to start with, it should output the uploaded file metainfo. If you wish to use the contents of the file, the temporary file will be in $_FILES[0]['tmp_name'].
But im currently not sending the file just now, or maybe i should send it right away? thing is im not even receiving the 'name'
Answer from server after adding that line is: HI!Array()
Shouldn't the python code be more akin to data= { 'q': '[python]' } r = requests.get('stackoverflow.com', data=data)
|

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.