Advertisement
cymplecy

WebToScratch

Sep 25th, 2015
490
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.97 KB | None | 0 0
  1. #!/usr/bin/env python
  2. from bottle import route, run, template, request
  3. import socket
  4.  
  5. @route('/')
  6. def index():
  7.     return '''
  8.       <form action="/send" method="post">
  9.           Command: <input name="scratchgpio" type="text" />
  10.           <input value="Send" type="submit" />
  11.       </form>
  12.   '''
  13.  
  14. @route('/send', method='POST')
  15. def get_sendtext():
  16.     sendText = request.forms.get('scratchgpio')
  17.     sendToScratch(sendText)
  18.     return "<p>Your command<p>" + sendText + "<p>was sent</p>"    
  19.    
  20. def sendToScratch(stext):
  21.     scratch_socket2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  22.     scratch_socket2.connect(('127.0.0.1', 42001))
  23.  
  24.     totalcmd = ''
  25.     cmd = 'broadcast "' + stext + '"'
  26.     n = len(cmd)
  27.     b = (chr((n >> 24) & 0xFF)) + (chr((n >> 16) & 0xFF)) + (chr((n >> 8) & 0xFF)) + (
  28.         chr(n & 0xFF))
  29.     totalcmd = b + cmd
  30.  
  31.     scratch_socket2.send(totalcmd)    
  32.     scratch_socket2.close()
  33.  
  34. run(host='0.0.0.0', port=80)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement