fetch_data.py 697 B

12345678910111213141516171819202122232425
  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. print(response.decode("utf-8")) # Debugging line
  18. return response.decode("utf-8")
  19. except Exception as e:
  20. return str(e)