Python 应用向服务器发送 get 链接,获取 200 OK 响应,但服务器脚本未执行


Python app send a get link to a server, get a 200 OK response, but the server script doesn't execute

我正在尝试制作一个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>

"幸福"数据库如下:

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 将我的服务器和数据库放在本地主机/测试中。 因此,脚本似乎确实收到了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 的基础知识。 这不是一个复杂的协议,它有很大帮助如果你知道你需要看哪里。

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

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

    在 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流"(

  • 同样正如布鲁诺指出的那样,GET不应该用于存储数据,你应该改用 POST(我认为它在 PHP 中存储为 $_POST(。

    顾名思义:GET的意思是得到,POST的意思是发布。

  • 最后但并非最不重要的一点是,您的PHP代码不太可能产生有效的HTML。<? ... >部分被它打印的任何内容(回声(所取代,因此您的实际输出如下所示:

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

    这绝对不是有效的 HTML。

如果在 PHP 中有一个未定义的变量警告,则需要立即处理。在这种情况下,你有 $nom_machine && ,因为没有定义$nom_machine,所以基本上等同于 if(FALSE && 。这意味着该if中的代码将永远不会运行。

在 Python 端,你应该读取输出,而不仅仅是标头。(例如。 print res.read() (。PHP 通常会在发送标头显示错误,这意味着您可能会遇到致命错误或带有 200 标头的空响应。