我有一个 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"
},
...
block 引用>这似乎表明我的服务器有问题。可能是什么问题?
最佳答案
你的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/