client.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #!/usr/bin/env python3.5
  2. # Copyright 2017 Digital
  3. #
  4. # This file is part of DigiLib.
  5. #
  6. # DigiLib is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # DigiLib is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with DigiLib. If not, see <http://www.gnu.org/licenses/>.
  18. import argparse
  19. import logging
  20. import logging.config
  21. import logging.handlers
  22. import sys
  23. import threading
  24. import traceback
  25. import time
  26. import yaml
  27. import mylibnetwork
  28. lroot = logging.getLogger("root")
  29. def user_input():
  30. while True:
  31. data = input("type something:")
  32. client.send(data)
  33. def configure_logging():
  34. with open("../../bee_logging.yaml") as f:
  35. data = f.read()
  36. config = yaml.safe_load(data)
  37. logging.config.dictConfig(config)
  38. parser = argparse.ArgumentParser(description="BeeWatch Server",)
  39. parser.add_argument("host",type=str,
  40. help="hostname/ip address/filename (only if --use-file was specified)")
  41. parser.add_argument("port",type=int,default=80540,nargs="?",
  42. help="""the port on wich the connection will open up. ignored if --use-file
  43. is given""")
  44. parser.add_argument("-d","--debug",default=False,action="store_true",
  45. help="enables logging")
  46. parser.add_argument("-a","--async_client",default=False,action="store_true",
  47. help="Use an asynchronous client instead of a threaded one")
  48. group_af_family = parser.add_mutually_exclusive_group()
  49. group_af_family.add_argument(
  50. "-u","--af-unix",
  51. dest="af_family",
  52. action="store_const",
  53. const="AF_UNIX",
  54. help="""use an AF_UNIX socket (a file). can't be used in combination with
  55. --af-inet."""
  56. )
  57. group_af_family.add_argument(
  58. "-i","--af_inet",
  59. default="AF_INET",
  60. dest="af_family",
  61. action="store_const",
  62. const="AF_INET",
  63. help="""use an AF_INET socket (hostname/ip address + port). can't be used
  64. with --af-unix"""
  65. )
  66. args = parser.parse_args()
  67. if args.debug:
  68. configure_logging()
  69. if args.async_client:
  70. client_class = mylibnetwork.AsyncClient
  71. else:
  72. client_class = mylibnetwork.Client
  73. client = client_class(
  74. host=args.host,
  75. port=args.port,
  76. af_family=args.af_family,
  77. )
  78. input_thread = threading.Thread(target=user_input,daemon=True)
  79. try:
  80. input_thread.start()
  81. if args.async_client:
  82. client.run(connect=True)
  83. else:
  84. client.start()
  85. client.connect()
  86. while True:
  87. client.join(1)
  88. if not client.is_running():
  89. break
  90. except KeyboardInterrupt:
  91. print("\r ", end="\r")
  92. lroot.warn("KeyboardInterrupt, aborting!")
  93. finally:
  94. client.stop()