OLD | NEW |
(Empty) | |
| 1 #!/usr/bin/env python |
| 2 ''' |
| 3 PEXPECT LICENSE |
| 4 |
| 5 This license is approved by the OSI and FSF as GPL-compatible. |
| 6 http://opensource.org/licenses/isc-license.txt |
| 7 |
| 8 Copyright (c) 2012, Noah Spurrier <noah@noah.org> |
| 9 PERMISSION TO USE, COPY, MODIFY, AND/OR DISTRIBUTE THIS SOFTWARE FOR ANY |
| 10 PURPOSE WITH OR WITHOUT FEE IS HEREBY GRANTED, PROVIDED THAT THE ABOVE |
| 11 COPYRIGHT NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES. |
| 12 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 13 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 14 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 15 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 19 |
| 20 ''' |
| 21 from __future__ import print_function |
| 22 import sys, tty, termios |
| 23 |
| 24 if hasattr(sys.stdin, 'buffer'): |
| 25 # Python 3: we want to read raw bytes |
| 26 stdin = sys.stdin.buffer |
| 27 else: |
| 28 stdin = sys.stdin |
| 29 |
| 30 def main(): |
| 31 print('READY', end='\r\n') |
| 32 while True: |
| 33 try: |
| 34 val = ord(stdin.read(1)) |
| 35 except KeyboardInterrupt: |
| 36 val = 3 |
| 37 print('%d<STOP>' % (val,), end='\r\n') |
| 38 if val == 0: |
| 39 # StopIteration equivalent is ctrl+' ' (\x00, NUL) |
| 40 break |
| 41 |
| 42 if __name__ == '__main__': |
| 43 fd = sys.stdin.fileno() |
| 44 old_settings = termios.tcgetattr(fd) |
| 45 try: |
| 46 tty.setraw(sys.stdin.fileno()) |
| 47 main() |
| 48 finally: |
| 49 termios.tcsetattr(fd, termios.TCSADRAIN, old_settings) |
| 50 sys.stdout.flush() |
OLD | NEW |