tcp_client.py 941 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #!/usr/bin/env python3
  2. #
  3. # SPDX-License-Identifier: GPL-2.0
  4. #
  5. import sys, os, os.path, getopt
  6. import socket, time
  7. import subprocess
  8. import select
  9. def read(sock, n):
  10. buf = b''
  11. while len(buf) < n:
  12. rem = n - len(buf)
  13. try: s = sock.recv(rem)
  14. except (socket.error) as e: return b''
  15. buf += s
  16. return buf
  17. def send(sock, s):
  18. total = len(s)
  19. count = 0
  20. while count < total:
  21. try: n = sock.send(s)
  22. except (socket.error) as e: n = 0
  23. if n == 0:
  24. return count;
  25. count += n
  26. return count
  27. serverPort = int(sys.argv[1])
  28. HostName = socket.gethostname()
  29. # create active socket
  30. sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM)
  31. try:
  32. sock.connect((HostName, serverPort))
  33. except socket.error as e:
  34. sys.exit(1)
  35. buf = b''
  36. n = 0
  37. while n < 1000:
  38. buf += b'+'
  39. n += 1
  40. sock.settimeout(1);
  41. n = send(sock, buf)
  42. n = read(sock, 500)
  43. sys.exit(0)