Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7035597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:19:23+00:00 2026-05-28T01:19:23+00:00

I am using this command to disable echo and fetch user input using sys.stdin.read(1)

  • 0

I am using this command to disable echo and fetch user input using sys.stdin.read(1)

tty.setcbreak(sys.stdin.fileno())

However during the course of my program I need to again enable and disable console echo. I tried

fd = sys.stdin.fileno()
old = termios.tcgetattr(fd)
termios.tcsetattr(fd, termios.TCSADRAIN, old)

But that aint working. How can I elegantly enable echo?

ps: I am using code from Python nonblocking console input by mizipzor

Heres the code:

import sys
import select
import tty
import termios
import time

def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        return False

def calc_time(traw):
    tfactor = {
    's':    1,
    'm':    60,
    'h':    3600,
    }
    if is_number(g[:-1]):
        return float(g[:-1]) * tfactor.get(g[-1])
    else:
        return None   
def isData():
    return select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], [])

old_settings = termios.tcgetattr(sys.stdin)
try:
    tty.setcbreak(sys.stdin.fileno())
    i = 0
    while 1:
        print i
        i += 1
        time.sleep(.1)
        if isData():
            c = sys.stdin.read(1)
            if c:
                if c == 'p':
                    print """Paused. Use the Following commands now:
Hit 'n' to skip and continue with next link.
Hit '5s' or '3m' or '2h' to wait for 5 secs, 3 mins or 3 hours
Hit Enter to continue from here itself.
Hit Escape to quit this program"""
                    #expect these lines to enable echo back again
                    fd = sys.stdin.fileno()
                    old = termios.tcgetattr(fd)
                    old[3] = old[3] & termios.ECHO
                    termios.tcsetattr(fd, termios.TCSADRAIN, old)

                    g = raw_input("(ENABLE ECHO HERE):")
                    
                    if g == '\x1b':
                        print "Escaping..."
                        break
                    if g == 'n':
                        #log error
                        continue
                    elif g[-1] in ['s','m','h']:
                        tval = calc_time(g)
                        if tval is not None:
                            print "Waiting for %s seconds."%(tval)
                            time.sleep(tval)
                    continue

finally:
    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:19:23+00:00Added an answer on May 28, 2026 at 1:19 am

    Writing this:

    termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings)
    

    Instead of the above 4 lines solved it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I built wxWidgets on Linux using this command: ../configure --enable-shared --disable-debug It see results
I'm using GNU bash, version 3.00.15(1)-release (x86_64-redhat-linux-gnu). And this command: echo -e doesn't print
The output of this echo is not passed on to the next command using
I am using this command: cut -d: -f2 To sort and reedit text, Is
I have turned computer monitor off using this command SendMessage(f.Handle, WM_SYSCOMMAND, (IntPtr)SC_MONITORPOWER, (IntPtr)(turnOff ?
I'm following the Railscasts tutorial on using OpenID with AuthLogic . This command: $
If I run a command like this, using ruby's pty class, how do I
Currently I am using a post-build event command line similar to this one: xcopy
This popped-up while writing a script for vim. Using the well-documented ':normal' command: :normal
I have been executing this command to disable indexes in tables. How can I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.