使用python實作rtsp客戶端的setup訊息發送時回傳狀態碼為503;現在不知如何解決,遂請求幫助。有償+wx:ww1194609610(10rmb)
```
import socket
from urllib.parse import urlparse
config_dict = { 'cseq': 2, 'user_agent': 'LibVLC/3.0.2 (LIVE555 Streaming Media v2016.11.28)', 'timeout': 3, 'recvbite': 4096, 'res_status': '200 OK', 'rtsp_status': 'flase'
}
clientports=[60784, 60785]
def options_get(url): ''' options請求檢測 url: rtsp流地址 return: options請求相應 ''' url = urlparse(url) host = url.netloc hostname = url.hostname path = url.path port = url.port str_options = 'OPTIONS rtsp://' + str(host) + \ path + ' RTSP/1.0\r\n' str_options += 'CSeq: ' + str(config_dict['cseq']) + '\r\n' str_options += 'User-Agent: ' + config_dict['user_agent'] + '\r\n' str_options += '\r\n' print(str_options) client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.settimeout(config_dict['timeout']) client.connect((hostname, port)) client.send(str_options.encode()) d = client.recv(config_dict['recvbite']) return d
def describe_get(url): ''' describe請求檢測 url: rtsp流地址 return: describe請求相應 ''' url = urlparse(url) host = url.netloc hostname = url.hostname path = url.path port = url.port str_describe = 'DESCRIBE rtsp://' + str(host) + \ path + ' RTSP/1.0\r\n' str_describe += 'CSeq: ' + str(config_dict['cseq'] + 1) + '\r\n' str_describe += 'User-Agent: ' + config_dict['user_agent'] + '\r\n' str_describe += '\r\n' client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.settimeout(config_dict['timeout']) client.connect((hostname, port)) client.send(str_describe.encode()) d = client.recv(config_dict['recvbite']) return d
def setup_get(url): ''' setup請求檢測 url: rtsp流地址 return: setup請求相應 ''' url = urlparse(url) host = url.netloc hostname = url.hostname path = url.path port = url.port str_setup = 'SETUP rtsp://' + str(host) + path + '/' + 'streamid=0' + ' RTSP/1.0\r\n' str_setup += 'CSeq: ' + str(config_dict['cseq'] + 2) + '\r\n' str_setup += 'User-Agent: ' + config_dict['user_agent'] + '\r\n' # config_dict['user_agent'] str_setup += 'Transport: RTP/AVP;unicast;client_port=61740-61741\r\n\r\n' str_setup += '\r\n' client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.settimeout(config_dict['timeout']) client.connect((hostname, port)) client.send(str_setup.encode()) d = client.recv(config_dict['recvbite']) return d
def teardown_get(url): ''' teardown請求檢測 url: rtsp流地址 return: teardown請求相應 ''' url = urlparse(url) host = url.netloc hostname = url.hostname path = url.path port = url.port str_teardown = 'TEARDOWN rtsp://' + str(host) + path + ' RTSP/1.0\r\n' str_teardown += 'CSeq: ' + str(config_dict['cseq'] + 4) + '\r\n' str_teardown += 'User-Agent: ' + config_dict['user_agent'] + '\r\n' str_teardown += '\r\n' print(str_teardown) client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) client.settimeout(config_dict['timeout']) client.connect((hostname, port)) client.send(str_teardown.encode()) d = client.recv(config_dict['recvbite']) return d
def send_main(url): try: str_options = str(options_get(url)) print(str_options) if config_dict['res_status'] in str_options: str_des = str(describe_get(url)) print(str_des) if config_dict['res_status'] in str_des: str_setup = str(setup_get(url)) str_teardown = str(teardown_get(url)) print(str_setup) print(str_teardown) if config_dict['res_status'] in str_teardown: config_dict['rtsp_status'] = 'true' return True except Exception: return False else: return False
print(send_main('rtsp://192.168.10.214:554/live/av0'))
```
上方為我的代碼。
b'RTSP/1.0 503 Service Unavailable\r\nCSeq: 4\r\nDate: Thu, 01 Jan 1970 07:11:16 GMT\r\n\r\n' 此段訊息為setup請求的回傳資訊,我看過rtsp的相關檔案,說describe請求回傳的sdp資訊,客戶端再分析該SDP描述,并為會話中的每一個流發送一個RTSP建立命令( SETUP)。這個我沒搞懂,也不知道如何寫,這里進行請教一下。
下面是我的一次正常的rtsp互動資訊。
```
OPTIONS rtsp://192.168.10.214:554/live/av0 RTSP/1.0
CSeq: 2
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
RTSP/1.0 200 OK
CSeq: 2
Public: OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE
DESCRIBE rtsp://192.168.10.214:554/live/av0 RTSP/1.0
CSeq: 3
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Accept: application/sdp
RTSP/1.0 200 OK
CSeq: 3
Date: Thu, 01 Jan 1970 07:05:17 GMT
Content-Base: rtsp://192.168.10.214:554/live/av0/
Content-Type: application/sdp
Content-Length: 315
v=0
o=- 0 0 IN IP4 127.0.0.1
s=No Title
c=IN IP4 0.0.0.0
t=0 0
a=tool:libavformat 55.12.100
m=video 0 RTP/AVP 96
a=rtpmap:96 H264/90000
a=fmtp:96 packetization-mode=1; sprop-parameter-sets=Z2QAKq2EAQwgCGEAQwgCGEAQwgCEK1A8ARPyzcBAQFAAAAMAEAAAAwPIQA==,aO48sA==; profile-level-id=64002A
a=control:streamid=0
SETUP rtsp://192.168.10.214:554/live/av0/streamid=0 RTSP/1.0
CSeq: 4
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Transport: RTP/AVP;unicast;client_port=61740-61741
RTSP/1.0 200 OK
CSeq: 4
Date: Thu, 01 Jan 1970 07:05:17 GMT
Session: 093634014dba841b
Transport: RTP/AVP/UDP;unicast;client_port=61740-61741;server_port=20006-20007
PLAY rtsp://192.168.10.214:554/live/av0/ RTSP/1.0
CSeq: 5
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Session: 093634014dba841b
Range: npt=0.000-
RTSP/1.0 200 OK
CSeq: 5
Date: Thu, 01 Jan 1970 07:05:17 GMT
Session: 093634014dba841b
TEARDOWN rtsp://192.168.10.214:554/live/av0/ RTSP/1.0
CSeq: 6
User-Agent: LibVLC/3.0.5 (LIVE555 Streaming Media v2016.11.28)
Session: 093634014dba841b
RTSP/1.0 200 OK
CSeq: 6
Date: Thu, 01 Jan 1970 07:05:21 GMT
Session: 093634014dba841b
```
uj5u.com熱心網友回復:
rtsp是做什么的?轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/115270.html
