server.py 2.6 KB

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