简体   繁体   English

通过python接收HTTP POST响应

[英]Receive HTTP POST response by python

I use the following example: http://www.w3schools.com/php/php_forms.asp我使用以下示例: http : //www.w3schools.com/php/php_forms.asp

When I run it from browser, I see the results in the browser:当我从浏览器运行它时,我在浏览器中看到结果:

Welcome John
Your email address is john.doe@example.com

When I run python POST http request:当我运行 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()

I see the following:我看到以下内容:

Status
200
Reason
OK
Read
<html>
<body>

Welcome <br>
Your email address is: 
</body>
</html>

The question is: How to receive POST request data in python?问题是:如何在python中接收POST请求数据?

You are using the wrong form names and the wrong HTTP method.您使用了错误的表单名称错误的 HTTP 方法。 There are no @ characters at their starts:它们的开头没有@字符:

params = urllib.urlencode({'name': 'John','email': 'John.doe@example.com'})

Next, the form you point to uses GET , not POST as the handling method, so you'll have to add these parameters to the URL instead:接下来,您指向的表单使用GET而非POST作为处理方法,因此您必须将这些参数添加到 URL 中:

conn.request("GET", "/welcome.php?" + params, '', headers)

You are doing yourself a disservice by trying to drive the HTTPConnection() manually.您尝试手动驱动HTTPConnection()是在伤害自己。 You could use urllib2.urlopen() instead for example:例如,您可以使用urllib2.urlopen()代替:

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()

or you could make use of the requests library (separate install) to make it yourself much easier still:或者您可以使用requests(单独安装)使自己更容易:

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

Instead of using urllib , use the requests library as Martijn suggested.不要使用urllib ,而是使用 Martijn 建议的requests库。 It will make things much simpler.它会让事情变得简单得多。

Check out the documentation: http://docs.python-requests.org/en/latest/user/quickstart/查看文档: http : //docs.python-requests.org/en/latest/user/quickstart/

I just removed the "@" and it works:我刚刚删除了“@”并且它有效:

Status
200
Reason
OK
Read
<html>
<body>

Welcome John<br>
Your email address is: John.doe@example.com
</body>
</html>

Thank you Martijn Pieters.谢谢马丁·彼得斯。

As for POST method, i used the example for infrastructure testing purposes.至于 POST 方法,我将示例用于基础设施测试目的。 Finally i need to fill mysql database and retrieve data from it over php using python script.最后,我需要填充 mysql 数据库并使用 python 脚本通过 php 从中检索数据。 What is the best method for it?最好的方法是什么? Why is HTTPConnection() not recommended?为什么不推荐使用 HTTPConnection()?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM