-
Notifications
You must be signed in to change notification settings - Fork 12
/
server.py
76 lines (66 loc) · 1.56 KB
/
server.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
import socket
import StringIO
import threading
from time import sleep
import pyscreenshot as ImageGrab
from PIL import Image
import utils
import random
HOST = '192.168.1.81'
PORT = 50007
exitFlag = False
def sendScreen(conn):
while exitFlag==False:
img1 = ImageGrab.grab()
output = StringIO.StringIO()
img1.save(output, format="JPEG")
data = output.getvalue()
l = len(data)
pre = str(l)
while len(pre)!=20:
pre+= ","
data = pre + data
print "ScreenShot Size =", img1.size
print "Packet Length =", len(data)
conn.sendall(data)
#sleep(0.1)
threads = []
key = 748264
if __name__ == '__main__':
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((HOST, PORT))
s.listen(1)
img1 = ImageGrab.grab()
size = img1.size
mi = utils.MouseClass()
while 1:
conn, addr = s.accept()
print 'Connected by', addr
serverid = random.randint(100000, 999999)
print ("Server ID: " + str(serverid))
encryptedId = serverid^key
receivedstr = conn.recv(1024)
receivedId = int(receivedstr)
if receivedId != encryptedId:
exitFlag = True
print "Invalid ID entered by client"
conn.close()
continue
conn.sendall(str(size))
print "Client Valid"
exitFlag = False
t = threading.Thread(target=sendScreen, args=(conn,))
t.daemon = True
threads.append(t)
t.start()
while 1:
data = ""
data = conn.recv(1024)
if len(data) == 0:
exitFlag = True
print "Disconnected by",addr
break
print "Received String: ", data
mi.setMouseValues(eval(data))
conn.close()