Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过python接收HTTP POST响应_Php_Python_Html_Http_Post - Fatal编程技术网

Php 通过python接收HTTP POST响应

Php 通过python接收HTTP POST响应,php,python,html,http,post,Php,Python,Html,Http,Post,我使用以下示例: 当我从浏览器运行它时, 我在浏览器中看到结果: Welcome John Your email address is john.doe@example.com 运行python POST http请求时: import httplib, urllib params = urllib.urlencode({'@name': 'John','@email': 'John.doe@example.com'}) headers = {"Content-type": "applica

我使用以下示例:

当我从浏览器运行它时, 我在浏览器中看到结果:

Welcome John
Your email address is john.doe@example.com
运行python POST http请求时:

import httplib, urllib
params = urllib.urlencode({'@name': 'John','@email': 'John.doe@example.com'})
headers = {"Content-type": "application/x-www-form-urlencoded","Accept": "text/html"}
conn = httplib.HTTPConnection("10.0.0.201")
conn.request("POST","/welcome.php",params, headers)
response = conn.getresponse()
print "Status"
print response.status
print "Reason"
print response.reason
print "Read"
print response.read()
conn.close()
我看到以下情况:

Status
200
Reason
OK
Read
<html>
<body>

Welcome <br>
Your email address is: 
</body>
</html>
状态
200
理由
好啊
阅读
欢迎
您的电子邮件地址是:
问题是:
如何在python中接收POST请求数据?

您使用了错误的表单名称和错误的HTTP方法。开头没有
@
字符:

params = urllib.urlencode({'name': 'John','email': 'John.doe@example.com'})
接下来,您指向的表单使用GET而不是POST作为处理方法,因此您必须将这些参数添加到URL中:

conn.request("GET", "/welcome.php?" + params, '', headers)
您试图手动驱动
HTTPConnection()
,这是在伤害自己。例如,您可以使用:

from urllib2 import urlopen
from urllib import urlencode

params = urlencode({'name': 'John','email': 'John.doe@example.com'})
response = urlopen('http://10.0.0.201/welcome.php?' + params)
print response.read()
或者,您可以使用(单独安装)使自己的安装更加简单:

import requests

params = {'name': 'John','email': 'John.doe@example.com'}
response = requests.get('http://10.0.0.201/welcome.php', params=params)
print response.content

不要使用
urllib
,而是按照Martijn的建议使用
requests
库。这将使事情变得更简单

查看文档:

我刚刚删除了“@”,它可以工作:

Status
200
Reason
OK
Read
<html>
<body>

Welcome John<br>
Your email address is: John.doe@example.com
</body>
</html>
状态
200
理由
好啊
阅读
欢迎约翰
你的电子邮件地址是:约翰。doe@example.com
谢谢Martijn Pieters

至于POST方法,我使用该示例进行基础设施测试。 最后,我需要填充mysql数据库,并使用python脚本通过php从中检索数据。 最好的方法是什么?
为什么不推荐使用HTTPConnection()?

为什么在有
urllib2
库或您可以安装时手动驱动
HTTPConnection
的痛苦路线?