使用Python-Requests Library发布文本文件

2 python post http-request python-requests

嗨,我在使用Python请求库(http://docs.python-requests.org/en/latest/index.html)发布文本文件时遇到问题,你能让我知道我做错了什么吗?

我尝试搜索相关问题,并从Python脚本中使用POST发现此发送文件,但它没有回答我的问题.

这是我的代码:

import codecs
import requests

# Create a text file
savedTextFile = codecs.open('mytextfile.txt', 'w', 'UTF-8')

# Add some text to it
savedTextFile.write("line one text for example\n and line two text for example")

# Post the file THIS IS WHERE I GET REALLY TRIPPED UP
myPostRequest = requests.post("https://someURL.com", files=savedTextFile)
Run Code Online (Sandbox Code Playgroud)

我已经尝试了上面的一些变化,我没有得到任何地方(每次都有新的错误).如何发布我刚刚创建的这个txt文件?我试图发布的API需要将文本文件发布到它.

任何帮助表示赞赏!

sud*_*dom 5

files参数需要一个与文件处理程序匹配的文件名的dict.这在源代码中描述(当前第69行):

Github Source(requests/models.py)

#: Dictionary of files to multipart upload (``{filename: content}``).
self.files = files
Run Code Online (Sandbox Code Playgroud)

有时最好的文档是代码.

您的最后一行应如下所示:

myPostRequest = requests.post("https://someURL.com", files={'mytextfile.txt': savedTextFile})
Run Code Online (Sandbox Code Playgroud)