4

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?

2
  • It's really going to depend upon the framework you are using. For django, for instance, you check request.method, and see if it is "PUT", "POST", "GET", "DELETE", or whatever else. Commented Jan 9, 2011 at 23:51
  • Unfortunately, the application whose server-side I'm duplicating requires usage of various different HTTP status codes, regardless of whether or not they'd actually make sense, which mostly don't -- the bash client just extracts the code out of the response header with cut -- Django is thus too low-level for this. Commented Jan 10, 2011 at 2:34

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

4 Comments

Wow, I didn't know PUT was so similar to POST in this regard! I'll try this in a moment.
Hmm... better make that tomorrow. No offense to you, another task just sprang up that takes priority over this one.
This just hangs in write to stdout (not writing to output, not reading wsgi.input). There is something different about the request handshake for PUT.
CONTENT_LENGTH is set. Maybe you have to use that? Is it always set? What about really large files?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.