system.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # File : system.py
  4. # Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
  5. # Date : 2022/9/6
  6. from flask import request
  7. import psutil
  8. import sys
  9. from utils.cfg import cfg
  10. def get_wlan_info():
  11. info = psutil.net_if_addrs()
  12. # print(info)
  13. netcard_info = []
  14. ips = []
  15. for k, v in info.items():
  16. for item in v:
  17. if item[0] == 2:
  18. netcard_info.append((k, item[1]))
  19. ips.append(item[1])
  20. return netcard_info,ips
  21. def get_host_ip(): # 获取局域网ip
  22. netcard_info,ips = get_wlan_info()
  23. # print(netcard_info)
  24. real_ips = list(filter(lambda x: x and x != '127.0.0.1', ips))
  25. jyw = list(filter(lambda x: str(x).startswith('192.168') and not str(x).endswith('.1'), real_ips))
  26. # print(jyw)
  27. return real_ips[-1] if len(jyw) < 1 else jyw[0]
  28. def getHost(mode=0,port=None):
  29. port = port or request.environ.get('SERVER_PORT')
  30. # mode 为0是本地,1是局域网 2是线上
  31. try:
  32. mode = int(mode)
  33. except:
  34. mode = 2
  35. if mode == 0:
  36. host = f'http://localhost:{port}'
  37. elif mode == 1:
  38. REAL_IP = get_host_ip()
  39. ip = REAL_IP
  40. host = f'http://{ip}:{port}'
  41. else:
  42. from controllers.service import storage_service
  43. lsg = storage_service()
  44. # print(cfg.PLAY_URL) # 可能会报错: 'EasyDict' object has no attribute 'xxx'
  45. host = lsg.getItem('PLAY_URL',cfg.get('PLAY_URL',''))
  46. # print(mode,host)
  47. return host
  48. def is_linux():
  49. return not 'win' in sys.platform