您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import hashlib
  2. import socket
  3. from typing import Any
  4. from .python_3x import url_encode
  5. # define constants
  6. CONFIGURATIONS = "configurations"
  7. NOTIFICATION_ID = "notificationId"
  8. NAMESPACE_NAME = "namespaceName"
  9. # add timestamps uris and keys
  10. def signature(timestamp: str, uri: str, secret: str) -> str:
  11. import base64
  12. import hmac
  13. string_to_sign = "" + timestamp + "\n" + uri
  14. hmac_code = hmac.new(secret.encode(), string_to_sign.encode(), hashlib.sha1).digest()
  15. return base64.b64encode(hmac_code).decode()
  16. def url_encode_wrapper(params: dict[str, Any]) -> str:
  17. return url_encode(params)
  18. def no_key_cache_key(namespace: str, key: str) -> str:
  19. return f"{namespace}{len(namespace)}{key}"
  20. # Returns whether the obtained value is obtained, and None if it does not
  21. def get_value_from_dict(namespace_cache: dict[str, Any] | None, key: str) -> Any | None:
  22. if namespace_cache:
  23. kv_data = namespace_cache.get(CONFIGURATIONS)
  24. if kv_data is None:
  25. return None
  26. if key in kv_data:
  27. return kv_data[key]
  28. return None
  29. def init_ip() -> str:
  30. ip = ""
  31. s = None
  32. try:
  33. s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  34. s.connect(("8.8.8.8", 53))
  35. ip = s.getsockname()[0]
  36. finally:
  37. if s:
  38. s.close()
  39. return ip