我想製作一個python應用程序,這將插入數據在SQL數據庫中。 要做到這一點我做了Python應用程序在GET請求發送參數,並所著這是爲了讓他們和使SQL請求Python應用程序發送獲取鏈接到服務器,得到200 OK響應,但服務器腳本不執行
Python的腳本(縮短)PHP腳本:
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
和執行時打印:
[email protected]:~$ 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腳本是否正常工作,並且/或者告訴我關於代碼中哪裏出錯的線索?
用的wget -O參數:
[email protected]:~$ 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行)。 –
也看看給出的響應*內容*(即使用wget或'print res.read()')。自從我看了PHP之後已經有一段時間了,但從我看來,即使SQL部分失敗,我也沒有看到腳本無法返回'200 OK'狀態的任何原因。 –