Spaces:
Running
Running
| from fastapi import FastAPI, Request | |
| import requests | |
| import time | |
| app = FastAPI() | |
| def callback_sender(sender_url: str): | |
| """Callback function to periodically call registered senders""" | |
| response_ok = True | |
| try: | |
| response = requests.get(sender_url, params={'callback': 'true', 'timestamp': time.time()}) | |
| print(f"Callback to {sender_url}: {response.status_code}") | |
| except Exception as e: | |
| print(f"Callback to {sender_url} failed: {e}") | |
| response_ok = False | |
| return response_ok | |
| async def receive_hc(request: Request, timestamp: float, host_url: str): | |
| response = False | |
| # calling the sender back | |
| if host_url: | |
| print(f"Calling back: {host_url}") | |
| response = callback_sender(host_url) | |
| # Print received data | |
| status = 'OK' if response else 'KO' | |
| url = str(request.url) | |
| original_url = url.split('?')[0] | |
| print(f"URL: {original_url}") | |
| print(f"Sender Host URL: {host_url}") | |
| print(f"Timestamp: {timestamp}") | |
| print(f"status: {status}") | |
| print("-" * 40) | |
| return {"status": status} | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run(app, host="0.0.0.0", port=8000) |