-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
205 lines (177 loc) · 5.11 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
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
197
198
199
200
201
202
203
204
205
import sys, cgi, SocketServer, urlparse, json
from daemon import Daemon
from dataParser import *
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
class DataHandler:
# def __init__(self, file):
def __init__(self,file):
self.msgFlow = ''
self.msgFlowJson = ''
self.file = file
def formate(self):
self.data = dataFormat(self.file)
self.data.readData()
self.data.formatData()
self.parser = dataParser()
self.parser.init()
self.parser.feed(self.data.getFormatedData())
self.msgFlow = self.parser.getMsgFlow()
# printFlow(parser.getMsgFlow())
def getMsgFlow(self):
return self.msgFlow
def getMsgFlowJson(self):
self.msgFlowJson = json.dumps(self.msgFlow)
return self.msgFlowJson
def printFlow(self):
for m in self.msgFlow:
print m.direction
print m.msgFrom
print m.to
# print time.strftime("%D %H:%M", time.localtime(int(m.time + '')))
print m.time
print m.method
print m.statuscode
print m.sipCallId
print m.isRequest
print m.msg
def toJson(self):
kvpair = dict()
jsonList = list()
for m in self.msgFlow:
kvpair['direction'] = m.direction
kvpair['msgFrom'] = m.msgFrom
kvpair['to'] = m.to
kvpair['time'] = m.time
# print time.strftime("%D %H:%M", time.localtime(int(m.time + '')))
kvpair['method'] = m.method
kvpair['statuscode'] = m.statuscode
kvpair['sipCallId'] = m.sipCallId
kvpair['isRequest'] = m.isRequest
kvpair['msg'] = m.msg
jsonList.append(json.dumps(kvpair))
return json.dumps(jsonList)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
for file in self.files:
os.unlink(file)
del self
class routeHandler(BaseHTTPRequestHandler):
def getFile(self, filename):
filename = filename.replace('/public','public')
if filename == '/favicon.ico':
filename = 'public/img/favicon.ico'
try:
f = open(filename, 'rb')
except Exception, e:
raise e
buf = f.read()
f.close()
return buf
def do_GET(self):
urlArg = urlparse.urlparse(self.path)
uri = urlArg.path
print 'REQUEST URI:: ' + uri
if uri == '/':
#response index file /
page = self.getFile('public/index.html')
self.protocal_version = 'HTTP/1.1'
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(page)
del page
elif uri.endswith('.js'):
#response js files
js = self.getFile(uri)
self.protocal_version = 'HTTP/1.1'
self.send_response(200)
self.send_header('Content-Type', 'text/javascript')
# self.send_header('Content-Disposition', 'attachment;''filename=%s' % filename)
self.end_headers()
self.wfile.write(js)
del js
elif uri.endswith('.html'):
page = self.getFile(uri)
self.protocal_version = 'HTTP/1.1'
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
self.wfile.write(page)
del page
elif uri.endswith('.css'):
#response css files
css = self.getFile(uri)
self.protocal_version = 'HTTP/1.1'
self.send_response(200)
self.send_header("Content-Type", "text/css")
self.end_headers()
self.wfile.write(css)
del css
elif uri.endswith('.ico'):
ico = self.getFile(uri)
self.protocal_version = 'HTTP/1.1'
self.send_response(200)
self.send_header("Content-Type", "image/png")
self.end_headers()
self.wfile.write(ico)
elif uri.endswith('.map'):
#deal with .map request which I dont quite understand comes from where
self.protocal_version = 'HTTP/1.1'
self.send_response(404)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write('404 not found')
elif uri.endswith('.data'):
self.protocal_version = 'HTTP/1.1'
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
def do_POST(self):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type'],
})
# form = cgi.FieldStorage()
# Begin the response
self.send_response(200)
self.end_headers()
# return
# Echo back information about what was posted in the form
for field in form.keys():
print field
field_item = form[field]
if field_item.filename:
tmpData = DataHandler(field_item.file)
tmpData.formate()
buf = tmpData.getMsgFlow()
# print buf
jsonData = tmpData.toJson()
self.wfile.write(jsonData)
del tmpData
else:
# Regular form value
self.wfile.write('\t%s=%s\n' % (field, form[field].value))
def start_server(port):
http_server = HTTPServer(('[IP]', int(port)), routeHandler)
http_server.serve_forever()
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
for file in self.files:
os.unlink(file)
del self
def main():
PORT = 3000
httpd = SocketServer.TCPServer(('0.0.0.0', PORT), routeHandler)
print "serving at port", PORT
httpd.serve_forever()
class serverd(Daemon):
def run(self):
main()
if __name__ == '__main__':
initd = serverd('/home/vagrant/sipParser/pid/pid.pid')
initd.start()
initd.run()