![](/media/cache/group/DevelopConnectDevices.png.50x50_q85.png)
Demo for Mbed Connect Cloud board and an HTTP Python Server
Dependencies: C12832 mbed-http
View the tutorial for this demo here.
working/server_working.py@2:b91140c3c3f6, 2017-11-17 (annotated)
- Committer:
- Jenny Plunkett
- Date:
- Fri Nov 17 16:04:17 2017 -0600
- Revision:
- 2:b91140c3c3f6
- Parent:
- 0:c5b042cf8162
Fixed typo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Jenny Plunkett |
0:c5b042cf8162 | 1 | from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer |
Jenny Plunkett |
0:c5b042cf8162 | 2 | import SocketServer |
Jenny Plunkett |
0:c5b042cf8162 | 3 | import socket |
Jenny Plunkett |
0:c5b042cf8162 | 4 | |
Jenny Plunkett |
0:c5b042cf8162 | 5 | class MyHandler(BaseHTTPRequestHandler): |
Jenny Plunkett |
0:c5b042cf8162 | 6 | |
Jenny Plunkett |
0:c5b042cf8162 | 7 | # HTTP REQUESTS HERE |
Jenny Plunkett |
0:c5b042cf8162 | 8 | |
Jenny Plunkett |
0:c5b042cf8162 | 9 | def do_POST(self): |
Jenny Plunkett |
0:c5b042cf8162 | 10 | content = b"POST: Hello, Mbed!" |
Jenny Plunkett |
0:c5b042cf8162 | 11 | self.send_response(200) |
Jenny Plunkett |
0:c5b042cf8162 | 12 | self.send_header('Content-type', 'text/plain') |
Jenny Plunkett |
0:c5b042cf8162 | 13 | self.send_header('Content-Length', len(content)) |
Jenny Plunkett |
0:c5b042cf8162 | 14 | self.end_headers() |
Jenny Plunkett |
0:c5b042cf8162 | 15 | self.wfile.write(content) |
Jenny Plunkett |
0:c5b042cf8162 | 16 | return |
Jenny Plunkett |
0:c5b042cf8162 | 17 | |
Jenny Plunkett |
0:c5b042cf8162 | 18 | def do_GET(self): |
Jenny Plunkett |
0:c5b042cf8162 | 19 | content = b"GET: Hello, Mbed!" |
Jenny Plunkett |
0:c5b042cf8162 | 20 | self.send_response(200) |
Jenny Plunkett |
0:c5b042cf8162 | 21 | self.send_header('Content-type', 'text/plain') |
Jenny Plunkett |
0:c5b042cf8162 | 22 | self.send_header('Content-Length', len(content)) |
Jenny Plunkett |
0:c5b042cf8162 | 23 | self.end_headers() |
Jenny Plunkett |
0:c5b042cf8162 | 24 | self.wfile.write(content) |
Jenny Plunkett |
0:c5b042cf8162 | 25 | return |
Jenny Plunkett |
0:c5b042cf8162 | 26 | |
Jenny Plunkett |
0:c5b042cf8162 | 27 | def do_PUT(self): |
Jenny Plunkett |
0:c5b042cf8162 | 28 | content = b"PUT: Hello, Mbed!" |
Jenny Plunkett |
0:c5b042cf8162 | 29 | self.send_response(200) |
Jenny Plunkett |
0:c5b042cf8162 | 30 | self.send_header('Content-type', 'text/plain') |
Jenny Plunkett |
0:c5b042cf8162 | 31 | self.send_header('Content-Length', len(content)) |
Jenny Plunkett |
0:c5b042cf8162 | 32 | self.end_headers() |
Jenny Plunkett |
0:c5b042cf8162 | 33 | self.wfile.write(content) |
Jenny Plunkett |
0:c5b042cf8162 | 34 | return |
Jenny Plunkett |
0:c5b042cf8162 | 35 | |
Jenny Plunkett |
0:c5b042cf8162 | 36 | def run(): |
Jenny Plunkett |
0:c5b042cf8162 | 37 | httpd = HTTPServer(('', 8080), MyHandler) |
Jenny Plunkett |
0:c5b042cf8162 | 38 | print "HTTP server running on port 8080" |
Jenny Plunkett |
0:c5b042cf8162 | 39 | print "Your IP address is: ", socket.gethostbyname(socket.gethostname()) |
Jenny Plunkett |
0:c5b042cf8162 | 40 | httpd.serve_forever() |
Jenny Plunkett |
0:c5b042cf8162 | 41 | |
Jenny Plunkett |
0:c5b042cf8162 | 42 | if __name__ == '__main__': |
Jenny Plunkett |
0:c5b042cf8162 | 43 | run() |