fetch_data.py 561 B

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