douba6361 2017-11-13 13:38
浏览 47
已采纳

MySQL DB,通过Python-PHP将多个数据发送到服务器

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!

  • 写回答

1条回答 默认 最新

  • dongwei7048 2017-11-13 13:42
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)