php - Python 应用程序向服务器发送 get 链接,得到 200 OK 响应,但服务器脚本不执行

标签 php python mysql python-2.7

我正在尝试制作一个Python应用程序,它将在SQL数据库中插入数据。 为了实现这一点,我让 python 应用程序在 GET 请求中发送参数,并编写了一个 php 脚本来获取它们并发出 SQL 请求

Python 脚本是(缩短的):

import httplib
import time

dd = time.time().__str__()[:-3]
d = time.time().__str__()[:-3]

link = str('?id_machine=1,type_erreur=ping,description_erreur=test,date_detection=' + dd + ',date=' + d)
print link

conn = httplib.HTTPConnection('localhost')

conn.request('GET','/test/erreur.php' + link)
res = conn.getresponse()
print res.status
print res.reason

执行打印时:

drakasan@debian:~$ python Ingesup/Web/AgentS.py 
?id_machine=1,type_erreur=ping,description_erreur=test,date_detection=1381245779,date=1381245779
200
OK

这是 php 脚本:

<?php
    $page ='Ajoutsalle';
    require_once ('connect.php');

    $id_machine=htmlspecialchars(trim($_GET['id_machine']));
    $type_machine=htmlspecialchars(trim($_GET['type_machine']));
    $description_erreur=htmlspecialchars(trim($_GET['description_erreur']));
    $date_detection=htmlspecialchars(trim($_GET['date_detection']));
    $date=htmlspecialchars(trim($_GET['date']));

    if($nom_machine && $id_salle && $ip && $systeme)
    {
        $query = $connect->query("SELECT * FROM erreur WHERE id='".$id."'");
        $rows=$query->rowCount();
        if($rows==1)
        {
            echo" <div id='error'>Ip existe deja </div>";
        } else {
            $req = $connect->prepare('INSERT INTO     erreur(id_machine,type_erreur,description_erreur,date_detection,date) VALUES(:id_machine,:type_erreur,:description_erreur,:date_detection,:date)');
            $req->execute(array(
                'id_machine'         => $id_machine,
                'type_machine'       => $type_machine,
                'description_erreur' => $description_erreur,
                'date_detection'     => $date_detection,
                'date'               => $date,
            ));
        }

    } else echo "vous devez renseigner tous les champs";
?>
<html>
    <form method='GET' action='#'>
    </form>
</html>

“bliss”数据库如下:

erreur (TABLE)
    -id (PRIMARY, AUTO INDENT, INT)
    -id_machine (INT, FOREIGN KEY)
    -type_erreur (VARCHAR[50])
    -description_erreur (VARCHAR[200])
    -date_detection (TIMESTAMP)
    -date (TIMESTAMP)

我正在使用 Xampp 将我的服务器和数据库放在 localhost/test 中。所以看来脚本确实收到了 GET 请求,但没有执行。

问题是,我还是 python 的初学者,而 php 完全是新手,所以我不知道在代码中去哪里搜索。

最终目标是:

agent.py --GET--> erreur.php --SQL--> bliss.erreur

由于会有很多代理,因此从 Python 脚本发送 SQL 请求并不是解决方案。

任何人都可以验证 Python 脚本是否有效,和/或给我关于代码中哪里出错的线索吗?

带 -O ​​标志的 wget:

drakasan@debian:~$ wget -O - http://localhost/test/erreur.php?id_machine=1,type_erreur=ping,description_erreur=test,date_detection=1381241491,date=1381241491
--2013-10-08 16:19:31--  http://localhost/test/erreur.php?id_machine=1,type_erreur=ping,description_erreur=test,date_detection=1381241491,date=1381241491
Resolving localhost (localhost)... ::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 826 [text/html]
Saving to: `STDOUT'

 0% [                                                                                                             ] 0           --.-K/s              <br />
<b>Notice</b>:  Undefined index: type_machine in <b>/opt/lampp/htdocs/test/erreur.php</b> on line <b>6</b><br />
<br />
<b>Notice</b>:  Undefined index: description_erreur in <b>/opt/lampp/htdocs/test/erreur.php</b> on line <b>7</b><br />
<br />
<b>Notice</b>:  Undefined index: date_detection in <b>/opt/lampp/htdocs/test/erreur.php</b> on line <b>8</b><br />
<br />
<b>Notice</b>:  Undefined index: date in <b>/opt/lampp/htdocs/test/erreur.php</b> on line <b>9</b><br />
<br />
<b>Notice</b>:  Undefined index: id in <b>/opt/lampp/htdocs/test/erreur.php</b> on line <b>11</b><br />
<b>Notice</b>:  Undefined variable: nom_machine in <b>/opt/lampp/htdocs/test/erreur.php</b> on line <b>13</b><br />
vous devez renseigner tous les champs
<html>
    <form method='GET' action='#'>
    </form>
</html>
100%[============================================================================================================>] 826         --.-K/s   in 0s      

2013-10-08 16:19:31 (69.1 MB/s) - written to stdout [826/826]

最佳答案

除此之外,您还没有正确遵循 HTTP URL 语法。一个例子 说明了一切:

http://my.host/some/path?foo=1&bar=2

要点是参数除以 & ,不是, .

<小时/>

其他提示:

  • 阅读 HTTP 基础知识。这不是一个复杂的协议(protocol),而且有很大帮助 如果您知道需要查看哪里。

    (提示:分为三部分:状态行、标题和正文)。

  • 调试时,也始终检查响应的正文,而不仅仅是 响应状态。

    在 Python 中,您可以通过打印 response.read() 的输出来完成此操作方法。 其他选项是使用命令行工具,例如 wget 或 curl 以及适当的 开关:

    $ wget -O - 'http://my.host/some/path?foo=1&bar=2'
    ...
    $ curl -v 'http://my.host/some/path?foo=1&bar=2'
    ... 
    $
    
  • 也许更好的选择是使用像 Wireshark 这样的数据包嗅探器, 您可以在其中看到整个请求和响应。这也是一个很好的习惯 如果您想将方案纳入您的血液系统。

    (提示:在 Wireshark 中,右键单击数据包并选择“跟随 TCP 流”)

  • 正如 bruno 指出的那样,GET 不应该用于存储数据,您应该 使用 POST (我认为它在 PHP 中存储为 $_POST)。

    顾名思义:GET 表示获取,POST 表示发布。

  • 最后但并非最不重要的一点是,您的 PHP 代码不太可能生成有效的 HTML。 <? ... >部分被它打印的任何东西( echo )取代,所以你的 实际输出如下:

    vous devez renseigner tous les champs
    <html>
        <form method='GET' action='#'>
        </form>
    </html>
    

    这绝对不是有效的 HTML。

关于php - Python 应用程序向服务器发送 get 链接,得到 200 OK 响应,但服务器脚本不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19248568/

相关文章:

php - 是否可以使用 Behat/Mink 从 HTML 表中获取值

php - 如果 div 里面的 div 得到了类,则为按钮添加特定的样式

python - 如何围绕 pygame 屏幕中的点平移和旋转坐标轴?

php - Node 使用公共(public)证书加密

php - HTTP header "Content-type: multipart/mixed"导致 "400 Bad request"

python - 在 Python 中按可重复键对 JSON 字典数组进行分组和排序

python - 如何在 python 中使用 cv2 中的 hough 圆?

php - 无法使 PHP 脚本运行,它显示为空白页

php - ng-repeat 在 angularjs 中的 ul 内部不起作用

mysql - "is not null"与 bool 值 MySQL - 性能