throughput_client.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #!/usr/bin/python3
  2. #
  3. import throughput_protocols
  4. import basic_protocols
  5. import useful
  6. import os
  7. import argparse
  8. import logging
  9. import socket
  10. #
  11. if __name__ == '__main__':
  12. logging.basicConfig(level=logging.DEBUG)
  13. #
  14. parser = argparse.ArgumentParser(description='Test the network throughput (optionally through a proxy).')
  15. parser.add_argument('ip', type=str, help='destination ip address')
  16. parser.add_argument('port', type=int, help='destination port')
  17. parser.add_argument('num_bytes', type=useful.parse_bytes,
  18. help='number of bytes to send (can also end with \'B\', \'KiB\', \'MiB\', or \'GiB\')', metavar='num-bytes')
  19. parser.add_argument('--proxy', type=str, help='proxy ip address and port', metavar=('ip','port'), nargs=2)
  20. parser.add_argument('--wait', type=int,
  21. help='wait until the given time before pushing data (time in seconds since epoch)', metavar='time')
  22. parser.add_argument('--buffer-len', type=useful.parse_bytes,
  23. help='size of the send and receive buffers (can also end with \'B\', \'KiB\', \'MiB\', or \'GiB\')', metavar='bytes')
  24. parser.add_argument('--no-accel', action='store_true', help='don\'t use C acceleration (use pure Python)')
  25. args = parser.parse_args()
  26. #
  27. #
  28. endpoint = (args.ip, args.port)
  29. client_socket = socket.socket()
  30. protocols = []
  31. #
  32. if args.proxy is None:
  33. logging.debug('Socket %d connecting to endpoint %r...', client_socket.fileno(), endpoint)
  34. client_socket.connect(endpoint)
  35. else:
  36. proxy_username = bytes([x for x in os.urandom(12) if x != 0])
  37. proxy_endpoint = (args.proxy[0], int(args.proxy[1]))
  38. #
  39. logging.debug('Socket %d connecting to proxy %r...', client_socket.fileno(), proxy_endpoint)
  40. client_socket.connect(proxy_endpoint)
  41. #
  42. proxy_protocol = basic_protocols.Socks4Protocol(client_socket, endpoint, username=proxy_username)
  43. protocols.append(proxy_protocol)
  44. #
  45. throughput_protocol = throughput_protocols.ClientProtocol(client_socket, args.num_bytes,
  46. wait_until=args.wait,
  47. send_buffer_len=args.buffer_len,
  48. use_acceleration=(not args.no_accel))
  49. protocols.append(throughput_protocol)
  50. #
  51. combined_protocol = basic_protocols.ChainedProtocol(protocols)
  52. combined_protocol.run()
  53. #