简体   繁体   English

将文件上传到服务器无法在python中工作

[英]upload file to server not working in python

I'd like to upload files to a server using python. 我想使用python将文件上传到服务器。 That means in this case that when I write http://example.com/files/ in my browser I'd like to see de.txt there in the list of files. 这意味着在这种情况下,当我在浏览器中写入http://example.com/files/时 ,我希望在文件列表中看到de.txt。 How should I change this script to be working? 我应该如何更改此脚本才能正常工作? When I run it, python shell writes out nothing, only the >>>. 当我运行它时,python shell只写>>>不写任何内容。

Thanks in advance! 提前致谢!

import requests

url = 'http://example.com/files/'
user, password = 'ex', 'ample'

files = {'upload_file': open(r'C:\Users\example\Desktop\code\de.txt','rb')}

r = requests.post(url,  auth=(user, password), files=files)

Writing a file across network 跨网络写入文件

import requests
from requests.auth import HTTPBasicAuth

url = 'http://example.com/files/'
user, password = 'ex', 'ample'

files = {'file': ('de.txt', open('de.txt', 'rb'), 'multipart/form-data', {'Expires': '0'})}

r = requests.post(url,  auth=HTTPBasicAuth(user, password), files=files)

For reference use Python Request documentation 供参考,请使用Python Request文档

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

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