我想从我的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