python-使用POST上传文件
我正在一台服务器上运行python,并希望将一些文件上载到另一台服务器。但是,当我运行python脚本时,文件不会移动到第二台服务器 第一台服务器上脚本的Python代码为:python-使用POST上传文件,python,php,python-2.7,post,Python,Php,Python 2.7,Post,我正在一台服务器上运行python,并希望将一些文件上载到另一台服务器。但是,当我运行python脚本时,文件不会移动到第二台服务器 第一台服务器上脚本的Python代码为: url = 'https://www.example.com/incoming.php' for x in list: if os.path.exists(x): filename = os.path.abspath(x) #get the full path of a file
url = 'https://www.example.com/incoming.php'
for x in list:
if os.path.exists(x):
filename = os.path.abspath(x) #get the full path of a file
new_name = filename.replace('/', '_') #replace the / with _
new_name_zip = new_name + '.zip'
shutil.copy(x, new_name) #copy the file and give it a new name
zippy = zipfile.ZipFile(new_name_zip, 'w', zipfile.ZIP_DEFLATED)
zippy.write(new_name)
os.remove(new_name) #remove the unzipped file
uploadFile = {'uploadFile': (new_name_zip, open(new_name_zip, 'rb'))}
r = requests.post(url, files=uploadFile)
print (r.status_code)
print (r.reason)
$name=$_FILES['uploadFile']['name'];
$size=$_FILES['uploadFile']['size'];
$type=$_FILES['uploadFile']['type'];
$tmp_name=$_FILES['uploadFile']['tmp_name'];
$error=$_FILES['uploadFile']['error'];
$location='uploads/';
if(move_uploaded_file($tmp_name, $location.$name)) {
$myfile = fopen("newfile.txt", "w");
$txt = "Success\n";
fwrite($myfile, $txt);
fclose($myfile);
}
?>
第二台服务器上incoming.php的代码为:
$name=$_FILES['uploadFile']['name'];
$size=$_FILES['uploadFile']['size'];
$type=$_FILES['uploadFile']['type'];
$tmp_name=$_FILES['uploadFile']['tmp_name'];
$error=$_FILES['uploadFile']['error'];
$location='uploads/';
if(move_uploaded_file($tmp_name, $location.$name)) {
$myfile = fopen("newfile.txt", "w");
$txt = "Success\n";
fwrite($myfile, $txt);
fclose($myfile);
}
?>
第二台服务器上的整个目录结构和所有文件都归www数据所有。在第一台服务器上运行脚本时得到的响应代码是“500 Internal server Error”。我在第一台服务器上使用了Ubuntu 18.04上的Python 2.7,在第二台服务器上使用了Ubuntu 18.04上的LAMP stack。好的,我重新整理了代码,并使其成功运行。工作守则是:
#!/usr/bin/python3
import requests
url = 'http://example.com/receiver.php'
filename = 'file.txt'
up = {'uploadedFile':(filename, open(filename, 'rb'), 'multipart/form-data')}
r = requests.post(url, files=up)
print (str(r.status_code) + ' ' + r.reason)
服务器端的PHP代码(/var/www/html)是: