For this to work without waiting until the stdin stream ends, you can iter on the readline. I think this is the simplest solution.
import sysk = 0try: for line in iter(sys.stdin.readline, b''): k = k + 1 print lineexcept KeyboardInterrupt: sys.stdout.flush() passprint k