我正在通过POST将一个包含Python脚本中字符串的文本文件发送到我的服务器:
fo = open('data.txt','a')
fo.write("hi, this is my testing data")
fo.close()
with open('data.txt', 'rb') as f:
r = requests.post("http://XXX.XX.X.X", data = {'data.txt':f})
f.close()
在我的服务器处理程序脚本中接收并处理它,该脚本是基于在线示例构建的:
def do_POST(self):
data = self.rfile.read(int(self.headers.getheader('Content-Length')))
empty = [data]
with open('processing.txt', 'wb') as file:
for item in empty:
file.write("%s\n" % item)
file.close()
self._set_headers()
self.wfile.write("<html><body><h1>POST!</h1></body></html>")
我想问的是,这个怎么实现:
self.rfile.read(int(self.headers.getheader('Content-Length')))
获取我的数据长度(一个整数,以字节/字符为单位),然后读取我的文件?我不明白它如何知道我的数据包含什么。HTTP背后发生了什么?
它输出data.txt=hi%2C+this+is+my+testing+data
到我的processing.txt,但我期望得到"hi this is my testing data"
我尝试过查找rfile.read()的文档,但没有成功,如果找到答案就可以解决我的问题,我会删除这个问题。