Since Brian Kernigham's 1972 book, A Tutorial Introduction to the Language B, "Hello, World!" has become a standard first program. So, we will start CherryPy too by writing a program to display "Hello, World!".
The below code starts a server at port number 8080 and hosts an application which will be served at request reaching http://127.0.0.1:8080/ or http://localhost:8080/
import cherrypy
class hello_world(object):
def index(self):
return "Hello, World!"
if __name__ == '__main__':
cherrypy.quickstart(hello_world())
This is the most basic application you could write with CherryPy. Save this code in a file named 'hello_world.py' and execute it in Terminal/Command Line as follows:
python3 hello_world.py
The output will look something like this:
[08/Sep/2020:12:17:54] ENGINE Listening for SIGTERM.[08/Sep/2020:12:17:54] ENGINE Bus STARTED
The first three lines indicate that the server will handle signals for you. The next line indicates the current state of the server which is in the STARTING stage. Then, it specifies that we are using default configuration. Next, the server starts a couple of internal utilities. Finally, the server indicates that it is now ready to accept incoming communications as it listens on the address 'http://127.0.0.1:8080'.
Now, open a browser window and enter http://127.0.0.1:8080/ or http://localhost:8080/ in the address bar. The result will be like this
Comments