python有办法暂时关闭输入吗

wy_ei 2014-10-08 08:07:29
我的一段程序需要输入密码,多次错误就会延时在次输入。延时的时候我使用了time.sleep(sec) 这样的方法,但是发现在这个时候还是可以按键盘输入的 只是不能被显示罢了,在延时之后,他就会被再次读入,当然密码是输入是错误的,所以他又会继续延时。我考虑这是一个bug,如果输入个一百次一千次,其中有一次恰好输入正确了。第二天再来看程序,发现已经进去了。所以想问问有没有什么方法可以把,输入定向到其他地方,或者直接丢弃
...全文
357 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
wy_ei 2014-10-18
  • 打赏
  • 举报
回复
引用 15 楼 micropentium6 的回复:
[quote=引用 11 楼 angel_su 的回复:] 貌似os.system('pause')一下可以清除先前的输入...
but it only works on Windows system, does it? You would think that python could offer cross-platform capability here... take a look at python/Modules/posixmodule.c and didn't find anything. It appears python simply passes it through to whatever implementation on the OS level to handle the arguments sent to the C STD call "system"... Don't know why pause under windows could clean up the console input queue either... But I guess he doesn't care...[/quote] thanks for your patient answer.you let me know when we do program we should consider the cross-platform capability .I think I should take some time to learn another OS.
  • 打赏
  • 举报
回复
引用 11 楼 angel_su 的回复:
貌似os.system('pause')一下可以清除先前的输入...
but it only works on Windows system, does it? You would think that python could offer cross-platform capability here... take a look at python/Modules/posixmodule.c and didn't find anything. It appears python simply passes it through to whatever implementation on the OS level to handle the arguments sent to the C STD call "system"... Don't know why pause under windows could clean up the console input queue either... But I guess he doesn't care...
wy_ei 2014-10-11
  • 打赏
  • 举报
回复
引用 11 楼 angel_su 的回复:
貌似os.system('pause')一下可以清除先前的输入...
确实是这样,可以用他来解决问题。多谢。
wy_ei 2014-10-11
  • 打赏
  • 举报
回复
当我多次输错以后,显示让我等待,但是我依旧多次输入,时间到了以后,按以前的情况会再次被raw_input读入,这下不会了。 代码中我添加了os.system('pause')在输入的前面
while right!=1:
				os.system('pause')
				pswd1=raw_input("please enter password:")
不过最后又出现了,请按任意键继续. . .。这个是不是又要用到输出重定向呀
wy_ei 2014-10-11
  • 打赏
  • 举报
回复
这个问题貌似很有意思,下面是我的代码,帮忙看看
right=0
			while right!=1:
				pswd1=raw_input("please enter password:")
				pswd=self.getPasswd()   
				if pswd1==pswd:
					right=1
					self.setWrongTimes(0)
				else:
					self.setWrongTime(time.time())  #record the  time when wrong input occur 
					self.wrongTimes+=1 
					print "password wrong!"
					self.setWrongTimes(self.wrongTimes)		# set wrongs times	
					self.WiatTime()       # wiat
	def WiatTime(self):   #according the password input wrong times set a wait time,at this time you can't enter password
		times=self.getWrongTimes()    #wrong times
		wrongTime =self.getWrongTime()    #the time last wrong input accour
		if times <= 3:					
			pass
		if times >3:
			currentTime=long(time.time())
			waitTime=currentTime-wrongTime
			if waitTime>times*times:
				pass
			else:
				timeYouShouldWait=times*times-(currentTime-wrongTime)
				print "you should wait for some sec:   ",
				for i in range(timeYouShouldWait):
					time.sleep(1)
					theTimeLave=timeYouShouldWait-i
					if theTimeLave<10:
						print  "\b\b\b",str(theTimeLave),	
					elif theTimeLave<100:
						print "\b\b\b\b",str(theTimeLave),
					elif theTimeLave<1000:
						print "\b\b\b\b\b\b",str(theTimeLave),
		print 
11楼的方法是可以的。下楼上试验结果。
angel_su 2014-10-10
  • 打赏
  • 举报
回复
貌似os.system('pause')一下可以清除先前的输入...
The_Third_Wave 2014-10-10
  • 打赏
  • 举报
回复
mark
  • 打赏
  • 举报
回复
引用 6 楼 panghuhu250 的回复:
[quote=引用 5 楼 micropentium6 的回复:] [quote=引用 3 楼 panghuhu250 的回复:] 这个问题不容易解决, 可以参考这个:http://stackoverflow.com/questions/2520893/how-to-flush-the-input-stream-in-python 改变stdin的办法是不行的. stdin指向os.devnull后, keyboard的输入依然输入到oldf, 等到stdin转回oldf时, 依然会有残存的输入存在. 从应用的角度看, 几次密码不对, 程序直接退出也是一个办法.
Then, if python exactly relies on C stdin, there is no function available to flush stdin buffer. However, you could consume those unnecessary characters by: While True: c=sys.stdin.read(1) if s=='\n' or c==None: break See C FAQ 12.26b[/quote] 问题是消耗光已有的输入后,read不会返回None, 而是会被阻塞. 另外, c=='\n'就推出循环的话, 只能消耗掉一行.[/quote] please discard all my previous comments here. I totally misunderstood the question and offered wrong direction and my understanding to the stdin/stdout buffer was also wrong which is actually not related to this question at all... I don't know windows, but for unix/linux, we are actually talking about tty device here. So the right interpretation of this question is: how could we pause tty input or flush tty input queue? man 3 tcflush

#include <unistd.h>
#include <stdio.h>
#include <termios.h>

void main(void)
{
    char c;
    sleep(5);
    printf("wake up\n");
    tcflush(0,TCIFLUSH);
    while((c=getchar())!='\n'&&c!=EOF)putchar(c);
    return;
}
so, the similar codes in python is in the url panghuhu250 offered.

import time
import subprocess
import sys
from termios import tcflush, TCIOFLUSH

alarm1 = int(raw_input("How many minutes (alarm1)? "))

while (1):
    time.sleep(60*alarm1)
    print "Alarm1"
    sys.stdout.flush();
    tcflush(sys.stdin, TCIOFLUSH)
    doit = raw_input("Continue (Y/N)?[Y]: ")
    print "Input",doit
    if doit == 'N' or doit=='n':
        print "Exiting....."
        break
Again, I apologize for all the confusion I brought over here and will pay more attention on my post...
  • 打赏
  • 举报
回复
引用 6 楼 panghuhu250 的回复:
[quote=引用 5 楼 micropentium6 的回复:] [quote=引用 3 楼 panghuhu250 的回复:] 这个问题不容易解决, 可以参考这个:http://stackoverflow.com/questions/2520893/how-to-flush-the-input-stream-in-python 改变stdin的办法是不行的. stdin指向os.devnull后, keyboard的输入依然输入到oldf, 等到stdin转回oldf时, 依然会有残存的输入存在. 从应用的角度看, 几次密码不对, 程序直接退出也是一个办法.
Then, if python exactly relies on C stdin, there is no function available to flush stdin buffer. However, you could consume those unnecessary characters by: While True: c=sys.stdin.read(1) if s=='\n' or c==None: break See C FAQ 12.26b[/quote] 问题是消耗光已有的输入后,read不会返回None, 而是会被阻塞. 另外, c=='\n'就推出循环的话, 只能消耗掉一行.[/quote] that's interesting, if python read from stdin can't catch EOF... As for multiple lines situation, is stdin line buffered? So, if we redirect it to /dev/null before we execute the above clean up buffer routine, we should expect to deal with only one line in the buffer...
jeky_zhang2013 2014-10-09
  • 打赏
  • 举报
回复
设置标志符,当多次输错后,不进行认证,并清空输入缓存
panghuhu250 2014-10-09
  • 打赏
  • 举报
回复
引用 5 楼 micropentium6 的回复:
[quote=引用 3 楼 panghuhu250 的回复:] 这个问题不容易解决, 可以参考这个:http://stackoverflow.com/questions/2520893/how-to-flush-the-input-stream-in-python 改变stdin的办法是不行的. stdin指向os.devnull后, keyboard的输入依然输入到oldf, 等到stdin转回oldf时, 依然会有残存的输入存在. 从应用的角度看, 几次密码不对, 程序直接退出也是一个办法.
Then, if python exactly relies on C stdin, there is no function available to flush stdin buffer. However, you could consume those unnecessary characters by: While True: c=sys.stdin.read(1) if s=='\n' or c==None: break See C FAQ 12.26b[/quote] 问题是消耗光已有的输入后,read不会返回None, 而是会被阻塞. 另外, c=='\n'就推出循环的话, 只能消耗掉一行.
  • 打赏
  • 举报
回复
如果每次都使用新的缓冲区呢?
  • 打赏
  • 举报
回复
引用 3 楼 panghuhu250 的回复:
这个问题不容易解决, 可以参考这个:http://stackoverflow.com/questions/2520893/how-to-flush-the-input-stream-in-python 改变stdin的办法是不行的. stdin指向os.devnull后, keyboard的输入依然输入到oldf, 等到stdin转回oldf时, 依然会有残存的输入存在. 从应用的角度看, 几次密码不对, 程序直接退出也是一个办法.
Then, if python exactly relies on C stdin, there is no function available to flush stdin buffer. However, you could consume those unnecessary characters by: While True: c=sys.stdin.read(1) if s=='\n' or c==None: break See C FAQ 12.26b
panghuhu250 2014-10-09
  • 打赏
  • 举报
回复
这个问题不容易解决, 可以参考这个:http://stackoverflow.com/questions/2520893/how-to-flush-the-input-stream-in-python 改变stdin的办法是不行的. stdin指向os.devnull后, keyboard的输入依然输入到oldf, 等到stdin转回oldf时, 依然会有残存的输入存在. 从应用的角度看, 几次密码不对, 程序直接退出也是一个办法.
  • 打赏
  • 举报
回复
1L正解, 每次接受输入判断失败后,都先重定向到一个新的缓冲区, 等到sleep结束要再次接受输入时再重定向回原缓冲区
  • 打赏
  • 举报
回复
if you'd like to redirect stdin, here it is:

import os

oldf=sys.stdin
f=open(os.devnull, 'r')
sys.stdin=f

37,719

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧