May-15-2020, 03:10 AM
Now I am trying to learn some basics about cgi scripts (on Python). I want to implement saving a file on the server,when User send it (push the button in html form). I made it like in manual : http://cgi.tutorial.codepoint.net/big-file-upload
However when I send a large csv file ,which includes 100 000 rows, I get response that file is written successfully,but really few number of rows were written (around 30000) and the process seems to stop. What are the possible reasons?And how to deal with these files in future? Scrypt:
However when I send a large csv file ,which includes 100 000 rows, I get response that file is written successfully,but really few number of rows were written (around 30000) and the process seems to stop. What are the possible reasons?And how to deal with these files in future? Scrypt:
# -*- coding: utf-8 -*-
import cgi, os
import cgitb
cgitb.enable()
try: # Windows needs stdio set for binary mode.
import msvcrt
msvcrt.setmode (0, os.O_BINARY) # stdin = 0
msvcrt.setmode (1, os.O_BINARY) # stdout = 1
except ImportError:
pass
form = cgi.FieldStorage()
# Generator to buffer file chunks
def fbuffer(f, chunk_size=1024):
while True:
chunk = f.read(chunk_size)
if not chunk: break
yield chunk
# A nested FieldStorage instance holds the file
fileitem = form['file']
# Test if the file was uploaded
if fileitem.filename:
fn = os.path.basename(fileitem.filename)
f = open('C:/Users/timna/' + fn, 'wb')
# Read the file in chunks
for chunk in fbuffer(fileitem.file):
f.write(chunk)
f.close()Server:# -*- coding: utf-8 -*-
from http.server import HTTPServer, CGIHTTPRequestHandler
server_address = ("", 8000)
httpd = HTTPServer(server_address, CGIHTTPRequestHandler)
httpd.serve_forever()Thanks for the response!
