1

我有一个简单的表单,它接受文本并通过 post 方法调用一个 php 文件 abc.php:

<form method='post' action="abc.php">
<input type="textarea" name="text">
<input type="submit">
</form>

abc.php 的内容

<?php
$msg=$_POST["text"];
print $msg;
?>

我编写了一个 python 脚本,用于使用 requests 库发布消息,如下所示:

import requests
keys={'text':'lol rofl'}
r=requests.post("http://127.0.0.1/abc.php/POST",params=keys)
print r.url
print r.text

我得到以下输出(错误):

  http://localhost/abc.php/POST?text=lol+rofl

<b>notice</b>:undefined index;text in C:/xampp/htdocs/abc.php on line 2

注意:从浏览器运行时,form 和 abc.php 可以完美运行

4

1 回答 1

5

使用的正确参数是data

http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests

Typically, you want to send some form-encoded data — much like an HTML form.
To do this, simply pass a dictionary to the data argument.
Your dictionary of data will automatically be form-encoded
when the request is made:

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> r = requests.post("http://httpbin.org/post", data=payload)
于 2013-03-17T14:15:10.287 回答