count = 0
while count<3:
name = input('请输入账号:')
password = input('请输入密码:')
if not name or not password:
print('账号密码不能为空')
right_name = 'admin'
right_password = '123xyz'
if name == right_name:
if password == right_password:
print('欢迎{}登录'.format(name))
break
else:
print('用户名密码错误')
else:
print('用户名密码错误')
count += 1
# -*- coding: utf-8 -*-errortimes=0
while errortimes<3:
user=raw_input("user:")
password=raw_input("password:")
if user=='123' and password=='456':
#正确后允许进入并退出循环
print "enter"
break
else:
#错误时,出错计数加1
print "Error , enter again:"
errortimes+=1
else:
#错误达到三次,结束循环
print "You are not allowed to enter!"
给你写个简单的例子你看看。跟你写的差别不大,要实现输入三次失败就禁止进入,只需要加一个计数的变量就行了,这里是errortimes。当error times达到三次,就中值循环。灵活借鉴,不要生搬硬套。
再说你写的问题:
if username = bizhenwei:
这句是比较的,应该用 == 而不是 = 也即if username == bizhenwei:
else:
print"repeat!"
这里print前缺少缩进。print后缺少空格。
如果是用Basic Auth 的话,可以参考这段代码import urllib2, base64
request = urllib2.Request(url) base64string = base64.encodestring('%s:%s' % (user,password))[:-1] request.add_header("Authorization", "Basic %s" % base64string)
htmlFile = urllib2.urlopen(request) htmlData = htmlFile.read() print htmlData htmlFile.close()
On 4/10/07, Shuning Hong [email protected]>wrote:
这是我的做法:
params='MainID=C&SubID=1'
authstr='Basic '+base64.encodestring(routeruser+':'+routerpass)
authstr=authstr[:-1] # delete last /n
headers={'Authorization':authstr}
On 4/10/07, 蒋辉 [email protected]>wrote:
我是电信光纤宽带用户,用web 认证不支持多台机器上网。参考了网上的资料,想 写一个 python 的小程序,可以自动获取宽带路由器的外网IP,自动发送认证信 息。宽带路由器登录时会要用户名密码,否则就连不上,我通过使用嗅探器抓包把 认证过的'Authorization' 值取出放在我的程序里能连上路由器,但通用性太差。 有什么好方法可以解决吗?
我的小程序如下: #ptyhon import urllib import httplib params = urllib.urlencode({}) headers = {'Accept': 'text/html', 'User-Agent': 'Mozilla','Content-Type': 'application/x-www-form-urlencoded','Authorization': 'Basic amh1aTpqaHVpMTIzNA=='} conn = httplib.HTTPConnection("192.168.1.1") #print headers conn.request("GET","/userRpm/StatusRpm.htm",params,headers) #id (conn) r1 = conn.getresponse() a = r1.read() #print a b = a.find("222") #print b c = a.find("</td",b) #print c d = a[b:c] print d
params = urllib.urlencode({'connectname': '', 'consumeright': 0, 'separatecard': 0, 'needActiveX' : 1, 'clienttype' :1, 'cookiedate' :3650, 'localip': d, 'httpIP' :d, 'isPNP':0, 'username': "njkd107089173", 'password': "1976"})
headers = {'Accept': 'text/html', 'User-Agent': 'Mozilla', 'Content-Type': 'application/x-www-form-urlencoded'}
server = '218.2.135.36' path = '/secu/webLogin.jsp'
conn = httplib.HTTPConnection(server) conn.request("POST", path, params, headers) r1 = conn.getresponse() print r1.status, r1.reason data1 = r1.read() print data1 conn.close()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)