/Demo/sockets/rpython.py

http://unladen-swallow.googlecode.com/ · Python · 35 lines · 27 code · 5 blank · 3 comment · 4 complexity · dd8ecaed0def30440749c6cd9b2a565f MD5 · raw file

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