-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
42 lines (42 loc) · 1.45 KB
/
client.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
import socket
methods=['OPTIONS', 'SETUP', 'PLAY', 'TEARDOWN']
class TcpClient:
def __init__(self, host=socket.gethostname(), port=8888):
self.host=host
self.port=port
self.url = f'diantp://{host}:{port}'
self.vertion = 0.5
self.CSeq=1
self.client=socket.socket()
self.client.connect((host,port))
self.session_record=[]
self.id=0
def message(self) -> str:
for i in methods:
self.msg = f'{i} {self.url} version={self.vertion}\r\nCSeq:{self.CSeq}\r\n'
self.xxx(i)
self.CSeq+=1
def xxx(self,i):
if i=='OPTIONS':
msg=self.msg
elif i=='SETUP':
msg=f'{self.msg}Transport:TCP\r\nclient_port={self.port}\r\n'
elif i=='PLAY':
self.id = self.client.recv(1024).decode('utf-8')
print(self.id)
message=self.client.recv(1024).decode('utf-8')
msg=f'{self.msg}Session_id={self.id}\r\nRange:ntp:{0}-{15}\r\n'
print(message)
elif i=='TEARDOWN':
msg=f'{self.msg}Session_id={self.id}\r\n'
else:
msg='WARNING:Wrong message'
self.client.send(msg.encode('utf-8'))
self.session_record.append(f'{msg}\r\n')
print(msg)
def __del__(self):
self.client.close()
print(self.session_record)
print('\n断开连接\n')
x=TcpClient()
x.message()