Python的好处,就是类库超多,多到只有你想不到的库,而没有你想到的,他却没有的库。所以飞信,在Python下也有一个开源的类库,这个就是PyFetion,他自己有带一个Demo,不过是一个CGI的程序,没有窗口界面,于是用之前Delphi写的DxVcl为这个飞信库实现了一个简单的界面GUI。代码很简单,就是两个窗口,一个窗口是验证码输入的窗口,还有一个是主窗口。界面信息:
主代码如下:
class
SeriForm(Form):
def __init__ (self,Owner):
self.Caption = ' 请输入验证码 '
self.Position = 5
self.BorderStyle = 3
self.Width = 275
self.Height = 162
self.lbl = Label(self)
self.lbl.SetProps(Parent = self,Caption = ' 请输入验证码 ' )
self.lbl.SetBounds( 24 , 16 , 72 , 13 )
self.EdtNum = Edit(self)
self.EdtNum.Parent = self
self.EdtNum.SetBounds( 102 , 11 , 139 , 21 )
self.Img = Image(self)
self.Img.Parent = self
self.Img.SetBounds( 24 , 35 , 217 , 59 )
self.Img.Center = True
self.Img.Picture.LoadFromFile( ' fetion_verify.jpg ' )
self.BtnOk = Button(self)
self.BtnOk.SetProps(Parent = self,Caption = ' 确定 ' )
self.BtnOk.SetBounds( 24 , 100 , 75 , 25 )
self.BtnOk.OnClick = self.BtnOkClick
self.BtnCancel = Button(self)
self.BtnCancel.SetProps(Parent = self,Caption = ' 取消 ' )
self.BtnCancel.SetBounds( 166 , 100 , 75 , 25 )
self.BtnCancel.OnClick = self.BtnCancelClick
def BtnCancelClick(self,Sender):
self.Close()
def BtnOkClick(self,Sender):
self.ModalResult = mrok
def GetSeriCode(self,picFile):
""" picFile 验证码图片 """
SeriFrm = SeriForm(None)
if SeriFrm.ShowModal() == mrok:
ret = SeriFrm.EdtNum.Text
else :
ret = ''
SeriFrm.Free()
return ret
class MainForm(Form):
def __init__ (self,Owner):
self.SetProps(Width = 492 ,Height = 401 ,BorderStyle = 3 )
self.lbUser = Label(self)
self.lbUser.SetProps(Parent = self,Caption = ' 用户 ' )
self.lbUser.SetBounds( 16 , 8 , 24 , 13 )
self.EdtUser = Edit(self)
self.EdtUser.Parent = self
self.EdtUser.SetBounds( 55 , 4 , 121 , 21 )
self.lbl = Label(self)
self.lbl.SetProps(Parent = self,Caption = ' 密码 ' )
self.lbl.SetBounds( 192 , 8 , 24 , 13 )
self.EdtPwd = Edit(self)
self.EdtPwd.SetProps(Parent = self,PasswordChar = ' * ' )
self.EdtPwd.SetBounds( 234 , 4 , 121 , 21 )
self.lbl1 = Label(self)
self.lbl1.SetProps(Parent = self,Caption = ' 好友列表 ' )
self.lbl1.SetBounds( 8 , 27 , 48 , 13 )
self.FriendList = ListBox(self)
self.FriendList.Parent = self
self.FriendList.SetBounds( 8 , 47 , 137 , 314 )
self.Memo1 = Memo(self)
self.Memo1.Parent = self
self.Memo1.SetBounds( 151 , 47 , 325 , 185 )
self.Memo2 = Memo(self)
self.Memo2.Parent = self
self.Memo2.SetBounds( 151 , 238 , 325 , 87 )
self.BtnSend = Button(self)
self.BtnSend.SetProps(Parent = self,Caption = ' 发送 ' )
self.BtnSend.SetBounds( 401 , 331 , 75 , 25 )
self.BtnSend.OnClick = self.BtnSendClick
self.BtnLog = Button(self)
self.BtnLog.SetProps(Parent = self,Caption = ' 登录 ' )
self.BtnLog.SetBounds( 361 , 3 , 75 , 25 )
self.BtnLog.OnClick = self.BtnLogClick
self.Phone = PyFetion( '' , '' , ' TCP ' )
self.threads = []
def BtnSendClick(self,Sender):
if self.Phone and self.Phone.alive:
if self.Phone.send_sms(toUTF8(self.Memo2.Lines.Text)):
self.Memo1.Lines.Add( ' 给自己发送短信息成功,目前只写了给自己发送信息 ' )
else :
ShowMessage( ' 无效的登录 ' )
def BtnLogClick(self,Sender):
if self.BtnLog.Caption == ' 登出 ' :
self.Phone.logout()
self.BtnLog.Caption = ' 登录 '
self.FriendList.Items.Clear()
return 1
self.Phone.mobile_no = self.EdtUser.Text
self.Phone.passwd = self.EdtPwd.Text
try :
ret = self.Phone.login(FetionOnline)
except PyFetionSupportError,e:
ShowMessage( ' 手机号未开通飞信 ' )
return 1
except PyFetionAuthError,e:
ShowMessage( ' 手机号密码错误 ' )
return 1
except PyFetionSocketError,e:
ShowMessage(e.msg)
return 1
finally :
pass
if ret:
ShowMessage( ' 登录成功 ' )
# 增加好友列表
buddys = self.Phone.get_contactlist()
if not buddys:
ShowMessage( ' 无好友 ' )
else :
self.BtnLog.Caption = ' 登录 '
for i in buddys:
if buddys[i][0] == '' :
buddys[i][0] = i[ 4 : 4 + 9 ]
for i in range(len(buddys)):
s = " %-4d%-8s%-20s " % (i,status[buddys[buddys.keys()[i]][ 2 ]].decode( ' gb2312 ' ).encode( ' utf8 ' ),buddys[buddys.keys()[i]][0],)
s = s.decode( ' utf8 ' ).encode( ' gb2312 ' )
self.FriendList.Items.Add(s)
self.threads.append(fetion_recv(self)) # 启动接收包
self.threads.append(fetion_alive(self.Phone)) # 启动心跳
for t in self.threads:
t.setDaemon(True)
t.start()
else :
ShowMessage( ' 失败 ' )
return 1
def guimain(argv = None):
PyFetion.GetSeirCodeEvent = GetSeriCode
Application.Initialize()
f = MainForm(Application)
f.Show()
FreeConsole()
Application.Run()
def __init__ (self,Owner):
self.Caption = ' 请输入验证码 '
self.Position = 5
self.BorderStyle = 3
self.Width = 275
self.Height = 162
self.lbl = Label(self)
self.lbl.SetProps(Parent = self,Caption = ' 请输入验证码 ' )
self.lbl.SetBounds( 24 , 16 , 72 , 13 )
self.EdtNum = Edit(self)
self.EdtNum.Parent = self
self.EdtNum.SetBounds( 102 , 11 , 139 , 21 )
self.Img = Image(self)
self.Img.Parent = self
self.Img.SetBounds( 24 , 35 , 217 , 59 )
self.Img.Center = True
self.Img.Picture.LoadFromFile( ' fetion_verify.jpg ' )
self.BtnOk = Button(self)
self.BtnOk.SetProps(Parent = self,Caption = ' 确定 ' )
self.BtnOk.SetBounds( 24 , 100 , 75 , 25 )
self.BtnOk.OnClick = self.BtnOkClick
self.BtnCancel = Button(self)
self.BtnCancel.SetProps(Parent = self,Caption = ' 取消 ' )
self.BtnCancel.SetBounds( 166 , 100 , 75 , 25 )
self.BtnCancel.OnClick = self.BtnCancelClick
def BtnCancelClick(self,Sender):
self.Close()
def BtnOkClick(self,Sender):
self.ModalResult = mrok
def GetSeriCode(self,picFile):
""" picFile 验证码图片 """
SeriFrm = SeriForm(None)
if SeriFrm.ShowModal() == mrok:
ret = SeriFrm.EdtNum.Text
else :
ret = ''
SeriFrm.Free()
return ret
class MainForm(Form):
def __init__ (self,Owner):
self.SetProps(Width = 492 ,Height = 401 ,BorderStyle = 3 )
self.lbUser = Label(self)
self.lbUser.SetProps(Parent = self,Caption = ' 用户 ' )
self.lbUser.SetBounds( 16 , 8 , 24 , 13 )
self.EdtUser = Edit(self)
self.EdtUser.Parent = self
self.EdtUser.SetBounds( 55 , 4 , 121 , 21 )
self.lbl = Label(self)
self.lbl.SetProps(Parent = self,Caption = ' 密码 ' )
self.lbl.SetBounds( 192 , 8 , 24 , 13 )
self.EdtPwd = Edit(self)
self.EdtPwd.SetProps(Parent = self,PasswordChar = ' * ' )
self.EdtPwd.SetBounds( 234 , 4 , 121 , 21 )
self.lbl1 = Label(self)
self.lbl1.SetProps(Parent = self,Caption = ' 好友列表 ' )
self.lbl1.SetBounds( 8 , 27 , 48 , 13 )
self.FriendList = ListBox(self)
self.FriendList.Parent = self
self.FriendList.SetBounds( 8 , 47 , 137 , 314 )
self.Memo1 = Memo(self)
self.Memo1.Parent = self
self.Memo1.SetBounds( 151 , 47 , 325 , 185 )
self.Memo2 = Memo(self)
self.Memo2.Parent = self
self.Memo2.SetBounds( 151 , 238 , 325 , 87 )
self.BtnSend = Button(self)
self.BtnSend.SetProps(Parent = self,Caption = ' 发送 ' )
self.BtnSend.SetBounds( 401 , 331 , 75 , 25 )
self.BtnSend.OnClick = self.BtnSendClick
self.BtnLog = Button(self)
self.BtnLog.SetProps(Parent = self,Caption = ' 登录 ' )
self.BtnLog.SetBounds( 361 , 3 , 75 , 25 )
self.BtnLog.OnClick = self.BtnLogClick
self.Phone = PyFetion( '' , '' , ' TCP ' )
self.threads = []
def BtnSendClick(self,Sender):
if self.Phone and self.Phone.alive:
if self.Phone.send_sms(toUTF8(self.Memo2.Lines.Text)):
self.Memo1.Lines.Add( ' 给自己发送短信息成功,目前只写了给自己发送信息 ' )
else :
ShowMessage( ' 无效的登录 ' )
def BtnLogClick(self,Sender):
if self.BtnLog.Caption == ' 登出 ' :
self.Phone.logout()
self.BtnLog.Caption = ' 登录 '
self.FriendList.Items.Clear()
return 1
self.Phone.mobile_no = self.EdtUser.Text
self.Phone.passwd = self.EdtPwd.Text
try :
ret = self.Phone.login(FetionOnline)
except PyFetionSupportError,e:
ShowMessage( ' 手机号未开通飞信 ' )
return 1
except PyFetionAuthError,e:
ShowMessage( ' 手机号密码错误 ' )
return 1
except PyFetionSocketError,e:
ShowMessage(e.msg)
return 1
finally :
pass
if ret:
ShowMessage( ' 登录成功 ' )
# 增加好友列表
buddys = self.Phone.get_contactlist()
if not buddys:
ShowMessage( ' 无好友 ' )
else :
self.BtnLog.Caption = ' 登录 '
for i in buddys:
if buddys[i][0] == '' :
buddys[i][0] = i[ 4 : 4 + 9 ]
for i in range(len(buddys)):
s = " %-4d%-8s%-20s " % (i,status[buddys[buddys.keys()[i]][ 2 ]].decode( ' gb2312 ' ).encode( ' utf8 ' ),buddys[buddys.keys()[i]][0],)
s = s.decode( ' utf8 ' ).encode( ' gb2312 ' )
self.FriendList.Items.Add(s)
self.threads.append(fetion_recv(self)) # 启动接收包
self.threads.append(fetion_alive(self.Phone)) # 启动心跳
for t in self.threads:
t.setDaemon(True)
t.start()
else :
ShowMessage( ' 失败 ' )
return 1
def guimain(argv = None):
PyFetion.GetSeirCodeEvent = GetSeriCode
Application.Initialize()
f = MainForm(Application)
f.Show()
FreeConsole()
Application.Run()
本代码就是在原作者的Fetion.py上修改来的,仅仅就是套上了一个界面GUI而已,另外发送短信,也就只写了发送给自己而已。如果个人有需要的自行扩展一下吧,嘿嘿,完整代码下载
本文转自 不得闲 博客园博客,原文链接:http://www.cnblogs.com/DxSoft/archive/2011/04/08/2009132.html ,如需转载请自行联系原作者