Upload text file?

Fredrik Lundh fredrik at pythonware.com
Wed Aug 14 06:03:03 EDT 2002


Gustaf Liljegren wrote:

> I'm looking for a general way to have a CGI script fetch the whole body of
> an HTTP POST request and put in a variable. It sounds so simple, but I've
> only found solutions for more complex problems, involving forms and binary
> files. Here's my POST request:
>
> ---
> POST /cgi-bin/test.py HTTP/1.1
> Host: 127.0.0.1
> Accept: */*
> Connection: close
>
> Hello world!
> ---
>
> So it's the "Hello world!" I want in a variable...

    variable = sys.stdin.read()

or better:

    bytes = int(os.environ.get("CONTENT_LENGTH", 0))

    if bytes > MAX_REQUEST_SIZE:
        giveup("request too large")

    variable = sys.stdin.read(bytes)

</F>





More information about the Python-list mailing list