2012-11-10 85 views
0

我有以下問題,當用戶在運行python腳本時,在命令窗口上一直按下「Enter key」...以下是在命令窗口上運行的內容,但用戶保留按enter直接輸入密碼,腳本失敗,我該如何防止?陳舊的輸入被輸入密碼

Parsing the XML 

Building the build combo table 

    **Password: //asks for password but user had multiple enter key presses above which is taken as passoword and fails** 

Python代碼: -

url='http://wiki.com/'+wikiName+'/w/index.php?title='+hId +'_'+rId+'&action=edit' 
cookiehand = urllib2.HTTPCookieProcessor() 
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm() 
password_mgr.add_password(user=getpass.getuser(),passwd=getpass.getpass(),uri=authhost,realm=realm) 
auth_handler = urllib2.HTTPBasicAuthHandler(password_mgr) 
opener = urllib2.build_opener(auth_handler, cookiehand) 
urllib2.install_opener(opener) 
# make the request 
req = urllib2.Request(url=url) 
try: 
    f = urllib2.urlopen(req) 
    txt = f.read() 
    f.close() 
except urllib2.HTTPError, e: 
    txt = '' 
    print 'An error occured connecting to the wiki. No wiki page will be generated.'//eventually gets this error 
+0

@Matti,感謝您指出 – user1795998

+0

任何人可以幫我解答一下請問? – user1795998

回答

0

你需要做的是驗證輸入。例如:

user = "" 
pass = "" 

while not user and not pass: 
    user = getpass.getuser() 
    passwd = getpass.getpass() 

請注意,如果允許使用空白密碼,這將不起作用。如果你有過將getpass庫控制,它可能會更有意義移動驗證這兩種方法中,也許會讓他們可選的像validate=False默認參數