Guest User

Untitled

a guest
Jan 3rd, 2025
708
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. import sys
  2. import asyncio
  3.  
  4. async def read_stdin():
  5.     reader = asyncio.StreamReader(sys.stdin)
  6.     while True:
  7.         try:
  8.             line = await reader.readline()
  9.             if not line:  # EOF
  10.                 break
  11.            
  12.             # Remove trailing newline
  13.             line = line.decode().rstrip()
  14.            
  15.             # Process the line here
  16.             print(f"Received: {line}")
  17.            
  18.         except Exception as e:
  19.             print(f"Error reading stdin: {e}")
  20.             break
  21.  
  22. async def main():
  23.     await read_stdin()
  24.  
  25. # Run the event loop
  26. try:
  27.     asyncio.run(main())
  28. except KeyboardInterrupt:
  29.     print("\nProgram terminated by user")
Advertisement
Add Comment
Please, Sign In to add comment