Zerodha API Token & WebSocket Setup

Job ID: 39832528

Budget: ₹600 – ₹1,500 INR

I need a small, well-commented Python routine that does two things:

1. Generates a fresh Zerodha access token every time I launch the script (I already have the API key and secret).
2. Immediately opens a stable Kite WebSocket connection so I can stream live ticks for my trading-automation engine.

Use the official kiteconnect Python SDK—or a lightweight alternative if you can show the same reliability—and keep external dependencies minimal. The function should return both the active KiteConnect object and the WebSocket instance so I can plug them straight into my existing trading logic.

Please include:
• A short README or inline docstring that explains required environment variables, login flow, and how to refresh the token before expiry.
• Simple error-handling around network drops and token invalidation.
• A quick demo call that prints the first few tick packets to verify everything is working.

That’s all I need to get rolling; concise, reusable code over elaborate frameworks is what I’m after.