configs.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #!/usr/bin/env/python
  2. # -*- coding:utf-8 -*-
  3. import logging
  4. import time
  5. import os
  6. import sys
  7. # 日志路径
  8. log_dir = os.getcwd()+ '\logs'
  9. if not os.path.exists(log_dir):
  10. os.mkdir(log_dir)
  11. class myLog(object):
  12. '''
  13. 封装后的logging
  14. '''
  15. def __init__(self, logger=None, log_cate='search'):
  16. '''
  17. 指定保存日志的文件路径,日志级别,以及调用文件
  18. 将日志存入到指定的文件中
  19. '''
  20. # 创建一个logger
  21. self.logger = logging.getLogger(logger)
  22. self.logger.setLevel(logging.INFO) # DEBUG
  23. # 创建一个handler,用于写入日志文件
  24. # self.log_time = time.strftime("%Y_%m_%d")
  25. # file_dir = os.getcwd() + '/../log'
  26. # if not os.path.exists(file_dir):
  27. # os.mkdir(file_dir)
  28. # self.log_path = file_dir
  29. # self.log_name = self.log_path + "/" + log_cate + "." + self.log_time + '.log'
  30. self.log_name = os.path.join(log_dir, 'parse_log2.log') # 日志地址
  31. # print(self.log_name)
  32. # fh = logging.FileHandler(self.log_name, 'a') # 追加模式 这个是python2的
  33. fh = logging.FileHandler(self.log_name, mode='a', encoding='utf-8', delay=True)
  34. # fh = logging.FileHandler(self.log_name, 'a', encoding='utf-8') # 这个是python3的
  35. fh.setLevel(logging.INFO)
  36. # 再创建一个handler,用于输出到控制台
  37. # ch = logging.StreamHandler()
  38. # ch.setLevel(logging.INFO)
  39. # 定义handler的输出格式
  40. # formatter = logging.Formatter(
  41. # '[%(asctime)s] %(filename)s->%(funcName)s line:%(lineno)d [%(levelname)s]%(message)s')
  42. formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  43. fh.setFormatter(formatter)
  44. # ch.setFormatter(formatter)
  45. # 给logger添加handler
  46. self.logger.addHandler(fh)
  47. # self.logger.addHandler(ch)
  48. # 添加下面一句,在记录日志之后移除句柄
  49. # self.logger.removeHandler(ch)
  50. # self.logger.removeHandler(fh)
  51. # 关闭打开的文件
  52. fh.close()
  53. # ch.close()
  54. def getlog(self):
  55. return self.logger
  56. # testing
  57. class TestingCfg:
  58. internal_ip = '192.168.1.140' # internal
  59. external_ip = '192.168.1.140' # external
  60. server_port = 8888
  61. wordbin_exe = r"F:\word_bin\ConsoleApplication1.exe"
  62. upload_folder = r'F:\zwj\word_upload_v1'
  63. # production
  64. class ProductionCfg:
  65. internal_ip = '0.0.0.0' # internal
  66. external_ip = '49.233.23.58' if sys.argv[-1] == '58' else '49.233.14.55' # external
  67. server_port = 11088
  68. wordbin_exe = r"D:\word_bin\ConsoleApplication1.exe"
  69. upload_folder = r'D:\zwj\word_uploads'
  70. # sys.argv:从控制台窗台运行程序,程序后加参数,以空格隔开,sys.argv[0]即程序本身
  71. config_class = TestingCfg # 没有参数时,默认按测试环境
  72. if len(sys.argv) > 1:
  73. print(sys.argv, sys.argv[0])
  74. print(sys.argv[1])
  75. if sys.argv[1] == 'test':
  76. config_class = TestingCfg
  77. elif sys.argv[1] == 'product':
  78. config_class = ProductionCfg
  79. else:
  80. print('cmd should be: python server.py test')
  81. print('or: python server.py product')
  82. raise ValueError("命令不正确")
  83. server_ip = config_class.external_ip
  84. server_port = config_class.server_port
  85. # 再解析新的文件路径
  86. IMG_FOLDER = "F:/zwj/parse_2021/img_folder"
  87. new_img_ip = "http://192.168.1.140:11066/ser_static"