Advertisement
Guest User

tulpe

a guest
Dec 3rd, 2009
7,240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. """Speaking HTTP server"""
  2.  
  3. __author__    = 'Thomas Schuessler <tulpe@atomar.de>'
  4. __copyright__ = 'Copyright (c) 2009, Thomas Schuessler'
  5. __license__   = 'Apache License, Version 2.0'
  6.  
  7.  
  8. import android
  9. import BaseHTTPServer
  10. import urlparse
  11.  
  12. HOST_NAME   = ''
  13. PORT_NUMBER = 9090
  14.  
  15. droid = android.Android()
  16.  
  17. PAGE_TEMPLATE = '''
  18. <html>
  19. <head>
  20. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  21. <title>Talk through the droid</title>
  22. <style type="text/css">
  23.     * {
  24.         font-family:arial;
  25.         font-size:12pt;
  26.     }
  27.     body {
  28.         background: #BBB;
  29.     }
  30.     #container {
  31.         text-align:center;
  32.         width:300px;
  33.     }
  34.     #say_what {
  35.         background:yellow;
  36.         border:3px solid #555;
  37.         color:#555;
  38.         width:250px;
  39.         height:100px;
  40.         padding:4px;
  41.     }
  42.     #talk_button {
  43.         background:red;
  44.         border:2px solid black;
  45.     }
  46. </style>
  47. </head>
  48. <body>
  49.     <div id="container">
  50.     <form method="get">
  51.         <textarea id="say_what" name="say_what">%s</textarea><br />
  52.         <input id="talk_button" type="submit" value="talk!" />
  53.     </form>
  54.     </div>
  55. </body>
  56. </html>
  57. '''
  58.  
  59.  
  60. class DroidHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  61.    
  62.     def do_HEAD(s):
  63.         s.send_response(200)
  64.         s.send_header("Content-type", "text/html; charset=utf-8")
  65.         s.end_headers()
  66.  
  67.     def do_GET(s):
  68.         """Respond to a GET request."""
  69.         s.send_response(200)
  70.         s.send_header("Content-type", "text/html; charset=utf-8")
  71.         s.end_headers()
  72.        
  73.         url = urlparse.urlsplit(s.path)
  74.         if url.path != '/':
  75.             return
  76.            
  77.         query = url.query
  78.         args = urlparse.parse_qsl(query)
  79.        
  80.         say_what = ''
  81.         for arg in args:
  82.             if arg[0] == 'say_what':
  83.                 say_what = arg[1].strip().replace('\r', '')
  84.                
  85.                 droid.speak(say_what)
  86.                 notice = '%s says:\n\n%s' % (s.client_address[0], say_what)
  87.                 droid.makeToast(notice)
  88.                 print(notice)
  89.                 break
  90.  
  91.         html = PAGE_TEMPLATE % say_what
  92.         s.wfile.write(html)
  93.  
  94. print 'web server running on port %s' % PORT_NUMBER
  95.  
  96. BaseHTTPServer.HTTPServer((HOST_NAME, PORT_NUMBER), DroidHandler).serve_forever()
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement