Reputation: 5127
I am having the following problem where the user keeps pressing "enter key" on the command window when the python script is running...following is what runs on the command window,but user keeps pressing enter which eventually gets fed into password and the script fails,how do I prevent this?
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 code:-
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
Upvotes: 2
Views: 137
Reputation: 23322
Kosklain's answer should set you on the right track. However, by the way you phrased your question, it seems you aren't aware of the process which is causing this behaviour, so here's some more insight into what's happening:
This situation occurs because the system buffers (saves in memory) your whole input stream, even when you're not actually requesting input. When (assuming) you do raw_input()
, all it does is read sys.stdin until it finds a newline. Obviously, if there's a newline before your recently introduced line of input, it stops there.
Thus, the solution to your problem is the following:
while there_is_a_new_line():
line= raw_input()
The there_is_a_new_line() function, however, turns out to be a bit involved. The existing solutions involve changing your terminal's attributes, and are not cross-platform. See kosklain's link for details.
Upvotes: 1
Reputation: 399
I think you should try flushing the user input just before you read the password.
In order to do so, having a look at this other post might help.
Upvotes: 2