-
Notifications
You must be signed in to change notification settings - Fork 2
/
pypelyne_server.py
executable file
·200 lines (142 loc) · 6.43 KB
/
pypelyne_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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!/bin/python
#v103_server
__author__ = 'michaelmussato'
from conf.valuePyPELyNE import *
import os
import sys
import socket
import threading
from logging import *
import signal
import json
import src.getLocalIp as getLocalIp
from conf.valuePyPELyNE import *
import settings as SETTINGS
class server():
def __init__(self, host = '', debugLevel = INFO):
basicConfig(level = debugLevel)
self.host = host
self.port = serverPort
self.sockets = []
self.threads = []
#how many ports should be checked if given port fails?
self.portRange = int(serverPortRange)
# self.projects_root = projectsRootServer
self.projectsRootDarwin = projectsRootServerDarwin
self.projectsRootWin = projectsRootServerWin
self.projectsRootLinux = projectsRootServerLinux
self.tarSep = SETTINGS.ARCHIVE_SEPARATOR
self.pypelyne_root = os.getcwd()
self.sockets = []
self.launch()
def sendList(self, sock, path, addr, data = None):
try:
serialized = json.dumps([path, addr, data])
# send the length of the serialized data first
sock.send('%d\n' %(len(serialized)))
# send the serialized data
sock.sendall(serialized)
info(' server %s:%s | JSON serialized data successfully sent to %s:%s' %(self.host, self.port, addr[0], addr[1]))
except:
try:
#raise Exception('You can only send JSON-serializable data')
# send the length of the serialized data first
#sock.send('%d\n' %(len(serialized)))
# send the serialized data
sock.sendall(data)
info(' server:%s | non serialized data successfully sent %s:%s' %(self.port, addr[0], addr[1]))
except (TypeError, ValueError), e:
warning(' server:%s | data could not be sent to %s:%s. don\'t know how to handle yet.' %(self.port, addr[0], addr[1]))
#return 0
#raise Exception('sending data not possible')
def listContent(self, path, sock, addr):
self.sockets.append(sock)
# syntax: (socket, isPath, pathExists, content, receiver)
info(' server %s:%s | connection to %s:%s established' %(self.host, self.port, addr[0], addr[1]))
#while True:
while sock:
response = sock.recv(1024)
if response == 'getProjectsRootServerDarwin':
info('client %s:%s requested projectsRootServerDarwin, which is %s' %(addr[0], addr[1], self.projectsRootDarwin))
#data = os.listdir(self.projectsRootDarwin)
self.sendList(sock, self.projectsRootDarwin, addr)
elif response == 'getProjectsRootServerLinux':
info('client %s:%s requested projectsRootServerLinux, which is %s' %(addr[0], addr[1], self.projectsRootLinux))
self.sendList(sock, self.projectsRootDarwin, addr)
elif response == 'getProjectsRootServerWin':
info('client %s:%s requested projectsRootServerWin, which is %s' %(addr[0], addr[1], self.projectsRootWin))
self.sendList(sock, self.projectsRootWin, addr)
elif response == 'addProjectsServer':
projects = os.listdir(self.projects_root)
self.sendList(sock, self.projects_root, addr, projects)
info('client %s:%s requested projects list, which is %s' %(addr[0], addr[1], projects))
elif response == 'bye':
#self.sendSerialized(socket, response)
info('client %s:%s sent bye bye' %(addr[0], addr[1]))
#self.sendList(sock, 'bye', None)
self.sockets.remove(sock)
sock.close()
info('%s connections left open' %(len(self.sockets)))
break
elif os.path.exists(response):
info('valid path received: %s' %(response))
#sock.send('path %s exists' %(path))
content = os.listdir(path)
#for directory in content:
self.sendList(sock, response)
else:
warning('invalid path received: %s' %(response))
#sock.send('path %s does not exist' %(path))
self.sendList(sock, 'path %s does not exist' %(response))
#info('connection closed by server')
#sock.close()
def launch(self):
info('server starting')
info('server ip is %s' %(self.host))
counter = 1
while True:
try:
info('trying port %s' %(self.port))
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#s.setsockopt(socket.SOCK_STREAM, socket.SO_REUSEADDR, 1)
#s.setsockopt(socket.SO_REUSEADDR, 1)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
#s.setblocking(0)
sock.bind((self.host, self.port))
#connections = []
sock.listen(5)
info('port succeeded')
break
except socket.error:
info('port failed')
if counter < self.portRange:
#warning('port %s in use' %(self.port))
counter += 1
self.port += 1
else:
warning('tried %s port(s) without success. server failed to start.' %(counter))
sys.exit()
info('server listening at %s:%s' %(self.host, self.port))
while True:
try:
conn, addr = sock.accept()
#info('client connection from %s' %(str(addr)))
thread = threading.Thread(target = self.listContent, args = ('RetrThread', conn, addr))
thread.start()
#self.threads.append(t)
except KeyboardInterrupt:
print len(self.sockets)
#for i in self.connections:
# i.close()
#for i in self.threads:
# i.stop()
sys.exit()
#s.close()
#info('connection closed by server')
if __name__ == '__main__':
try:
ip = getLocalIp.getIp()
except:
ip = '127.0.0.1'
#server = server(host = ip, debugLevel = INFO)
server = server(host = ip, debugLevel = INFO)