带有Unicode文件名的python-requests post [英] python-requests post with unicode filenames

查看:94
本文介绍了带有Unicode文件名的python-requests post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里已经阅读了一些有关SO的相关问题,但没有找到有效的解决方案.

I've read through several related questions here on SO but didn't manage to find a working solution.

我有一个带有以下简化代码的Flask服务器:

I have a Flask server with this simplified code:

app = Flask(__name__)
api = Api(app)


class SendMailAPI(Resource):
    def post(self):
        print request.files
        return Response(status=200)

api.add_resource(SendMailAPI, '/')

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

然后在客户端中

# coding:utf-8

import requests

eng_file_name = 'a.txt'
heb_file_name = u'א.txt'

requests.post('http://localhost:5000/', files={'file0': open(eng_file_name, 'rb')})
requests.post('http://localhost:5000/', files={'file0': open(heb_file_name, 'rb')})

当发送带有非utf-8文件名的第一个请求时,服务器将接收带有文件的请求并打印 ImmutableMultiDict([('file0',< FileStorage:u'a.txt'(无)>)]),但是在发送带有utf-8文件名的文件时,服务器似乎没有收到文件,因为它会打印 ImmutableMultiDict([]).

When sending the first request with the non-utf-8 filename the server receives the request with the file and prints ImmutableMultiDict([('file0', <FileStorage: u'a.txt' (None)>)]), but when sending the file with the utf-8 filename the server doesn't seem to receive the file as it prints ImmutableMultiDict([]).

我正在使用请求 2.3.0 ,但最新版本( 2.8.1 )也无法解决该问题,Flask版本为0.10.1 和Flask-RESTful版本是 0.3.4 .

I'm using requests 2.3.0 but the problem doesn't resolve with the latest version as well (2.8.1), Flask version is 0.10.1 and Flask-RESTful version is 0.3.4.

我已经在 requests 代码中进行了一些挖掘,并且请求似乎可以正常发送(即与文件一起发送),并且我在发送请求之前立即打印了该请求,并查看了文件名确实已编码为RFC2231:

I've done some digging in requests code and the request seems to be sent ok (ie with the file), and I printed the request right before it is being sent and see the file name was indeed encoded to RFC2231:

--6ea257530b254861b71626f10a801726
Content-Disposition: form-data; name="file0"; filename*=utf-8''%D7%90.txt

总而言之,我不完全确定问题是否出在 requests 内,该问题没有将文件正确地附加到请求上,或者 Flask 是否有问题拾取具有根据RFC2231编码的文件名的文件时出现问题.

To sum things up, I'm not entirely sure if the problem lies within requests that doesn't properly attach the file to the request or if Flask is having issues with picking up files with file names that are encoded according to RFC2231.

更新:在 requests GitHub中遇到了此问题: https://github.com/kennethreitz/requests/issues/2505

UPDATE: Came across this issue in requests GitHub: https://github.com/kennethreitz/requests/issues/2505

推荐答案

我认为这里的编码可能有些混乱-

I think maybe there's confusion here on encoding here -

eng_file_name = 'a.txt'  # ASCII encoded, by default in Python 2
heb_file_name = u'א.txt'  # NOT UTF-8 Encoded - just a unicode object

要将第二个发送到服务器,您要执行以下操作:

To send the second one to the server what you want to do is this:

requests.post('http://localhost:5000/', files={'file0': open(heb_file_name.encode('utf-8'), 'rb')})

我对它在尝试打开文件时不会在客户端上引发错误感到有些惊讶-您在客户端看不到任何指示错误的信息吗?

I'm a little surprised that it doesn't throw an error on the client trying to open the file though - you see nothing on the client end indicating an error?

一种确认或否定我的想法的简单方法当然是从客户端内部打印出内容,以确保其被正确读取.

An easy way to confirm or deny my idea is of course to print out the contents from inside the client to ensure it's being read properly.

这篇关于带有Unicode文件名的python-requests post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆