-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
88 lines (80 loc) · 2.16 KB
/
main.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python
import socket
import threading
import time
import pyperclip
import traceback
import daemon
import sys
perip=open('config.ini').readline().strip()
print perip
port=9999
#important ! if not set ,daemon can't call paste copy correct
pyperclip.set_clipboard('xsel')
clipdata=pyperclip.paste()
def tcplink(sock,addr):
global clipdata
print 'Accept new connection from %s:%s...' % addr
while True:
data=sock.recv(1024*10)
time.sleep(1)
if data=='exit' or not data:
break
#sock.send('Hello,%s!'%data)
print "%s - %s"%("recv",data)
try:
pyperclip.copy(data)
clipdata = data
except Exception as e:
traceback.print_exc()
sock.close()
print 'Connection from %s:%s closed.'%addr
def clipbord_recv_server():
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind(('0.0.0.0',port))
s.listen(5)
while True:
sock,addr = s.accept()
t=threading.Thread(target=tcplink,args=(sock,addr))
#t.setDaemon(True)
t.start()
def clipbord_send():
global clipdata
while True:
data = pyperclip.paste()
if data != clipdata:
try:
s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((perip,port))
s_data = data
s.send(s_data)
clipdata = data
print "%s - %s"%("send",s_data)
except Exception :
traceback.print_exc()
finally:
s.close()
time.sleep(1)
def test():
while True:
print "running"
time.sleep(10)
def main():
time.sleep(1)
threads = []
t1 = threading.Thread(target=clipbord_send)
t2 = threading.Thread(target=clipbord_recv_server)
t3 = threading.Thread(target=test)
threads = [t1,t2,t3]
for t in threads:
t.setDaemon(True)
t.start()
while True:
time.sleep(1)
if __name__ == "__main__":
if len(sys.argv) == 2 and sys.argv[1] == "debug":
main()
else:
print "start as daemon.."
with daemon.DaemonContext():
main()