CGI, POST, and file uploads

  • Thread starter Mitchell L Model
  • Start date
M

Mitchell L Model

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top