def login(website=None):
global websiteObject
global acc_manager
if website is None and acc_manager.account is not None:
website, username, password = acc_manager.get_account(acc_manager.account)
websiteObject = websiteObject.factoryMethod(website)
else:
username = input("Enter your username: ")
try:
password = getpass.getpass("Enter your password: ")
except getpass.GetPassWarning:
print("Your system is not allowing us to disable echo. We cannot read your password")
return
if website is None and websiteObject is None:
website = input("Enter website: ")
websiteObject = SessionAPI.SessionAPI().factoryMethod(website)
elif websiteObject is None:
websiteObject = SessionAPI.SessionAPI().factoryMethod(website)
websiteObject.login(username, password)
acc_manager.insert(website, username, password)
if websiteObject.logged_in:
print("Successful Login")
else:
print("Login Failed")
python类GetPassWarning()的实例源码
def insacc():
global acc_manager
website = input("Enter Website: ")
username = input("Enter username: ")
try:
password = getpass.getpass("Enter your password: ")
except getpass.GetPassWarning:
print("Your system is not allowing us to disable echo. We cannot read your password")
return
acc_manager.insert(website, username, password)
def fix_get_pass():
try:
import getpass
except ImportError:
return #If we can't import it, we can't fix it
import warnings
fallback = getattr(getpass, 'fallback_getpass', None) # >= 2.6
if not fallback:
fallback = getpass.default_getpass # <= 2.5
getpass.getpass = fallback
if hasattr(getpass, 'GetPassWarning'):
warnings.simplefilter("ignore", category=getpass.GetPassWarning)
def fix_getpass():
try:
import getpass
except ImportError:
return #If we can't import it, we can't fix it
import warnings
fallback = getattr(getpass, 'fallback_getpass', None) # >= 2.6
if not fallback:
fallback = getpass.default_getpass # <= 2.5 @UndefinedVariable
getpass.getpass = fallback
if hasattr(getpass, 'GetPassWarning'):
warnings.simplefilter("ignore", category=getpass.GetPassWarning)
def test_falls_back_to_stdin(self):
with mock.patch('os.open') as os_open, \
mock.patch('sys.stdin', spec=StringIO) as stdin:
os_open.side_effect = IOError
stdin.fileno.side_effect = AttributeError
with support.captured_stderr() as stderr:
with self.assertWarns(getpass.GetPassWarning):
getpass.unix_getpass()
stdin.readline.assert_called_once_with()
self.assertIn('Warning', stderr.getvalue())
self.assertIn('Password:', stderr.getvalue())
def test_falls_back_to_stdin(self):
with mock.patch('os.open') as os_open, \
mock.patch('sys.stdin', spec=StringIO) as stdin:
os_open.side_effect = IOError
stdin.fileno.side_effect = AttributeError
with support.captured_stderr() as stderr:
with self.assertWarns(getpass.GetPassWarning):
getpass.unix_getpass()
stdin.readline.assert_called_once_with()
self.assertIn('Warning', stderr.getvalue())
self.assertIn('Password:', stderr.getvalue())
def login_in_terminal(self, username=None, password=None,
use_getpass=True, captcha_filename=None):
"""
?????????????????????????
???? username ? password ?????????????
.. note:: ???????????????????
:param str|unicode username: ???????
:param str|unicode password: ???
:param bool use_getpass: ?????????????????????????? True?
????????? Windows ??? IDE ?????????????getpass ???
????????? False ??????new in version > 0.0.16?
:param str|unicode captcha_filename: ??????????
?????????????????????????????
:return: .. seealso:: :meth:`.login`
"""
print('----- Zhihu OAuth Login -----')
print('?????????????????? +86')
username = username or input('email/phone: ')
if password is None:
if use_getpass:
with warnings.catch_warnings():
warnings.simplefilter('ignore', getpass.GetPassWarning)
password = getpass.getpass(str('password: '))
else:
password = input('password: ')
try:
success, reason = self.login(username, password)
except NeedCaptchaException:
print('Need for a captcha, getting it......')
captcha_image = self.get_captcha()
captcha_filename = captcha_filename or DEFAULT_CAPTCHA_FILENAME
with open(captcha_filename, 'wb') as f:
f.write(captcha_image)
print('Please open {0} for captcha'.format(
os.path.abspath(captcha_filename)))
captcha = input('captcha: ')
os.remove(os.path.abspath(captcha_filename))
success, reason = self.login(username, password, captcha)
if success:
print('Login success.')
else:
print('Login failed, reason: {}'.format(reason))
return success, reason