使用POST从PHP向树莓派的Python脚本发送数据 [英] Sending data using POST from PHP to raspberry's Python script

查看:77
本文介绍了使用POST从PHP向树莓派的Python脚本发送数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近找到了一种使用请求发布将数据从python文件发送到php文件的方法,现在我希望能够始终使用与php文件相同的方法将数据发送到python文件,并且知道我的文件php位于服务器中,而python文件位于连接到3g Internet的树莓中,但是我对该域名了解不多,但我还是一个新手,我向您展示我的将python文件发送到PHP文件的代码:

i recently found the way to send data from a python file to a php file using request post and now i would like to be able to send data always with the same method of a php file to a python file knowing that my file php is in a server and the python file is in a raspberry that is connected to the 3g Internet, but I do not know too much about this domain i am still new i show you my code to send a python file to a PHP file :

树莓python客户端:

raspberry python client :

import requests
import time
i =1
j= 0
while i==1 :
   j = j+1
   userdata = {"id": j ,  "firstname": "jo", "lastname": "rasp",
               "password": "666"}
   resp = requests.post('http://jawad.meswatts.fr/sous/api.php',
                          data = userdata)
   print(resp.text)
   time.sleep(3)

php服务器脚本:

<?php
try
    {
      $bdd = new PDO('mysql:host=localhost;dbname=jawad;charset=utf8',
                     'jawad', 'knlnklml54vn,6');
    }
   catch(Exception $e)
   {
     die('Erreur : '.$e->getMessage());
   }


      $id = htmlspecialchars($_POST["id"]);
      $firstname = htmlspecialchars($_POST["firstname"]);
      $lastname = htmlspecialchars($_POST["lastname"]);
      $password = htmlspecialchars($_POST["password"]);


      $req = $bdd->prepare( '                            
                            INSERT INTO reception(nom,prenom,identifiant,id)
                            VALUES (:lastname,:firstname,:password,:id);
                            ' );

    $req->execute(array('lastname' => $lastname, 'firstname' => 
                       $firstname,'password' => $password,'id' => $id)); 
  ?>

所以这是我现在的两个程序,它们现在运行良好,我想换一种方式,我非常了解PHP,但是我网络不是很强大,能帮我吗?

So here are my two programs that work very well now I would like to do that in the other way I know PHP very well but I'm not very very strong network can you help me please ?

推荐答案

因此,当前计划的问题是您必须能够将张贴到.

So the problem with your current plan is that you must be able to POST to somewhere.

在将pi发布到服务器的情况下,我们将发布到 http://jawad.meswatts.fr/sous/api.php

In the case of the pi posting to the server, we are posting to http://jawad.meswatts.fr/sous/api.php

但是如果服务器想发布到pi.它会发布到哪里?pi在任何域名或公共IP上均不公开.(可以通过这种方式进行设置,但我不建议初学者使用.)

But if the server wanted to post to the pi. Where would it post to? The pi isn't publicly available at any domain name or public IP. (Its possible to set it up this way, but I wouldn't recommend it for beginners).

相反,我将从其他所有人过去如何解决此问题开始.让pi向服务器上的另一个位置发出GET请求.该函数将返回本应发布的信息.这样,服务器就永远不会尝试找到pi.它只是等到被告知存储某些信息(POST)或被问到某些信息是什么(GET)为止.

Instead I would start with how everyone else has solved this in the past. Have the pi make a GET request to another location on the server. This function would return the information that would have been posted. In this way, the server never tries to find the pi. It just waits until it is told to store some information (POST) or until it is asked what some information is (GET).

这篇关于使用POST从PHP向树莓派的Python脚本发送数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆