python-django 實現商職學院官網實名認證demo
阿新 • • 發佈:2018-09-06
mage 所有 shel django all tor window 動態 ()
python-django 實現商職學院官網實名認證demo
版權聲明:未經授權嚴禁轉載!
ESchool 大學生校園交易平臺 實名認證測試代碼:
# """ 實名認證 """ def renzheng(request): name = request.session.get(‘username‘) c = models.User.objects.filter(username=name, netname="0") # 驗證用戶是否登錄 if name and c: n = models.User.objects.filter(username=name)[0].realnameif n: # 已經登錄 return render(request, "text.html", {"title": "實名認證通知", "name": "ESchool大學生校園交易平臺提醒您", "text": "您在此之前已完成實名認證,無需再次認證!"}) # 導包 import os import requests import os import shelve from lxml importetree #DstDir = os.getcwd() + "\\static\\yzm\\" #設置驗證碼存儲路徑 DstDir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))+"/static/yzm/" if request.method == ‘POST‘: username = request.POST.get(‘name‘) if models.User.objects.filter(studentnum=username):# 請求商職學院官網 s = requests.session() imgUrl = "http://211.64.112.16:8080/CheckCode.aspx?" imgresponse = s.get(imgUrl, stream=True) image = imgresponse.content img_name = str(time.time()) try: # 存儲爬取的驗證碼圖片 with open(DstDir + "code" + img_name + ".jpg", "wb") as jpg: jpg.write(image) except IOError: print("IO Error\n") finally: jpg.close # 動態創建文件 file = shelve.open(DstDir + "code" + img_name + ".dat") data = {‘s‘: s} # 存儲第一次請求對象 file[‘my_s‘] = data file.close() return render(request, ‘renzheng.html‘, {‘username‘: name, ‘s‘: s, ‘img_name‘: img_name, ‘text‘: ‘該用戶已被其他賬號實名認證!‘}) password = request.POST.get(‘pnamwd‘) yan = request.POST.get(‘yanzhengma‘) img_name = request.POST.get(‘img_name‘) url = "http://211.64.112.16:8080/default2.aspx" file = shelve.open(DstDir + "code" + img_name + ".dat") s = file[‘my_s‘][‘s‘] file.close() response = s.get(url) selector = etree.HTML(response.content) __VIEWSTATE = selector.xpath(‘//*[@id="form1"]/input/@value‘)[0] data = { "__VIEWSTATE": __VIEWSTATE, "txtUserName": username, "TextBox2": password, "txtSecretCode": yan, "Button1": "", } # 提交表頭,裏面的參數是電腦各瀏覽器的信息。模擬成是瀏覽器去訪問網頁。 headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.110 Safari/537.36", } # 登陸教務系統 response = s.post(url, data=data, headers=headers) content = response.content.decode(‘gb2312‘) # 網頁源碼是gb2312要先解碼 selector = etree.HTML(content) try: infor = selector.xpath(‘//*[@id="xhxm"]/text()‘)[0] except: os.remove(DstDir + "code" + img_name + ".dat") os.remove(DstDir + "code" + img_name + ".jpg") return render(request, ‘renzheng.html‘, {‘username‘: name, ‘s‘: s, ‘img_name‘: img_name, ‘text‘: ‘認證失敗!‘}) text = infor.replace(" ", "").replace(‘同學‘, ‘‘) os.remove(DstDir + "code" + img_name + ".dat") os.remove(DstDir + "code" + img_name + ".jpg") models.User.objects.filter(username=name).update(realname=text, studentnum=username, studentpwd=password, rtime=time.strftime(‘%Y-%m-%d %X‘)) return render(request, "text.html", {"title": "實名認證通知", "name": text + "同學,ESchool大學生交易平臺 實名認證成功通知", "text": text + " 同學 您好,您於 " + time.strftime( ‘%Y-%m-%d %X‘) + " 完成 ESchool系統實名認證,您現在開始有權使用ESchool所有免費功能並為您提供優質服務,ESchool將會盡全力維護您的合法權益,願您使用愉快!(最終解釋權歸ESchool所有)"}) else: s = requests.session() imgUrl = "http://211.64.112.16:8080/CheckCode.aspx?" imgresponse = s.get(imgUrl, stream=True) image = imgresponse.content img_name = str(time.time()) try: with open(DstDir + "code" + img_name + ".jpg", "wb") as jpg: jpg.write(image) except IOError: print("IO Error\n") finally: jpg.close file = shelve.open(DstDir + "code" + img_name + ".dat") data = {‘s‘: s} file[‘my_s‘] = data file.close() return render(request, ‘renzheng.html‘, {‘username‘: name, ‘s‘: s, ‘img_name‘: img_name}) else: return redirect(‘/login.html‘)
python-django 實現商職學院官網實名認證demo