PYthon自动登陆网站有关问题

PYthon自动登陆网站问题


import urllib,urllib2, cookielib, sys, time

cj = cookielib.LWPCookieJar()
cookie_support = urllib2.HTTPCookieProcessor(cj)
opener = urllib2.build_opener(cookie_support)
urllib2.install_opener(opener)
headers = {'User-Agent': 'Mozilla/5.0 (compatible; MISE 9.0; Windows NT 6.1); Trident/5.0'}

def login(username, password, checkcode):
    posturl = 'http://uat.shgt.com/account/api/login/'

    postdata = {'aciton': 'login', 'username': username, 'passward': password, 'captcha': checkcode}
    postdata = urllib.urlencode(postdata)
    req = urllib2.Request(posturl, postdata, headers)
    html = urllib2.urlopen(req).read()
    print cj
    print html


path = "E:/project/ver.jpg"               #下载验证码

def get_vaildate():
    rq = urllib2.Request('http://uat.shgt.com/account/api/login/', headers=headers)
    # h = urllib2.urlopen(rq)
    print cj
    url = r'http://uat.shgt.com/captcha.gif?t=' + str(time.time())
  
    urllib.urlretrieve(url, path)

get_vaildate()
from os import system
system('start ' + path)

value = raw_input()
login('xxxx', 'xxxx', value)


源码如上 为什么我每次登陆输入验证码都提示错误,下载验证码cookie问题吗?该如何修改
------解决方案--------------------
大概验证码和cookie相关联了,
你下载验证码是一次,再次登陆时用的不是同一个会话。
保险的做法是,请求一次登陆页面如index.jsp,提取出验证码图片地址并下载,然后用同一个会话把验证码密码用户名post到登陆页面如login.jsp即可

------解决方案--------------------
你的get_vaildate()  下载验证码 都没用到cookielib,改成这样试下:
outfile=open('validateCode.jpg', 'wb')
outfile.write(cookie_support.open(urllib2.Request(url)).read())
outfile.close()