【发布时间】:2013-10-15 10:15:05
【问题描述】:
我正在尝试制作一个可以在 SQL 数据库中插入数据的 python 应用程序。 为了实现这一点,我让 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 将我的服务器和数据库放在 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]
【问题讨论】:
-
从命令行使用
wget测试 URL 以确定 PHP 脚本是否失败(看起来) -
显然你已经“缩短”了脚本太多了。我看不出您的代码如何打印参数(第 9 行)然后继续到达最后两行(输出中的“200”和“OK”),而不会在两者之间打印完整的查询(第 12 行)。
-
同时查看 content 给出的响应(即使用 wget 或
print res.read())。自从我看到 PHP 已经有一段时间了,但从我的阅读来看,我看不出脚本不会返回200 OK状态的任何原因,即使 SQL 部分失败。 -
@DrakaSAN 我真的不明白如何从您发布的代码中获取您发布的输出。可以重新检查吗?关于错误,我的意思是 php 脚本确实返回了 200 OK,但您没有输出 content,即您
echo的字符串。为了看到这一点,使用read()方法或 wget,但使用-O -(dash, big oh, space, dash) ,像这样:wget -O - http://localhost/...-O -使 wget 显示内容(应该是其中之一您的法语字符串加上一组空的 HTML 标记)就在终端中。 -
您的 URI 查询语法不正确。它是和号,而不是逗号,例如:
?par=value&par1=value2。 (请注意,您需要引用 URI 以将其传递给 wget。)
标签: php python mysql python-2.7