tools.py 465 B

123456789101112131415161718192021222324
  1. import socket
  2. import hashlib
  3. def get_host_ip():
  4. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  5. try:
  6. s.connect(('8.8.8.8', 80))
  7. ip = s.getsockname()[0]
  8. finally:
  9. s.close()
  10. return ip
  11. def sha1(text: str):
  12. """
  13. 十六进制数字字符串形式摘要值
  14. @param text: 字符串文本
  15. @return: 摘要值
  16. """
  17. _sha1 = hashlib.sha1()
  18. _sha1.update(text.encode("utf-8"))
  19. return _sha1.hexdigest()