金山快盘登陆签到

import urllib
import urllib2
import cookielib
import json
import re
import getpass

class Login_kp:
    def __init__(self):
        cj = cookielib.CookieJar()
        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        urllib2.install_opener(self.opener)
        self.opener.addheaders = [('User-agent', 'IE')]

    def login(self, username, password):
        url = 'https://www.kuaipan.cn/index.php?ac=account&op=login'
        data = urllib.urlencode({'username':username, 'userpwd':password})
        req = urllib2.Request(url, data)
        #print "First"
        try:
            fd = self.opener.open(req)
        except Exception, e:
            print(u'Http feedback wriong!')
            return False
        print fd.url   #用于调试,一开始的时候url写错了
        if fd.url != "http://www.kuaipan.cn/home.htm":
            print(u"coding error")
            return False

        print(u'%s Login ready to sign..   ' % username),
        return True

    def logout(self):
        url = 'http://www.kuaipan.cn/index.php?ac=account&op=logout'
        req = urllib2.Request(url)
        fd = self.opener.open(req)
        fd.close()
        
    def sign(self):
        url = 'http://www.kuaipan.cn/index.php?ac=common&op=usersign'
        req = urllib2.Request(url)
        fd = self.opener.open(req)
        sign_js = json.loads(fd.read())
        if sign_js['state'] == -102:
            print(u"already signed")
        elif sign_js['state'] == 1:
            print(u"sign successfully score:%d,total:%d;space getting:%dM\n" % (sign_js['increase'], sign_js['status']['points'], sign_js['rewardsize']))
        else:
            print(u"sign fail!")
        #print type(sign_js)
        fd.close()

if __name__ == '__main__':
    l = Login_kp()
    name = raw_input('Email Address: ')
    pwd = getpass.getpass("enter password for user %s: " % name)  #这个功能:输入密码不回显在windows下不能支持,但是在Linux可以支持
    if l.login(name, pwd) == False:
        exit(0)
    l.sign()

注:该代码来源于网络,做了些修改之后在IDLE和PyDev上运行成功,并尝试了使用getpass()函数来保护密码的输入

之前学过一段时间的Python,主要学习的是“Python 基础教程”,看了一些“Python Essential Reference”(感觉这本书较难,但是讲得很透彻),也看过一些“Dive into python”。感觉Python的学习主要还是库的学习。希望能够坚持学习Python,Python真的狠可爱

你可能感兴趣的:(金山)