2013-10-08 71 views
0

我想製作一個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] 
+1

測試與命令行了'wget'的URL,以確定是否是PHP腳本失敗(如看起來是) –

+0

顯然,你有「縮短」的劇本太多。我沒有看到代碼如何打印參數(第9行),然後繼續到達最後兩行(外部爲「200」和「OK」),而不打印完整查詢(第12行)。 –

+2

也看看給出的響應*內容*(即使用wget或'print res.read()')。自從我看了PHP之後已經有一段時間了,但從我看來,即使SQL部分失敗,我也沒有看到腳本無法返回'200 OK'狀態的任何原因。 –

回答

1

除了其他的事情,你沒有正確地遵循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流」)

  • 而且布魯諾指出,弄不應該被用於存儲數據,你應該 使用POST(我認爲它存儲爲PHP中的$_POST)。

    正如名稱所示:GET表示獲得,POST表示發佈。

  • 最後但並非最不重要的是,您的PHP代碼不太可能產生有效的HTML。 的<? ... >部分由任何它打印(回聲)所取代,所以您 實際輸出會是這樣:

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

    這絕對不是一個有效的HTML。

0

如果你有在PHP中未定義的變量警告,你需要立即照顧那個。在這種情況下,您有$nom_machine &&,這是因爲$nom_machine沒有定義,基本上相當於if(FALSE &&。這意味着if裏面的代碼永遠不會運行。

在Python方面,您應該閱讀輸出而不僅僅是標題。 (例如,print res.read())。在之後,PHP會經常在之後顯示錯誤,這意味着您可能會有一個致命錯誤或空的響應,並帶有一個200標題。

相關問題