Can someone tell me how to upload the contents of a (relatively small) file using an HTML form and CGI in Python 3.1? As far as I can tell from a half-day of experimenting, browsing, and searching the Python issue tracker, this is broken. Very simple example:

<html>
  <head>
  </head>
  <body>
    <form action="http://localhost:9000/cgi/cgi-test.py";
          enctype="multipart/form-data"
          method="post">
        <label>File</label><br/>
          <input type="file" name="contents"> <br/>
        <button type="submit" >Submit</button> <br/>
    </form>
  </body>
</html>


cgi-test.py:


#!/usr/local/bin/python3
import cgi
import sys
form = cgi.FieldStorage()
print(form.getfirst('contents'), file=sys.stderr)
print('done')


I run a CGI server with:

#!/usr/bin/env python3
from http.server import HTTPServer, CGIHTTPRequestHandler
HTTPServer(('', 9000), CGIHTTPRequestHandler).serve_forever()



What happens is that the upload never stops. It works in 2.6.

If I cancel the upload from the browser, I get the following output, so I know that basically things are working;
the cgi script just never finishes reading the POST input:

localhost - - [02/Mar/2010 16:37:36] "POST /cgi/cgi-test.py HTTP/1.1" 200 -
<<<CONTENTS OF MY FILE PRINTED HERE>>>
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 55779)
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/ python3.1/socketserver.py", line 281, in _handle_request_noblock
    self.process_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/ python3.1/socketserver.py", line 307, in process_request
    self.finish_request(request, client_address)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/ python3.1/socketserver.py", line 320, in finish_request
    self.RequestHandlerClass(request, client_address, self)
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/ python3.1/socketserver.py", line 614, in __init__
    self.handle()
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/ python3.1/http/server.py", line 352, in handle
    self.handle_one_request()
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/ python3.1/http/server.py", line 346, in handle_one_request
    method()
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/ python3.1/http/server.py", line 868, in do_POST
    self.run_cgi()
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/ python3.1/http/server.py", line 1045, in run_cgi
    if not self.rfile.read(1):
File "/Library/Frameworks/Python.framework/Versions/3.1/lib/ python3.1/socket.py", line 214, in readinto
    return self._sock.recv_into(b)
socket.error: [Errno 54] Connection reset by peer
----------------------------------------


--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to