This repository has been archived on 2023-06-10. You can view files and clone it, but cannot push or open issues or pull requests.
homeassistant-config/deps/lib/python3.10/site-packages/pytapo/media_stream/_utils.py

30 lines
843 B
Python
Raw Permalink Normal View History

2023-02-11 18:37:07 +00:00
import hashlib
import os
from typing import Mapping, Tuple, Optional
def md5digest(to_hash: bytes) -> bytes:
return hashlib.md5(to_hash).digest().hex().upper().encode()
def generate_nonce(length: int) -> bytes:
return os.urandom(length).hex().encode()
def parse_http_headers(data: bytes) -> Mapping[str, str]:
return {
i[0].strip(): i[1].strip()
for i in (j.split(":", 1) for j in data.decode().strip().split("\r\n"))
}
def parse_http_response(res_line: bytes) -> Tuple[bytes, int, Optional[bytes]]:
http_ver, status_code_and_status = res_line.split(b" ", 1)
if b" " in status_code_and_status:
status_code, status = status_code_and_status.split(b" ", 1)
else:
status_code = status_code_and_status
status = None
return http_ver, int(status_code.decode()), status