kcg_remote_client.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import socket
  2. import argparse as ap
  3. import readline
  4. def connect(host, port):
  5. soc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  6. soc.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  7. soc.connect((host, port))
  8. return soc
  9. def send(string, host, port, p=True):
  10. soc = connect(host, port)
  11. soc.send(string)
  12. ret = ""
  13. if 'get_image' in string:
  14. f = open('/tmp/kcgimage.png', 'w+')
  15. while True:
  16. try:
  17. if 'get_image' in string:
  18. res = soc.recv(4096)
  19. if 'close' in res:
  20. f.write(res[:-5])
  21. f.close()
  22. return "Done"
  23. f.write(res)
  24. continue
  25. soc.settimeout(20)
  26. res = soc.recv(1024)
  27. if res == 'close':
  28. if p:
  29. return
  30. else:
  31. # print ret
  32. return ret
  33. # if '.' == res[0]:
  34. # if p:
  35. # print res[1:]
  36. # else:
  37. # ret += res[1:]
  38. else:
  39. if p:
  40. print res
  41. else:
  42. ret += res
  43. except socket.timeout:
  44. if not p:
  45. print "Timed Out"
  46. else:
  47. while True:
  48. input = raw_input("Server takes long to respond, continue waiting? [y]es/[n]o ")
  49. if input == 'y':
  50. break
  51. elif input == 'n':
  52. return
  53. else:
  54. print 'wrong input'
  55. if 'get_image' in string:
  56. f.close()
  57. return
  58. if not p:
  59. return ret
  60. def main():
  61. parse = ap.ArgumentParser("Remote Client for KCG - KAPTURE Control Gui")
  62. parse.add_argument("--port", "-p", type=int, required=True, help="Port")
  63. parse.add_argument("--host", "-H", type=str, required=True, help="Host")
  64. parse.add_argument("--command", "-c", type=str, help="Directly send this command and do not start interactive session.")
  65. args = parse.parse_args()
  66. if args.command:
  67. send(args.command, args.host, args.port)
  68. else:
  69. while True:
  70. input = raw_input("KCG > ")
  71. if input == 'stop':
  72. break
  73. send(input, args.host, args.port)
  74. if __name__ == '__main__':
  75. main()