0

Pythonスクリプトの実行中にユーザーがコマンドウィンドウで「Enterキー」を押し続けるという次の問題があります...以下はコマンドウィンドウで実行されるものですが、ユーザーは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
4

1 に答える 1

0

あなたがする必要があるのは、入力を検証することです。このようなもの:

user = ""
pass = ""

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

空のパスワードが許可されている場合、これは機能しないことに注意してください。getpass ライブラリを制御できる場合は、検証をこれら 2 つのメソッド内に移動し、おそらく次のようなデフォルトの引数でオプションにする方が理にかなっています。validate=False

于 2012-12-13T00:25:48.120 に答える