You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

awsd.py 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import sys
  2. import socket
  3. import os
  4. def read_char():
  5. import sys, termios, tty
  6. fd = sys.stdin.fileno()
  7. old_settings = termios.tcgetattr(fd)
  8. try:
  9. tty.setraw(sys.stdin.fileno())
  10. ch = sys.stdin.read(1)
  11. finally:
  12. termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
  13. return ch
  14. def read_view(f):
  15. view = f.readline()
  16. if not view:
  17. return
  18. for x in range(2, len(view)):
  19. line = f.readline()
  20. if not line:
  21. return
  22. view += line
  23. return view
  24. def main(host='localhost', port=62688):
  25. s = socket.socket()
  26. s.connect((host, port))
  27. f = s.makefile()
  28. commands = {'w':'^', 'a':'<', 's':'v', 'd':'>', 'W':'^', 'A':'<', 'S':'v', 'D':'>' }
  29. while True:
  30. try:
  31. view = read_view(f)
  32. if not view:
  33. break
  34. os.system('clear')
  35. sys.stdout.write(view)
  36. cmd = read_char()
  37. if cmd == 'q':
  38. break
  39. cmd = commands[cmd]
  40. s.send(cmd if sys.version_info[0] < 3 else str.encode(cmd))
  41. except Exception as e:
  42. print(e)
  43. break
  44. s.close()
  45. if __name__ == '__main__':
  46. main( * sys.argv[1:])