Advertisement
mrk1989

DirectToDiskFileUpload.py

Oct 22nd, 2013
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. #!/usr/bin/python2.4
  2.  
  3. import cherrypy
  4. import cgi
  5. import tempfile
  6. import os
  7.  
  8.  
  9. __author__ = "Ex Vito"
  10.  
  11.  
  12. #FROM http://tools.cherrypy.org/wiki/DirectToDiskFileUpload
  13.  
  14. class myFieldStorage(cgi.FieldStorage):
  15. """Our version uses a named temporary file instead of the default
  16. non-named file; keeping it visibile (named), allows us to create a
  17. 2nd link after the upload is done, thus avoiding the overhead of
  18. making a copy to the destination filename."""
  19.  
  20. def make_file(self, binary=None):
  21. return tempfile.NamedTemporaryFile()
  22.  
  23.  
  24. def noBodyProcess():
  25. """Sets cherrypy.request.process_request_body = False, giving
  26. us direct control of the file upload destination. By default
  27. cherrypy loads it to memory, we are directing it to disk."""
  28. cherrypy.request.process_request_body = False
  29.  
  30. cherrypy.tools.noBodyProcess = cherrypy.Tool('before_request_body', noBodyProcess)
  31.  
  32.  
  33. class fileUpload:
  34. """fileUpload cherrypy application"""
  35.  
  36. @cherrypy.expose
  37. def index(self):
  38. """Simplest possible HTML file upload form. Note that the encoding
  39. type must be multipart/form-data."""
  40.  
  41. return """
  42. <html>
  43. <body>
  44. <form action="upload" method="post" enctype="multipart/form-data">
  45. File: <input type="file" name="theFile"/> <br/>
  46. <input type="submit"/>
  47. </form>
  48. </body>
  49. </html>
  50. """
  51.  
  52. @cherrypy.expose
  53. @cherrypy.tools.noBodyProcess()
  54. def upload(self, theFile=None):
  55. """upload action
  56.  
  57. We use our variation of cgi.FieldStorage to parse the MIME
  58. encoded HTML form data containing the file."""
  59.  
  60. # the file transfer can take a long time; by default cherrypy
  61. # limits responses to 300s; we increase it to 1h
  62. cherrypy.response.timeout = 3600
  63.  
  64. # convert the header keys to lower case
  65. lcHDRS = {}
  66. for key, val in cherrypy.request.headers.iteritems():
  67. lcHDRS[key.lower()] = val
  68.  
  69. # at this point we could limit the upload on content-length...
  70. # incomingBytes = int(lcHDRS['content-length'])
  71.  
  72. # create our version of cgi.FieldStorage to parse the MIME encoded
  73. # form data where the file is contained
  74. formFields = myFieldStorage(fp=cherrypy.request.rfile,
  75. headers=lcHDRS,
  76. environ={'REQUEST_METHOD':'POST'},
  77. keep_blank_values=True)
  78.  
  79. # we now create a 2nd link to the file, using the submitted
  80. # filename; if we renamed, there would be a failure because
  81. # the NamedTemporaryFile, used by our version of cgi.FieldStorage,
  82. # explicitly deletes the original filename
  83. theFile = formFields['theFile']
  84. os.link(theFile.file.name, '/tmp/'+theFile.filename)
  85.  
  86. return "ok, got it filename='%s'" % theFile.filename
  87.  
  88.  
  89. # remove any limit on the request body size; cherrypy's default is 100MB
  90. # (maybe we should just increase it ?)
  91. cherrypy.server.max_request_body_size = 0
  92.  
  93. # increase server socket timeout to 60s; we are more tolerant of bad
  94. # quality client-server connections (cherrypy's defult is 10s)
  95. cherrypy.server.socket_timeout = 60
  96. tutconf = os.path.join(os.path.dirname(__file__), 'cherrypy_file.conf')
  97.  
  98. if __name__ == '__main__':
  99. # CherryPy always starts with app.root when trying to map request URIs
  100. # to objects, so we need to mount a request handler root. A request
  101. # to '/' will be mapped to HelloWorld().index().
  102. cherrypy.quickstart(fileUpload(), config=tutconf)
  103. else:
  104. # This branch is for the test suite; you can ignore it.
  105. cherrypy.tree.mount(fileUpload(), config=tutconf)
  106.  
  107. #cherrypy.quickstart(fileUpload())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement