fetch_data.py 639 B

123456789101112131415161718192021222324
  1. import os
  2. import socket
  3. from dotenv import load_dotenv
  4. load_dotenv()
  5. SQUID_HOST = os.getenv("SQUID_HOST", "127.0.0.1")
  6. SQUID_PORT = int(os.getenv("SQUID_PORT", 3128))
  7. def fetch_squid_data():
  8. try:
  9. with socket.create_connection((SQUID_HOST, SQUID_PORT), timeout=5) as s:
  10. request = (
  11. f"GET cache_object://{SQUID_HOST}/active_requests HTTP/1.0\r\n\r\n"
  12. )
  13. s.sendall(request.encode())
  14. response = b""
  15. while chunk := s.recv(4096):
  16. response += chunk
  17. return response.decode("utf-8")
  18. except Exception as e:
  19. return str(e)