使用 pycurl 从西里尔路径上传文件

发布于 2024-10-15 17:25:15 字数 1378 浏览 3 评论 0原文

我最近设计了一个由 PyCURL 支持的上传对话框,我正在我的一些应用程序中使用它。
我在设置 pycurl 的 HTTPPOST 选项时遇到了问题。我这样设置:
self.curl.setopt(self.curl.HTTPPOST, [(字段, (self.curl.FORM_FILE, 文件名))])
如果文件名是字符串,则一切正常。然而,如果我向它传递一个 unicode,它会引发一个 TypeError。 我有什么办法可以给它一个西里尔路径吗?我尝试过UTF-8编码,但不成功。 感谢您抽出时间

更新:

我实际上是从 WX 控件获取文件名,因此在我接触它之前它就是 unicode。然后,当我将其编码为 UTF-8 时(使用 filename = filename.encode('UTF-8')),setopt 运行正常,但执行时一切都会崩溃:

* About to connect() to example.com port 80 (#0)
*   Trying 123.123.123.123... * connected
* Connected to example.com (123.123.123.123) port 80 (#0)
* failed creating formpost data
* Connection #0 to host example.com left intact
Traceback (most recent call last):
  File "c:\python27\lib\site-packages\transfer_dialogs-0.28-py2.7.egg\transfer_dialogs\transfer_dialogs.py", line 64, in perform_transfer
    self.curl.perform()
error: (26, 'failed creating formpost data')

更新 2:

根据要求,多一点数据。文件名包含打开对话框中 GetValue() 的结果。
logging.debug("文件名: %r 编码文件名: %r" % (文件名, filename.encode('UTF-8')))
结果:
2011 年 2 月 5 日星期六 03:33:56 core.dialogs.upload_audio 调试:文件名:u'C:\Users\Q\test\\u0422\u0435\u0441\u0442\u043e\u0432\u0430\u044f \u043f\u0430 \u043f\u043a\u0430\test.mp3' 编码文件名:'C:\Users\Q\test\\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82\xd0\xbe\xd0\xb2\xd0 \xb0\xd1\x8f\xd0\xbf\xd0\xb0\xd0\xbf\xd0\xba\xd0\xb0\test.mp3'

I've recently designed an upload dialog backed by PyCURL which I'm using in a few of my applications.
I have run into an issue when setting pycurl's HTTPPOST option. I am setting it like so:
self.curl.setopt(self.curl.HTTPPOST, [(field, (self.curl.FORM_FILE, filename))])
If filename is a string, all is fine. If I pass it a unicode, however, it raises a TypeError.
Is there any way for me to be able to give it a Cyrillic path? I tried UTF-8 encoding it, but that was unsuccessful.
Thank you for your time

Update:

I'm actually getting the filename from a WX control, so it's unicode before I even touch it. When I then encode it to UTF-8, (using filename = filename.encode('UTF-8')) the setopt goes fine but everything blows up on perform:

* About to connect() to example.com port 80 (#0)
*   Trying 123.123.123.123... * connected
* Connected to example.com (123.123.123.123) port 80 (#0)
* failed creating formpost data
* Connection #0 to host example.com left intact
Traceback (most recent call last):
  File "c:\python27\lib\site-packages\transfer_dialogs-0.28-py2.7.egg\transfer_dialogs\transfer_dialogs.py", line 64, in perform_transfer
    self.curl.perform()
error: (26, 'failed creating formpost data')

Update 2:

As requested, a bit more data. filename contains the result of a GetValue() from the open dialog.
logging.debug("Filename: %r encoded filename: %r" % (filename, filename.encode('UTF-8')))
result:
Sat Feb 05, 2011 03:33:56 core.dialogs.upload_audio DEBUG: Filename: u'C:\Users\Q\test\\u0422\u0435\u0441\u0442\u043e\u0432\u0430\u044f \u043f\u0430\u043f\u043a\u0430\test.mp3' encoded filename: 'C:\Users\Q\test\\xd0\xa2\xd0\xb5\xd1\x81\xd1\x82\xd0\xbe\xd0\xb2\xd0\xb0\xd1\x8f \xd0\xbf\xd0\xb0\xd0\xbf\xd0\xba\xd0\xb0\test.mp3'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尸血腥色 2024-10-22 17:25:15

文件名应采用 UTF-8 格式,您上传文件的主机应支持 UTF-8 文件名。如果它支持不同的非 Unicode 编码,请尝试对文件名 KOI8-R 或 WIN1251 进行编码(但这当然不太好且不符合标准)。

编辑,看到评论后:可能应该是 ur"C:\Users\Q\test\Тестовая папка\test.mp3".encode("UTF-8")< /代码>。 认为这很重要;如果没有它,西里尔字母将在您的控制台编码中进行编码。我只是尝试了一下,它成功了(不是上传,只是 setopt)。

Filename should be in UTF-8, and the host you upload it to should support UTF-8 file names. If it supports a different, non-Unicode encoding, try to encode the filename KOI8-R or WIN1251 (but this, of course, is not nice and standards-compliant).

EDIT, having seen the comments: Probably it should have been ur"C:\Users\Q\test\Тестовая папка\test.mp3".encode("UTF-8"). That u bit it important; without it, the Cyrillic letters are taken encoded in your console encoding. I did just try it, and it worked (not upload, just setopt).

木槿暧夏七纪年 2024-10-22 17:25:15

将此问题分解为 2 个部分:

  1. 告诉 pycurl 打开哪个文件来读取文件数据
  2. 将文件名以正确的编码发送到服务器

这些可能是相同的编码,也可能不是相同的编码。

对于 1,使用 sys.getfilesystemencoding() 来将 unicode 文件名(在整个 Python 代码中正确使用)转换为 pycurl/libcurl 可以使用 fopen() 正确打开的字符串。使用 strace (linux) 或等效的 windows osx 验证 pycurl 打开的文件路径是否正确。

如果完全失败,您始终可以通过 pycurl.READFUNCTION 从 Python 提供文件数据流。

对于2,了解文件上传过程中如何传输文件名,示例。我没有一个好的链接,我只知道这不是微不足道的,例如当涉及到很长的文件名时。

Decompose this problem into 2 components:

  1. tell pycurl which file to open to read file data
  2. send filename in correct encoding to the server

These may or may not be same encodings.

For 1, use sys.getfilesystemencoding() to convert unicode filename (which you use throughout python code correctly) to a string that pycurl/libcurl can open correctly with fopen(). Use strace (linux) or equivalent windows osx to verify correct file path is being opened by pycurl.

If that totally fails you can always feed file data stream from Python via pycurl.READFUNCTION.

For 2, learn how filename is transmitted during file upload, example. I don't have a good link, all I know it's not trivial, e.g. when it comes to very long file names.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文