我想從我的python文件發送數據到一個php文件並在瀏覽器中顯示它。從python發送數據到php文件
我讀過以下問題:Sending data using POST in Python to PHP和我基於我的代碼問題的答案。
我的Python代碼到目前爲止是:
import urllib2, urllib
def main():
mydata = [('one', '1'), ('two', '2')] # The first is the var name the second is the value
mydata = urllib.urlencode(mydata)
path = 'http://localhost/test2.php' # the url you want to POST to
req = urllib2.Request(path, mydata)
req.add_header("Content-type", "application/x-www-form-urlencoded")
page = urllib2.urlopen(req).read()
print page
if __name__ == "__main__":
main()
我的PHP代碼:
<meta http-equiv="refresh" content="1" /> <!-- Updates the whole page each second -->
<?php
echo $_POST['one'];
echo "\n";
echo $_POST['two'];
?>
我更新了網頁,但它永遠不會顯示在1至2中的瀏覽器,它確實表明了1和2用python打印。有沒有辦法如何更新php文件,以便顯示我發送給它的所有內容?
謝謝!
你必須再回頭看看事情是如何工作的。在http服務器環境中使用的php文件將其輸出發送到請求客戶端。這是用於刷新頁面視圖的瀏覽器,以及來自該腳本的請求的python腳本。這兩個不同的請求是完全獨立的。沒有任何機制可以使某個腳本中使用的值奇蹟般地出現在完全獨立的腳本運行中。您希望使用數據庫來最可能地持久存儲此類值。 – arkascha