# -*- coding: utf-8 -*- """ Created on Wed May 3 16:26:55 2017 @author: x-power """ import requests import http.cookiejar as cookielib import re import time import os.path from PIL import Image import hashlib headers = { "Host": "211.84.144.42", "Referer": "http://211.84.144.42/jwweb/_data/login_new.aspx", 'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0', 'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language':'en-US,en;q=0.5', 'Accept-Encoding':'gzip, deflate', 'Content_Type':'application/x-www-form-urlencoded', # 'Content-length':'5523', #'Cookie':'name=value; ASP.NET_SessionId=hgyga345qcutdv55rejc3x45; ASP.NET_SessionId_NS_Sig=oenCV6mdwWlp9VG_', 'Connection':'keep-alive', 'Upgrade-Insecure-Requests':'1', } session = requests.session() session.cookies = cookielib.LWPCookieJar(filename='cookies') try: session.cookies.load(ignore_discard=True) # 如果已经有 cookie信息的话 直接用于登录 except: print("Cookie 未能加载") #userid = input("userid: ") #password = input("password: ") userid = '1313132121' password = '13213132' def qg_fgfg(validateCode): # Encryption authentication code code = validateCode.upper() m2 = hashlib.md5() m2.update(code.encode(encoding='gb2312')) s4 = m2.hexdigest() s5 = s4[0:30].upper() + '10481' m3 = hashlib.md5() m3.update(s5.encode(encoding='gb2312')) s6 = m3.hexdigest() s7 = s6[0:30].upper() return s7 def qg_dsds(userid,password): # Encrypt the user name and password m = hashlib.md5() m.update(password.encode(encoding='gb2312')) psw = m.hexdigest() s1 = userid + psw[0:30].upper() + '10481' m1 = hashlib.md5() m1.update(s1.encode(encoding='gb2312')) s2 = m1.hexdigest() s3 = s2[0:30].upper() return s3 def get_captcha(): captcha_url = 'http://211.84.144.42/jwweb/sys/ValidateCode.aspx' r = session.get(captcha_url, headers=headers) with open('captcha.jpg', 'wb') as f: f.write(r.content) f.close() im = Image.open('captcha.jpg') im.show() im.close() captcha = input("please input the captcha >") return captcha captcha = get_captcha() postdata = { '_VIEWSTATE': 'd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Type':'STU', 'txt_asmcdefsddsd':userid, 'txt_pewerwedsdfsdff':password, 'txt_sdertfgsadscxcadsads':captcha, } #def login(): post_url ='http://211.84.144.42/jwweb/_data/login_new.aspx' login_page = session.post(post_url, data=postdata, headers=headers) session.cookies.save()