rpython.py 778 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/usr/bin/env python3
  2. """
  3. Remote python client.
  4. Execute Python commands remotely and send output back.
  5. """
  6. import sys
  7. from socket import socket, AF_INET, SOCK_STREAM, SHUT_WR
  8. PORT = 4127
  9. BUFSIZE = 1024
  10. def main():
  11. if len(sys.argv) < 3:
  12. print("usage: rpython host command")
  13. sys.exit(2)
  14. host = sys.argv[1]
  15. port = PORT
  16. i = host.find(':')
  17. if i >= 0:
  18. port = int(host[i+1:])
  19. host = host[:i]
  20. command = ' '.join(sys.argv[2:])
  21. s = socket(AF_INET, SOCK_STREAM)
  22. s.connect((host, port))
  23. s.send(command.encode())
  24. s.shutdown(SHUT_WR)
  25. reply = b''
  26. while True:
  27. data = s.recv(BUFSIZE)
  28. if not data:
  29. break
  30. reply += data
  31. print(reply.decode(), end=' ')
  32. s.close()
  33. main()