forked from gregoriorobles/ptavi-p6
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·123 lines (94 loc) · 3.99 KB
/
server.py
File metadata and controls
executable file
·123 lines (94 loc) · 3.99 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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""SIP/UDP/RTP server."""
import socketserver
import sys
import os
def composeSipAnswer(method, address):
"""composeSipAnswer builds a SIP message with correct format.
arguments needed are (method, address)
"""
sipmsg = method
return sipmsg
def sendSong(song):
"""sendSong calls command to be executed by shell.
arguments needed are (song_name)
"""
command = './mp32rtp -i 127.0.0.1 -p 23032 < ' + song
print(command)
os.system(command)
def checkClientMessage(msg):
"""checkClientMessage checks if received message is correct formatted.
arguments needed are (msg)
"""
sipPart = msg[msg.find(' ')+1:]
sipPart = [sipPart[:sipPart.find(':')+1],
sipPart[sipPart.find(':')+1:sipPart.find('@')],
sipPart[sipPart.find('@'):sipPart.find('@')+1],
sipPart[sipPart.find('@')+1:sipPart.find(' ')],
sipPart[sipPart.find(' ')+1:]]
msg = msg.split(' ')
if sipPart[0] == 'sip:' and sipPart[2] == '@'\
and sipPart[4] == 'SIP/2.0\r\n\r\n':
if msg[0] == 'INVITE' or msg[0] == 'ACK' or msg[0] == 'BYE':
msgInfo = ['OK', msg[0]]
return msgInfo
else:
msgInfo = ['METHOD NOT ALLOWED', msg[0]]
return msgInfo
else:
msgInfo = ['BAD REQUEST', msg[0]]
return msgInfo
class EchoHandler(socketserver.DatagramRequestHandler):
"""Echo server class."""
def handle(self):
"""handle do all the things relates do communication."""
print('Replying to', self.client_address)
while 1:
line = self.rfile.read()
if line:
print("user sent " + line.decode('utf-8'))
checkClientMessage(line.decode('utf-8'))
if checkClientMessage(line.decode('utf-8'))[0] == 'OK':
if checkClientMessage(line.decode('utf-8'))[1] == 'ACK':
sendSong(sys.argv[3])
elif checkClientMessage(line.decode('utf-8'))[1] == 'BYE':
LINE = (composeSipAnswer('SIP/2.0 200 OK',
self.client_address) + '\r\n\r\n').encode()
self.wfile.write(LINE)
else:
print('METHOD ALLOWED')
LINE = (composeSipAnswer('SIP/2.0 100 Trying',
self.client_address) + '\r\n\r\n').encode()
self.wfile.write(LINE)
LINE = (composeSipAnswer('SIP/2.0 180 Ringing',
self.client_address) + '\r\n\r\n').encode()
self.wfile.write(LINE)
LINE = (composeSipAnswer('SIP/2.0 200 OK',
self.client_address) + '\r\n\r\n').encode()
self.wfile.write(LINE)
elif checkClientMessage(line.decode('utf-8'))[0]\
== 'METHOD NOT ALLOWED':
print('METHOD NOT ALLOWED')
LINE = (composeSipAnswer('405 METHOD NOT ALLOWED',
self.client_address) + '\r\n').encode()
self.wfile.write(LINE)
else:
print('BAD REQUEST')
LINE = (composeSipAnswer('400 BAD REQUEST',
self.client_address) + '\r\n').encode()
self.wfile.write(LINE)
# Si no hay más líneas salimos del bucle infinito
if not line:
break
if __name__ == "__main__":
# Creamos servidor de eco y escuchamos
try:
serv = socketserver.UDPServer((sys.argv[1],
int(sys.argv[2])), EchoHandler)
print("Listening..." + '\r\n')
serv.serve_forever()
except KeyboardInterrupt:
sys.exit('Exiting')
except IndexError:
sys.exit('Usage: python3 server.py IP port audio_file')