Simple WEB Control of Raspberry Pi using Python

Source: Internet
Author: User
This article describes how to use Python to easily implement Raspberry Pi's WEB control. If you need it, you can refer to it to show you the effect. I feel very satisfied. Please continue to read the full text:


Knowledge used: Python Bottle HTML Javascript JQuery Bootstrap AJAX, of course, linux

I'm going, so many ...... Let me start ......

First paste the final source code:

#! /Usr/bin/env python3from bottle import get, post, run, request, template @ get ("/") def index (): return template ("index ") @ post ("/cmd") def cmd (): print ("press the button:" + request. body. read (). decode () return "OK" run (host = "0.0.0.0 ")

That's right. I just want to explain 10 sentences:

1 .#! /Usr/bin/env python3: Tell shell that the file is Python source code, and let bash call python3 to explain the code.

2. from bottle import get, post, run, request, template, the methods and objects I used are imported from the bottle framework.

The following sentence defines two routes, one is "/" and the other is "/cmd". The former is of the get type (decorated with @ get ), the latter is POST type (decorated with @ post)

The first route is very simple, that is, reading the index template (the template is html) and sending it to the client (browser), because the path is "/", that is, for example, the IP address of Raspberry Pi is 192.168.0.10.

Use http: // 192.168.0.10: 8080 to access our "/" route (the default port of the bottle is 8080)

Similarly, the second route path is "/cmd", that is, access http: // 192.168.0.10: 8080/cmd to access the second route.

The last sentence: run (host = "0.0.0.0") is to call the run method of bottle to create an http server so that we can access our interface through a browser.

Below I will explain in detail the functions of these codes:

The role of the first route is to throw the browser an HTML (index. tpl) document to display this interface:


The source code of this file is as follows:

 
 Remote Control Raspberry Pi
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.