Php Python应用程序向服务器发送get链接,获得200 OK响应,但服务器脚本没有';不执行
我正在尝试制作一个python应用程序,它将在SQL数据库中插入数据。 为了实现这一点,我让python应用程序在GET请求中发送参数,并编写了一个php脚本来获取参数并发出SQL请求 Python脚本是(缩短的): 执行打印时:Php Python应用程序向服务器发送get链接,获得200 OK响应,但服务器脚本没有';不执行,php,python,mysql,python-2.7,Php,Python,Mysql,Python 2.7,我正在尝试制作一个python应用程序,它将在SQL数据库中插入数据。 为了实现这一点,我让python应用程序在GET请求中发送参数,并编写了一个php脚本来获取参数并发出SQL请求 Python脚本是(缩短的): 执行打印时: drakasan@debian:~$ python Ingesup/Web/AgentS.py ?id_machine=1,type_erreur=ping,description_erreur=test,date_detection=1381245779,date
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>
我正在使用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]
drakasan@debian:~$wget-O-http://localhost/test/erreur.php?id_machine=1,类型为ping,描述为test,日期为1381241491,日期为1381241491
--2013-10-08 16:19:31-- http://localhost/test/erreur.php?id_machine=1,类型为ping,描述为test,日期为1381241491,日期为1381241491
正在解析本地主机(本地主机)1, 127.0.0.1
正在连接到本地主机(本地主机)|:::1 |:80。。。有联系的。
HTTP请求已发送,正在等待响应。。。200行
长度:826[文本/html]
保存到:`STDOUT'
0%[]0---K/s
注意:未定义的索引:第6行/opt/lampp/htdocs/test/erreur.php中的type_machine
注意:第7行/opt/lampp/htdocs/test/erreur.php中未定义的索引:description\u erreur
注意:未定义索引:第8行/opt/lampp/htdocs/test/erreur.php中的日期检测
注意:未定义索引:第9行/opt/lampp/htdocs/test/erreur.php中的日期
注意:第11行/opt/lampp/htdocs/test/erreur.php中未定义的索引:id
注意:未定义变量:第13行/opt/lampp/htdocs/test/erreur.php中的nom_machine
你是我们的冠军吗
100%[================================================================================================================================================================>]826--0秒后为K/s
2013-10-08 16:19:31(69.1MB/s)-写入标准输出[826/826]
如果PHP中存在未定义的变量警告,则需要立即处理。在本例中,您有$nom_machine&&
,因为$nom_machine
没有定义,它基本上等同于if(FALSE&&
。这意味着if
中的代码永远不会运行
在Python方面,您应该读取输出,而不仅仅是标题。(例如,
print res.read()
).PHP通常会在发送标头后显示错误,这意味着您可能会遇到致命错误或带有200
标头的空响应。除此之外,您没有正确遵循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中存储为
$\u POST
)
正如名字所暗示的:GET意味着GET,POST意味着POST
- 最后但并非最不重要的一点是,PHP代码不太可能生成有效的HTML。
使用commandline中的wget
测试URL,以确定PHP脚本是否失败(看起来是这样),显然您已经“缩短”了脚本太多。我不知道您的代码如何打印参数(第9行),然后继续到达最后两行(“输出”中的“200”和“确定”),而不打印完整查询(第12行)介于两者之间。同时查看给出的响应内容(即使用wget或打印分辨率read()
)。我已经有一段时间没有看到PHP了,但从我的阅读来看,我看不出脚本为什么不会返回200 OK
状态,即使SQL部分失败。@DrakaSAN我真的不知道如何从发布的代码中获得发布的输出。你能重新检查吗?关于错误,我的意思是PHP脚本确实重新发布了urn 200可以,但不输出内容,即回送的字符串。要查看这些内容,请使用read()
方法或wget,但要使用-O-
(破折号,大号,空格,破折号),如下所示:wget-O-http://localhost/...-O-
使wget显示内容(应该是一个法语字符串加上一组空HTML标记)就在终端上。URI查询语法不正确。它是符号,而不是逗号,例如:?
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)
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]