Skip to content
Related Articles

Related Articles

Upload file and read its content in cherrypy python
  • Last Updated : 05 Sep, 2020

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 ?

  • Python with version 2.4 or above
  • CherryPy 3.0
  • Installation –
    To install cherrypy run the following command in terminal:



    pip install cherrypy

    A simple Application –

    A cherrypy Application typically looks like this:

    import cherrypy

    class HelloWorld(object):
    @cherrypy.expose
    def index(self):
    return “Hello World!”

    cherrypy.quickstart(HelloWorld())

    Project to Upload file and read its content –

    Steps taken to upload a file and read its content using cherrypy:



    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

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

    chevron_right

    
    

    Read file:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    # 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())

    chevron_right

    
    

    To stop the engine, use the following code:

    cherrypy.engine.exit()

    Output(Before file upload):

    Output(After uploading the file):

    After submission:

    Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

    To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

    My Personal Notes arrow_drop_up
Recommended Articles
Page :