Installing the web server

In this section, we will install a local web server on Raspberry Pi. There are different web server frameworks that can be installed on the Raspberry Pi. They include Apache v2.0, Boost, the REST framework, and so on.

Prepare for lift off

As mentioned earlier, we will build a web server based on the web.py framework. This section is entirely referenced from web.py tutorials (http://webpy.github.io/). In order to install web.py, a Python module installer such as pip or easy_install is required. We will install it using the following command:

sudo apt-get install python-setuptools

Engage thrusters

The web.py framework can be installed using the easy_install tool:

sudo easy_install web.py

Once the installation is complete, it is time to test it with a Hello World! example.

We will open a new file using a text editor available with Python IDLE and get started with a Hello World! example for the web.py framework using the following steps:

  1. The first step is to import the web.py framework:
    import web
  2. The next step is defining the class that will handle the landing page. In this case, it is index:
    urls = ('/','index')
  3. We need to define what needs to be done when one tries to access the URL. We will like to return the Hello world!text:
    class index:
      def GET(self):
        return "Hello world!"
  4. The next step is to ensure that a web page is set up using the web.py framework when the Python script is launched:
    if __name__ == '__main__':
      app = web.application(urls, globals())
      app.run()
  5. When everything is put together, the following code is what we'll see:
    import web
    
    urls = ('/','index')
    
    class index:
        def GET(self):
      return "Hello world!"
    
    if __name__ == '__main__':
      app = web.application(urls,globals())
      app.run()
  6. We should be able to start the web page by executing the Python script:
    python helloworld.py

We should be able to launch the website from the IP address of the Raspberry Pi. For example, if the IP address is 10.0.0.10, the web page can be accessed at http://10.0.0.10:8080 and it displays the text Hello world. Yay!

Engage thrusters

A Hello world! example using the web.py framework

Objective complete – mission debriefing

We built a simple web page to display the Hello world text. In the next task, we will be interfacing the Christmas tree and other decorative appliances to our web page so that we can control it from anywhere on the local network.

It is possible to change the default port number for the web page access by launching the Python script as follows:

python helloworld.py 1234

Now, the web page can be accessed at http://<IP_Address_of_the_Pi>:1234.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.161.228