开发者

Handle the HTTP PUT method in Python WSGI

I currently have a bash application that, among other things, uses cURL to upload a file 开发者_Go百科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?


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜