$_FILES is empty after POSTing with python script


$_FILES is empty after POSTing with python script

我有一个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脚本的响应:

阵列(0){

}

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