n/a

Dependencies:   C12832 mbed-http

Fork of HTTP-Python-Demo by Cambridge Hackathon

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers server_working.py Source File

server_working.py

00001 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
00002 import SocketServer
00003 import socket
00004 
00005 class MyHandler(BaseHTTPRequestHandler):
00006 
00007     # HTTP REQUESTS HERE
00008 
00009     def do_POST(self):
00010         content = b"POST: Hello, Mbed!"
00011         self.send_response(200)
00012         self.send_header('Content-type', 'text/plain')
00013         self.send_header('Content-Length', len(content))
00014         self.end_headers()
00015         self.wfile.write(content)
00016         return
00017 
00018     def do_GET(self):
00019         content = b"GET: Hello, Mbed!"
00020         self.send_response(200)
00021         self.send_header('Content-type', 'text/plain')
00022         self.send_header('Content-Length', len(content))
00023         self.end_headers()
00024         self.wfile.write(content)
00025         return
00026 
00027     def do_PUT(self):
00028         content = b"PUT: Hello, Mbed!"
00029         self.send_response(200)
00030         self.send_header('Content-type', 'text/plain')
00031         self.send_header('Content-Length', len(content))
00032         self.end_headers()
00033         self.wfile.write(content)
00034         return
00035 
00036 def run():
00037     httpd = HTTPServer(('', 8080), MyHandler)
00038     print "HTTP server running on port 8080"
00039     print "Your IP address is: ", socket.gethostbyname(socket.gethostname())
00040     httpd.serve_forever()
00041 
00042 if __name__ == '__main__':
00043     run()