在Python WSGI中处理HTTP PUT方法

发布于 2024-10-11 03:42:08 字数 331 浏览 5 评论 0原文

我目前有一个 bash 应用程序,除其他外,它使用 cURL 通过 PUT 方法将文件上传到 Web 应用程序。我试图复制 Web 应用程序,因为客户端 (bash) 部分是 GPL,但 Web 部分不是。我也无法更改客户端应用程序,因为它会从开发人员的网站自动更新。 我找到了大量关于如何使用 WSGI、CherryPy、Twisted 处理 HTTP POST 方法的信息,以及几乎所有在 WWW 上运行 Python 脚本的方法。但是,我找不到任何有关 PUT 方法的信息。有谁知道如何使用 WSGI 处理 PUT 请求,或者是否有其他一些我缺少的具有 PUT 功能的框架?

I currently have a bash application that, among other things, uses cURL to upload a file to a web application with the PUT method. I am attempting to duplicate the web application as the client (bash) portion is GPL but the web portion is not. I also cannot alter the client application as it auto-updates itself from the developers' website.
I have found multitudes of information on how to handle the HTTP POST method with WSGI, CherryPy, Twisted, and practically every way of having Python scripts working on the WWW. However, I can't find a single thing about the PUT method. Does anyone know how to process a PUT request with WSGI, or is there some other framework with PUT functionality that I am missing?

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

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

发布评论

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

评论(1

宫墨修音 2024-10-18 03:42:08

据我了解,您只想读取流environ['wsgi.input'],因为PUT请求将发送的全部内容PUT 作为请求的正文。

我不知道您需要处理任何编码问题(除了它是二进制的事实)。

前段时间,我编写了一组简单的 PHP 脚本,用于从 LAN 上的另一台服务器获取和提供大文件。我们从 POST 开始,但很快就耗尽了较大文件的内存。因此,我们切换到 PUT,其中 PHP 脚本可以花很长时间循环遍历 php://input 一次 4096 个字节(或其他)...它工作得很好。

这是 PHP 代码:

$f1 = fopen('php://input', 'rb');
$f2 = fopen($FilePath, 'wb');

while($data = fread($f1, 4096))
{
    fwrite($f2, $data);
}

fclose($f1);
fclose($f2);

根据我在 WSGI 中使用 POST 处理 multipart/form-data 的经验,我毫不怀疑您可以处理 PUT code> 只需读取输入流即可。

python 代码应该是这样的:

  output = open('/tmp/input', 'wb')
  while True:
    buf = environ['wsgi.input'].read(4096)
    if len(buf) == 0:
      break
    output.write(buf)

As I understand it, you will just want to read the stream environ['wsgi.input'], because a PUT request will send the entire contents of the PUT as the body of the request.

I am not aware of any encoding issues you will have to deal with (other than the fact that it is binary).

Some time ago, I wrote a simple set of PHP scripts to take and give huge files from another server on a LAN. We started with POST, but quickly ran out of memory on the larger files. So we switched to PUT, where the PHP script could take it's good time looping through php://input 4096 bytes at a time (or whatever)... It works great.

Here is the PHP code:

$f1 = fopen('php://input', 'rb');
$f2 = fopen($FilePath, 'wb');

while($data = fread($f1, 4096))
{
    fwrite($f2, $data);
}

fclose($f1);
fclose($f2);

From my experience in handling multipart/form-data in WSGI with POST, I have little doubt that you can handle a PUT by just reading the input stream.

The python code should be like this:

  output = open('/tmp/input', 'wb')
  while True:
    buf = environ['wsgi.input'].read(4096)
    if len(buf) == 0:
      break
    output.write(buf)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文