Ubuntu Pastebin

Paste from jusss at Thu, 16 Apr 2015 06:56:36 +0000

Download as text
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env python3

"""
通过时间差判断,n秒后执行某函数而不停止当前函数,像sleep()那样可以计时执行某函数,但不会像sleep()那样阻塞当前函数
收到新邮件提醒后,如果不读取,每3分钟重新检测下再提醒
因为sleep()会阻塞函数导致socket超时中断,现在用时间差判断可以取代sleep()而且又不会阻塞函数导致socket超时中断
"""

import socket, ssl, os, io, time, sys

address='jusss.org'
port=993
user='x'
password='x'
encoding='utf-8'
count=0
latest_recent_time=time.time()

a_socket = socket.socket()
ssl_socket = ssl.wrap_socket(a_socket)
ssl_socket.connect((address,port))

ssl_socket.write(('a_tag login ' + user + ' ' + password + '\r\n').encode(encoding))
ssl_socket.write('a_tag select inbox\r\n'.encode(encoding))
ssl_socket.write('a_tag idle\r\n'.encode(encoding))

while True:
    try:    
        recv_msg=ssl_socket.read().decode(encoding)[:-2]
    
    except ssl_socket.SSLEOFError:
        sys.exit()

    print(recv_msg)
    
    if recv_msg.find('RECENT') > -1:
        
        if count > 0:
            os.system("mplayer -really-quiet /home/jusss/bash/new-email.mp3 2> /dev/null")
            latest_recent_time=time.time()
        else:
            count=count+1

    if time.time() - latest_recent_time > 600:
        latest_recent_time=time.time()
        ssl_socket.write('done\r\n'.encode(encoding))
        recv_msg=ssl_socket.read().decode(encoding)[:-2]
        print(recv_msg)
        ssl_socket.write('a_tag status inbox (unseen)\r\n'.encode(encoding))
        recv_msg=ssl_socket.read().decode(encoding)[:-2]
        print(recv_msg)
        ssl_socket.write('a_tag idle\r\n'.encode(encoding))
        
        if recv_msg[recv_msg.find('UNSEEN')+7] != '0':
            os.system("mplayer -really-quiet /home/jusss/bash/new-email.mp3 2> /dev/null")


#    ssl_socket.write('a_tag status inbox (unseen)\r\n'.encode(encoding))
#    * STATUS "inbox" (UNSEEN 0)
#    idle need to be end with 'done'
Download as text