-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_server.py
More file actions
137 lines (113 loc) · 4.13 KB
/
api_server.py
File metadata and controls
137 lines (113 loc) · 4.13 KB
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
import socket
import sys
import os
import json
from pathlib import Path
from file_explorer import FileExplorer
class APIServer:
# init API server
def __init__(self, host, port):
self.host = host
self.port = port
self.fileExplorer = FileExplorer()
# method to start the server and listen for incoming requests
def startServer(self):
addr = (self.host, self.port)
# create socket server and listen for connections
print(f"server listening on {self.host}:{self.port}")
self.servSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.servSock.bind(addr)
self.servSock.listen(1)
while True:
# accept incoming connection
remoteSock, _ = self.servSock.accept()
sockIn = remoteSock.makefile(mode="r")
sockOut = remoteSock.makefile(mode="w")
# receive message
reqString = sockIn.readline()[:-1]
request = json.loads(reqString)
# handle request
self.handleRequest(request, remoteSock)
# close files
sockIn.close()
sockOut.close()
# close server socket after breaking from loop
self.servSock.close()
# method to handle requests
def handleRequest(self, request, remoteSock):
# create io files
sockOut = remoteSock.makefile("w")
# GENERAL
if request["message"] == "ping":
response = {"success": True}
sockOut.write(json.dumps(response))
elif request["message"] == "getHostInfo":
response = {"success": True, "name": "Krishna-Mac"}
sockOut.write(json.dumps(response))
# FILES
# current directory name
elif request["message"] == "getCurrentDirectory":
response = {
"success":
True,
"cwd":
self.fileExplorer.getFileRep(
self.fileExplorer.currentDirectory),
}
sockOut.write(json.dumps(response))
# current directory contents
elif request["message"] == "getCurrentDirectoryContents":
contents = []
for file in self.fileExplorer.cwdContents():
contents.append(self.fileExplorer.getFileRep(file))
response = {"success": True, "contents": contents}
sockOut.write(json.dumps(response))
# change current directory
elif request["message"] == "changeCurrentDirectory":
# change directory
destination = request["destinationDirectory"]
result = self.fileExplorer.down(destination)
# write response
response = {
"success":
result,
"cwd":
self.fileExplorer.getFileRep(
self.fileExplorer.currentDirectory),
}
sockOut.write(json.dumps(response))
# go up file hierarchy
elif request["message"] == "goUp":
if self.fileExplorer.currentDirectory != Path.home():
self.fileExplorer.up()
# write response
response = {
"success":
True,
"cwd":
self.fileExplorer.getFileRep(
self.fileExplorer.currentDirectory),
}
sockOut.write(json.dumps(response))
# download file
elif request["message"] == "downloadFile":
# get arguments
fileName = request["fileName"]
# MESSAGING
elif request["message"] == "sendMessage":
# sync this message to database
print(request["payload"])
# show message
print(request["payload"])
# send response
response = {
"success": True,
}
sockOut.write(json.dumps(response))
# invalid request
else:
response = {"success": False, "message": "Invalid request"}
sockOut.write(json.dumps(response))
# close files and socket
sockOut.close()
remoteSock.close()