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 ?
- 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:
- Create any text file to read or existing file can also be used. Geeks.txt file is used in the program.
- Create user interface that upload a file from system
- 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><divclass="container"><h2><u><i>Upload a file</i></u></h2><formaction="store"id="form"method="GET"><inputclass="off"type="file"name="myFile"/><br/><inputstyle="margin-left: 250px;"class="on"id=" submit"type="submit"/></div></div></form></div></body></html>Read file:
# import filesimportrandomimportstringimportcherrypy# function to read file contentdefreadf(filename):file=open(filename)read=file.read()returnreadclassRoot(object):@cherrypy.exposedefindex(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.exposedefstore(self, myFile):f=readf(myFile)# read the uploaded filereturnfif__name__=="__main__":# set port address to 8089cherrypy.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:


.png)

.png)