-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
161 lines (127 loc) · 4.34 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import pyautogui
from bluetooth import *
import sys
import math
BUFFER_SIZE = 1024
DESKTOP_PATH = os.path.expanduser("~\Desktop\\")
SOCK = BluetoothSocket(RFCOMM)
UUID = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
def takeScreenshot():
myScreenshot = pyautogui.screenshot()
filePath = DESKTOP_PATH + "screenshot.png"
myScreenshot.save(filePath)
return
def serverPart():
SOCK.bind(("", PORT_ANY))
SOCK.listen(1)
port = SOCK.getsockname()[1]
advertise_service(SOCK, "SampleServer",
service_id=UUID,
service_classes=[UUID, SERIAL_PORT_CLASS],
profiles=[SERIAL_PORT_PROFILE])
print("Waiting for connection on RFCOMM channel %d" % port)
serverSock, clientInfo = SOCK.accept()
print("Accepted connection from ", clientInfo)
while True:
print("Enter command...")
command = serverSock.recv(1024)
if len(command) == 0:
break
print("received [%s]" % command)
stringData = command.decode("utf-8")
if "command" in stringData:
filePath = ""
fileSize = 0
message = ""
if "screenshot" in stringData:
takeScreenshot()
filePath = DESKTOP_PATH + "screenshot.png"
fileSize = os.stat(filePath).st_size
message = "screenshot.png " + str(fileSize)
elif "file" in stringData:
args = stringData.split()
filePath = str(args[2])
fileSize = os.stat(filePath).st_size
fileName = os.path.basename(filePath)
message = fileName + " " + str(fileSize)
print("file size - " + str(fileSize))
serverSock.send(message)
f = open(filePath, "rb")
while True:
content = f.read(BUFFER_SIZE)
if len(content) != 0:
flag = serverSock.send(content)
print(flag)
else:
break
print("File transfered!")
f.close()
elif "stop" in stringData:
break
else:
print("wrong command [%s]" % stringData)
print("disconnected")
serverSock.close()
SOCK.close()
return
def clientPart():
# Implement searching function
addr = "18:CF:5E:E4:AC:A7"
# if len(sys.argv) < 2:
# print("no device specified. Searching all nearby bluetooth devices for")
# print("the SampleServer service")
# else:
# addr = sys.argv[1]
print("Searching for SampleServer on %s" % addr)
service_matches = find_service(uuid=UUID, address=addr)
if len(service_matches) == 0:
print("Couldn't find the SampleServer service.")
sys.exit(0)
first_match = service_matches[0]
port = first_match["port"]
name = first_match["name"]
host = first_match["host"]
print("Connecting to \"%s\" on %s" % (name, host))
SOCK.connect((host, port))
print("Connected. Type stuff")
while True:
command = input()
if len(command) == 0:
break
SOCK.send(command)
# command screenshot
# command file
# stop
if "command" in command:
if "screenshot" in command or "file" in command:
fileInfo = SOCK.recv(BUFFER_SIZE)
fileNameAndSize = fileInfo.decode("utf-8")
print(fileNameAndSize)
args = fileNameAndSize.split()
fileName = args[0]
size = int(args[1])
f = open(str(DESKTOP_PATH) + str(fileName), "wb")
buffi = size - BUFFER_SIZE * math.floor(size / BUFFER_SIZE)
print(buffi)
while True:
data = SOCK.recv(BUFFER_SIZE)
if len(data) == buffi:
f.write(data)
break
f.write(data)
f.close()
print("File transfered!")
elif "stop" in command:
break
else:
continue
SOCK.close()
return
while True:
chooseRole = input()
if chooseRole == "server":
serverPart()
elif chooseRole == "client":
clientPart()
else:
"Wrong role! Try again"