Hardware Reference
In-Depth Information
10. Open a web browser, enter your Galileo's IP into the address bar and hit
Enter.
You should see the text on screen: “The button is not pressed!” Try pushing
the button and hitting refresh on your browser. You should then see “The
button is pressed!”
Example 6-11. Serving web pages with Python
import SocketServer
import SimpleHTTPServer #
PORT = 80 #
class MyTCPServer ( SocketServer . TCPServer ):
allow_reuse_address = True #
class myHandler ( SimpleHTTPServer . SimpleHTTPRequestHandler ):
def do_GET ( self ):
self . send_response ( 200 ) #
self . send_header ( "Content-type" , "text/html" )
self . end_headers ()
self . wfile . write ( "<html><body>" ) #
self . wfile . write ( "<h1>The button is" )
with open ( "/sys/class/gpio/gpio32/value" , "r" ) as gpio : #
state = gpio . read ( 1 ) #
if state == "0" : #
self . wfile . write ( " not" ) #
self . wfile . write ( " pressed!</h1>" ) #
self . wfile . write ( "</body></html>" )
httpd = MyTCPServer (( "" , PORT ), myHandler )
print "Serving from port" , PORT
httpd . serve_forever () #
Import the Python libraries for server functionality.
Set the port to 80, the default for HTTP.
Avoid “address already in use” error.
Send the HTTP response headers.
Send the HTTP response body.
Open the GPIO system file for pin 2 to get the pin's status.
Store one byte from that file into state .
If the state is 0, it means the button is not pressed (low).
Reply with the word “not” if the button is not pressed.
Send the rest of the HTTP response.
Run the server until the user presses Control-C.
Search WWH ::




Custom Search