php - 使用 python 脚本发布后 $_FILES 为空

标签 php python

我有一个 python 脚本,应该将文件上传到 php 脚本。

Python

import requests

file={'file':('text.txt','hello')}

url='mywebsite.org/test.php


response = requests.post(url, files=file)
                              
print(response.text)

PHP

<?php
    var_dump($_FILES);
    var_dump($_POST);
?>

这是我对 python 脚本的响应:

array(0) {

}

array(0) {

}

但是,当我尝试发帖到 http://httpbin.org/post 时,

我明白了

...

"files": {

"file": "hello"

},

...

这似乎表明我的服务器有问题。可能是什么问题?

最佳答案

你的Python代码似乎有问题——目前你没有发送文件,因为它没有打开。假设您的 text.txt 包含 1234。发布至http://httpbin.org/post像这样:

import requests

file={'file':(open('text.txt','r').read())}

url='http://httpbin.org/post'

response = requests.post(url, files=file)

print(response.text)

我们得到以下回复:

...
"files": {
    "file": "1234"
  },
...

如果你想添加一些额外的参数,你可以这样做:

values = {'message': 'hello'}
response = requests.post(url, files=file, data=values)

关于php - 使用 python 脚本发布后 $_FILES 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24028749/

相关文章:

php - 带有 ajax 提交处理程序的 jquery 验证插件不起作用

javascript - 为公共(public)共享保存值状态(添加到 URL)

php - 如何减去分钟数

python - 我继承的类 MenuBar(tk.Menu) 不显示菜单栏

python - 解析 DTD 以揭示元素的层次结构

python - python中的贪心排序

python - 文件名中的空格 python 3.4.2

php - 我可以仅通过数据中的字母和数字查询列吗?比如 preg_replace

php - 将 mysql_num_rows 与多个表一起使用?

python - 我可以在嵌入式交互式 Python 控制台中使用 IPython 吗?