return filetextsrvrname make writableowned by 'nobodydef main()if not 'clientfilein formprint(html 'errorno file was received'elif not form['clientfile'filenameprint(html 'errorfilename is missing'elsefileinfo form['clientfile'tryfiletextsrvrname saveonserver(fileinfoexcepterrmsg 'error% %stuple(sys exc_info()[: ]print(html errmsgelseprint(goodhtml (cgi escape(fileinfo filename)cgi escape(srvrname)cgi escape(filetext))main(within this scriptthe python-specific interfaces for handling uploaded files are employed they aren' very newreallythe file comes into the script as an entry in the parsed form object returned by cgi fieldstorageas usualits key is clientfilethe input control' name in the html page' code this timethoughthe entry has additional attributes for the file' name on the client moreoveraccessing the value attribute of an uploaded file input object will automatically read the file' contents all at once into string on the server for very large fileswe can instead read line by line (or in chunks of bytesto avoid overflowing memory space internallypython' cgi module stores uploaded files in temporary files automaticallyreading them in our script simply reads from that temporary file if they are very largethoughthey may be too long to store as single string in memory all at once for illustration purposesthe script implements either schemebased on the setting of the loadtextauto global variableit either asks for the file contents as string or reads it line by line in generalthe cgi module gives us back objects with the following attributes for file upload controlsfilename the name of the file as specified on the client file file object from which the uploaded file' contents can be read value the contents of the uploaded file (read from the file on attribute accessadditional attributes are not used by our script files represent third input field objectas we've also seenthe value attribute is string for simple input fieldsand we may receive list of objects for multiple-selection controls transferring files to clients and servers