server.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 traceback
  24. import time
  25. import yaml
  26. import mylibnetwork
  27. lroot = logging.getLogger("root")
  28. parser = argparse.ArgumentParser(
  29. description="BeeWatch Server",
  30. )
  31. parser.add_argument(
  32. "host",
  33. type=str,
  34. help="hostname/ip address/filename (only if --use-file was specified)"
  35. )
  36. parser.add_argument(
  37. "port",
  38. type=int,
  39. default=80540,
  40. nargs="?",
  41. help="the port on wich the connection will open up. ignored if --use-file \
  42. is given"
  43. )
  44. parser.add_argument(
  45. "-d","--debug",
  46. default=False,
  47. action="store_true",
  48. help="enables logging"
  49. )
  50. group_af_family = parser.add_mutually_exclusive_group()
  51. group_af_family.add_argument(
  52. "-u","--af-unix",
  53. dest="af_family",
  54. # default=False,
  55. action="store_const",
  56. const="AF_UNIX",
  57. help="""use an AF_UNIX socket (a file). can't be used in combination with
  58. --af-inet."""
  59. # " will be used \
  60. # instead of an AF_INET socket (hostname/ip address + port)."
  61. )
  62. group_af_family.add_argument(
  63. "-i","--af_inet",
  64. default="AF_INET",
  65. dest="af_family",
  66. action="store_const",
  67. const="AF_INET",
  68. help="""use an AF_INET socket (hostname/ip address + port). can't be used
  69. with --af-unix"""
  70. # (hostname/ip address + port) will be used instead of an AF_INET \
  71. # socket (a file)."
  72. )
  73. args = parser.parse_args()
  74. print(args)
  75. if args.debug:
  76. with open("../../bee_logging.yaml") as f:
  77. data = f.read()
  78. config = yaml.safe_load(data)
  79. logging.config.dictConfig(config)
  80. server = mylibnetwork.Server(
  81. host=args.host,
  82. port=args.port,
  83. af_family=args.af_family,
  84. handler=mylibnetwork.ConnHandlerEcho
  85. )
  86. try:
  87. server.run()
  88. except KeyboardInterrupt:
  89. lroot.warn("KeyboardInterrupt, aborting!")
  90. finally:
  91. pass