简体   繁体   English

python请求模块发布到PHP脚本

[英]python requests module post to php script

I need to upload some .csv file into a self signed https apache server. 我需要将一些.csv文件上传到自签名的https apache服务器中。 My https server is running PHP. 我的https服务器正在运行PHP。 My client is running a python script to POST files into the https server. 我的客户端正在运行python脚本,以将文件发布到https服务器中。 PHP.ini has 20M for upload_file and POST_max_size directives with no extra whitespace. PHP.ini有20M用于upload_file和POST_max_size指令,没有多余的空格。 The file i need to upload is only 4kilobytes 我需要上传的文件只有4千字节

my python script: 我的python脚本:

import requests
username = "user"
password = 'password'
myfile = "/full/path/to/myfile.csv"
url = "https://www.mydomain.com/file_upload.php"

files = {'file': open(myfile, 'rb')}

r = requests.post(url, files=files, auth=(username, password), verify=False)
print r.text
print r.status_code

I receive a status code 200 but the file is not in the destination server I believe the error is somewhere in my file_upload.php 我收到状态码200,但文件不在目标服务器中,我认为错误在我的file_upload.php中

$_FILES['userfile']['name'] was the name i was using when i was posting the file from a HTML form which is not the case anymore. $ _FILES ['userfile'] ['name']是我从HTML表单发布文件时使用的名称,现在不再如此。 I believe i am missing something due to my lack of understanding of the $_FILES variable in PHP - How the file-id should look like when not posting from a form? 我相信由于我对PHP中的$ _FILES变量缺乏理解而错过了一些东西- 当不从表单发布时,文件ID应该是什么样子?

<?php

$uploaddir = '/var/www/html/uploads/';
$uploadfile = $uploaddir . basename($_FILES['file-id']['name']);

move_uploaded_file($_FILES['file-id']['tmp_name'], $uploadfile);
error_log(print_r($_FILES['file-id']['error']), 3, $error_log_file);

?>

my apache error.log reports the following PHP notice 我的Apache error.log报告以下PHP通知

[Thu Jun 05 09:35:19.120760 2014] [:error] [pid 8030] [client xxx.xxx.xxx.xxx:49207] PHP Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0 [2014年6月5日星期四09:35:19.120760] [:错误] [pid 8030] [客户端xxx.xxx.xxx.xxx:49207] PHP警告:第0行上的多部分/表单数据POST数据缺少边界

[Thu Jun 05 09:35:19.124625 2014] [:error] [pid 8030] [client xxx.xxx.xxx.xxx:49207] PHP Notice: Undefined index: file in /var/www/html/file_upload.php on line 8 [2014年6月5日星期四09:35:19.124625] [:错误] [pid 8030] [客户端xxx.xxx.xxx.xxx:49207] PHP注意:未定义的索引:在/var/www/html/file_upload.php中的文件8号线

[Thu Jun 05 09:35:19.124673 2014] [:error] [pid 8030] [client xxx.xxx.xxx.xxx:49207] PHP Notice: Undefined index: file in /var/www/html/file_upload.php on line 11 [2014年6月5日星期四09:35:19.124673] [:错误] [pid 8030] [客户端xxx.xxx.xxx.xxx:49207] PHP注意:未定义的索引:在/var/www/html/file_upload.php中的文件11行

[Thu Jun 05 09:35:19.124686 2014] [:error] [pid 8030] [client xxx.xxx.xxx.xxx:49207] PHP Notice: Undefined index: file in /var/www/html/file_upload.php on line 12 [2014年6月5日星期四09:35:19.124686] [:错误] [pid 8030] [客户端xxx.xxx.xxx.xxx:49207] PHP注意:未定义的索引:在/var/www/html/file_upload.php中的文件12行

Permission my www folder is chown -R www-data.www-data 权限我的www文件夹是chown -R www-data.www-data

Here is the solution: 解决方法如下:

When you want to upload a file using the python module requests read the file as follow: 当您想使用python模块上传文件时,请按照以下说明读取文件:

files = {'testname': open(myfile, 'rb')}

now in the receiving PHP file the $_FILES variable must be used as follow: 现在,在接收PHP文件中,必须按如下方式使用$ _FILES变量:

$uploadfile = $uploaddir . basename($_FILES['testname']['name']);
move_uploaded_file($_FILES['testname']['tmp_name'], $uploadfile);

basically the name for the $_FILES index is the NAME of the label you give when you read your file name in python. 基本上,$ _FILES索引的名称是在python中读取文件名时所给标签的名称。

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

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