Open In App

Upload file and read its content in cherrypy python

CherryPy is a web framework of Python which provides a friendly interface to the HTTP protocol for Python developers. It is also called a web application library. It allows developers to build web applications in much the same way they would build any other object-oriented Python program. This results in smaller source code developed in less time.

This framework is mainly for the developers who want to create a portable database-driven web applications using Python, as it provides Create, Retrieve, Update and Delete functionalities.



Prerequisites –

The basic requirements for installation of CherryPy framework include ?



  1. Create any text file to read or existing file can also be used. Geeks.txt file is used in the program.
  2. Create user interface that upload a file from system
  3. Write cherrypy program that read the content of file and show its content.
HTML code that uploads the file from system:




<html>
<head>
  <style>
    .container{
      height: 300px;
      width: 600px;
      background-color: maroon;
      margin-top: 12%;
      margin-left: 25%;
      color: white;
    }
    .container h2{
      font-size: 40px;
      text-align: center;  
        
    }
    .off{
      font-size: 25px;
      margin-left: 23%;
      color: yellow;
      background-color: ;
    }
    .on{
      font-size: 25px;
      border-top-right-radius: 25px;
      border-top-left-radius: 25px;
      border-bottom-left-radius: 25px;
      border-bottom-right-radius: 25px;
      margin-top: 20px;   }
  </style>
  </head>
<body>
  
  <div class="container">  
    <h2><u><i>Upload a file</i></u></h2>
    <form action="store" id="form" method="GET">
    <input class="off" type="file" name="myFile" /><br />
      
    <input style="margin-left: 250px;" class="on" id=" submit" type="submit" /></div>
  </div>    
    </form>
  </div>
              
</body>
</html>

Read file:




# import files
import random
import string
import cherrypy
  
# function to read file content
def readf(filename):
    file = open(filename)
    read = file.read()
    return read
  
class Root(object):
     
    @cherrypy.expose
    def index(self):
        return """<html>
<head>
  <style>
    .container{
      height: 300px;
      width: 600px;
      background-color: maroon;
      margin-top: 12 %;
      margin-left: 25 %;
      color: white;
    }
    .container h2{
      font-size: 40px;
      text-align: center;  
        
    }
    .off{
      font-size: 25px;
      margin-left: 23 %;
      color: yellow;
      background-color: ;
    }
    .on{
      font-size: 25px;
      border-top-right-radius: 25px;
      border-top-left-radius: 25px;
      border-bottom-left-radius: 25px;
      border-bottom-right-radius: 25px;
      margin-top: 20px;   }
      p{ margin-top:100px;
      font-size: 50px;
      color: green}
  </style>
  </head>
<body>
 <center><p> GEEKSFORGEEKS</p></center>
  <div class ="container">  
    <h2><u><i>Upload a file</i></u></h2>
     
    <form action ="store" id ="form" method ="GET">
    <input class ="off" type ="file" name ="myFile" /><br />   
      
      
    <input style ="margin-left: 250px;" class ="on" id =" submit" type ="submit" /></div>
  </div>    
    </form>
  </div>
              
</body>
</html>
"""
     
      
    @cherrypy.expose
    def store(self, myFile):       
        f = readf(myFile)                # read the uploaded file
   
        return f
    
   
if __name__=="__main__":
    # set port address to 8089
    cherrypy.config.update({'server.socket_port': 8089})   
    cherrypy.quickstart(Root())

To stop the engine, use the following code:

cherrypy.engine.exit()

Output(Before file upload):

Output(After uploading the file):

After submission:


Article Tags :