Python 导入 MSN 联系人(web.py 方式)
一、下载并安装msnlib :
# wget http ://blitiri .com .ar /p /msnlib /files /3.8/msnlib- 3.8.tar .gz
# tar zxvf msnlib- 3.8.tar .gz
# cd msnlib- 3.8
# ./ install
# msnsetup
# msn
二、安装web .py :
# wget http ://webpy .org /static /web .py- 0.35.tar .gz
# tar zxvf web .py- 0.35.tar .gz
# cd web .py- 0.35
# python setup .py install
三、msn .py
# coding :utf- 8
import socket ,select ,time ,msnlib ,msncb ,web ,urllib
urls = (
'/', ' hello '
)
web .config .debug =False
app =web .application (urls ,globals ())
class hello :
def POST (self ):
i =web .input ()
result = get_friend_list(i .username ,i .pwd )
if result is None :
return '{status :false }'
else :
http_result = '{ status :true ,body :[';
for k ,v in result .iteritems ():
http_result = http_result + '{ name :"' +v .decode ("utf- 8", "ignore ") + '",k + '"},'
http_result = http_result[0:-1] + ']}';
return http_result
def get_friend_list(password ):
m =msnlib .msnd ()
m .cb =msncb .cb ()
m .strip ()
m .pwd =password .strip ()
m .encoding = 'utf- 8'
try :
m .login ()
m .sync ()
except :
return
m .change_status('invisible ')
begin_time = time .time ()
users =set ()
while True :
fds =m .pollable ()
infd =fds [0]
outfd =fds [1]
fds =select .select (infd ,outfd , [], 0)
for i in fds [0] +fds [1]:
try :
m .read (i )
except ('SocketError ',socket .error ),err :
if i !=m :
m .close (i )
merge_users = users |set (m .users .keys ())
if len (users ) ==len (merge_users):
end_time = time .time ()
if len (users ):
if end_time- begin_time >= 2:
break
elif end_time- begin_time >= 3:
break
time .sleep (0.05)
else :
users = merge_users
begin_time = time .time ()
result = {}
for i in users :
nick =m .users [i ].nick
i_lower = i .lower ()
if nick .lower () == i_lower:
nick =nick .split ("@", 1)[0]
result [i_lower] =nick
m .disconnect ();
return result
if __name__ == "__main__":
msnlib .debug =msncb .debug =void
app .run ()
四、执行msn .py 程式,并指定端口为8888:
# python msn .py 8888
五、参考文献:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。