Python模拟Web Fetion登录解析

这个貌似没啥好说的我是参照两篇博客学的:

http://miaoo.in/python-fetion.html

http://www.cnblogs.com/xiaoxia/archive/2010/08/04/1792461.html

看看这两篇博客就差不多了,但是Fetion的版本更新了,原来的URL用不了了,我换成了新的

另外提供一个挺不错的登录飞信代码,但是也是版本过低了,不可以用了吧,我没仔细看

网址:http://code.google.com/p/pytool/source/browse/trunk/PyFetion/fetion.py?spec=svn79&r=79


下面就直接上代码吧:(Python 2.7)

#-*-coding:utf-8-*-

import urllib
import urllib2
import cookielib

class PyFetion:
    def __init__(self,fetion_name,fetion_psd,fetion_url):
        self.fetion_name = fetion_name
        self.fetion_psd = fetion_psd
        self.fetion_url = fetion_url
        self.operate = ""
        self.cj = cookielib.LWPCookieJar()
        try:
            self.cj.revert("fetion.cookie")
        except Exception,e:
            print e
        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
        urllib2.install_opener(self.opener)
        
    def fetionLogin(self):
        params = {'m':self.fetion_name,'pass':self.fetion_psd}
        data = urllib.urlencode(params)
        url = "http://f.10086.cn/im5/login/loginHtml5.action?t=40645775874802847"
        req = urllib2.Request(url,data)
        self.operate = self.opener.open(req)
        url_out = "http://f.10086.cn/im5/index/logoutsubmit.action"
        if self.operate.geturl() != url_out:
            print "Login Successfully"
            self.cj.save("fetion.cookie")
            return True
        else:
            print "Login Failed"
            return False
    
    def fetionLogout(self):
        url_logout = "http://f.10086.cn:80/im5/index/logoutsubmit.action"
        logout = urllib2.Request(url_logout)
        response = urllib2.urlopen(logout)
    
    def sendMsg(self,msg,touserid):
        url_msg = 'http://f.10086.cn/im5/chat/sendHtml5Msg.action?touserid=' + touserid + '&html5Url=toChat'
        sendmsg = urllib2.Request(url_msg, urllib.urlencode({'msg':msg}))
        finish = urllib2.urlopen(sendmsg)
        if finish.geturl == url_msg:
            print 'Send Failed!'
        else:
            print 'Send Successfully'


你可能感兴趣的:(exception,Web,python,html5,url,login)