tcp_connect.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env python2.7
  2. # Copyright 2015 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """Opens a TCP connection to a specified server and then exits."""
  16. import argparse
  17. import socket
  18. import sys
  19. import threading
  20. import time
  21. def main():
  22. argp = argparse.ArgumentParser(
  23. description='Open a TCP handshake to a server')
  24. argp.add_argument('-s',
  25. '--server_host',
  26. default=None,
  27. type=str,
  28. help='Server host name or IP.')
  29. argp.add_argument('-p',
  30. '--server_port',
  31. default=0,
  32. type=int,
  33. help='Port that the server is listening on.')
  34. argp.add_argument('-t',
  35. '--timeout',
  36. default=1,
  37. type=int,
  38. help='Force process exit after this number of seconds.')
  39. args = argp.parse_args()
  40. socket.create_connection([args.server_host, args.server_port],
  41. timeout=args.timeout)
  42. if __name__ == '__main__':
  43. main()