importsocketimporthashlibimportbase64importtime
config_dict={'server_username': 'admin', #RTSP用户名
'server_password': '123456', #RTSP用户名对应密码
'server_ip': '10.10.6.92', #RTSP服务器IP地址
'server_port': 554, #RTSP服务器使用端口
'server_path': '/chIP=1&streamType=main/', #URL中端口之后的部份,测试发现不同服务器对这部份接受的值是不一样的,也就是说自己使用时很可能得自己修改这部份的值
'cseq': 2, #RTSP使用的请求起始序列码,不需要改动
'user_agent': 'LibVLC/3.0.2 (LIVE555 Streaming Media v2016.11.28)', #自定义请求头部
'buffer_len': 1024, #用于接收服务器返回数据的缓冲区的大小
'auth_method': 'Digest', #RTSP使用的认证方法,Basic/Digest
'header_normal_modify_allow': False, #是否允许拼接其他协议规定的请求头的总开关,请些请求头的值为正常值(大多是RFC给出的示例)
'header_overload_modify_allow': False, #是否允许拼接其他协议规定的请求头的总开关,请些请求头的值为超长字符串
'options_header_modify': True, #OPTIONS请求中,是否允许拼接其他协议规定的请求头的开关
'describe_header_modify': True, #第一次DESCRIBE请求中,是否允许拼接其他协议规定的请求头的开关
'describe_auth_header_modify': True, #第二次DESCRIBE请求中,是否允许拼接其他协议规定的请求头的开关
'setup_header_modify': True, #第一次SETUP请求中,是否允许拼接其他协议规定的请求头的开关
'setup_session_header_modify': True, #第二次SETUP请求中,是否允许拼接其他协议规定的请求头的开关
'play_header_modify': True, #PLAY请求中,是否允许拼接其他协议规定的请求头的开关
'get_parameter_header_modify': True, #GET PARAMETER请求中,是否允许拼接其他协议规定的请求头的开关
'teardown_header_modify': True #TEARDOWN请求中,是否允许拼接其他协议规定的请求头的开关
}classRtspClient():def __init__(self):#self.socket_send = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#self.socket_send.settimeout(5)
#self.socket_send.connect((config_dict['server_ip'], config_dict['server_port']))
pass
#用于Digest认证方式时生成response的值
defgen_response_value(self,url,public_method,realm,nonce):
frist_pre_md5_value= hashlib.md5((config_dict['server_username'] + ':' + realm + ':' + config_dict['server_password']).encode()).hexdigest()
first_post_md5_value= hashlib.md5((public_method+':' +url).encode()).hexdigest()
response_value= hashlib.md5((frist_pre_md5_value + ':' + nonce + ':' +first_post_md5_value).encode()).hexdigest()returnresponse_value#生成options请求头部
defgen_options_header(self):globalconfig_dict
str_options_header= 'OPTIONS rtsp://' + config_dict['server_ip'] + ':' + str(config_dict['server_port']) +config_dict['server_path'] + 'RTSP/1.0\r\n'str_options_header+= 'CSeq:' + str(config_dict['cseq']) + '\r\n'str_options_header+= 'User-Agent:' + config_dict['user_agent'] + '\r\n'str_options_header+= '\r\n'
returnstr_options_header#生成第一次describe请求头部
defgen_describe_header(self):globalconfig_dict
str_describe_header= 'DESCRIBE rtsp://' + config_dict['server_ip'] + ':' + str(config_dict['server_port']) +config_dict['server_path'] + 'RTSP/1.0\r\n'str_describe_header+= 'CSeq:' + str(config_dict['cseq'] + 1) + '\r\n'str_describe_header+= 'User-Agent:' + config_dict['user_agent'] + '\r\n'str_describe_header+= 'Accept: application/sdp\r\n'str_describe_header+= '\r\n'
returnstr_describe_header#生成第二次describe请求头部
defgen_describe_auth_header(self,url,realm,nonce):globalconfig_dict
public_method= 'DESCRIBE'str_describe_auth_header= 'DESCRIBE rtsp://' + config_dict['server_ip'] + ':' + str(config_dict['server_port']) + config_dict['server_path'] + 'RTSP/1.0\r\n'str_describe_auth_header+= 'CSeq:' + str(config_dict['cseq'] + 2) + '\r\n'
if config_dict['auth_method'] == 'Basic':
auth_64= base64.b64encode((config_dict['server_username'] + ":" + config_dict['server_password']).encode("utf-8")).decode()
str_describe_auth_header+= 'Authorization: Basic'+auth_64 + '\r\n'
else:
response_value=self.gen_response_value(url, public_method, realm, nonce)
str_describe_auth_header+= 'Authorization: Digest username="'+config_dict['server_username']+'", realm="'+realm+'", nonce="'+nonce+'", uri="'+url+'", response="'+response_value+'"\r\n'str_describe_auth_header+= 'User-Agent:' + config_dict['user_agent'] + '\r\n'str_describe_auth_header+= 'Accept: application/sdp\r\n'str_describe_auth_header+= '\r\n'
returnstr_describe_auth_header#生成第一次setup请求头部
defgen_setup_header(self,url, realm, nonce):globalconfig_dict
public_method= 'SETUP'str_setup_header= 'SETUP rtsp://' + config_dict['server_ip'] + ':' + str(config_dict['server_port']) + config_dict['server_path'] + 'trackID=0 RTSP/1.0\r\n'str_setup_header+= 'CSeq:' + str(config_dict['cseq'] + 3) + '\r\n'
if config_dict['auth_method'] == 'Basic':
auth_64= base64.b64encode((config_dict['server_username'] + ":" + config_dict['server_password']).encode("utf-8")).decode()
str_setup_header+= 'Authorization: Basic'+auth_64 + '\r\n'
else:
response_value=self.gen_response_value(url, public_method, realm, nonce)
str_setup_header+= 'Authorization: Digest username="'+config_dict['server_username']+'", realm="'+realm+'", nonce="'+nonce+'", uri="'+url+'", response="'+response_value+'"\r\n'str_setup_header+= 'User-Agent:' + config_dict['user_agent'] + '\r\n'str_setup_header+= 'Transport: RTP/AVP;unicast;client_port=50166-50167\r\n'str_setup_header+= '\r\n'
returnstr_setup_header#生成第二次setup请求头部
defgen_setup_session_header(self,url, realm, nonce,session):globalconfig_dict
public_method= 'SETUP'str_setup_session_header= 'SETUP rtsp://' + config_dict['server_ip'] + ':' + str(config_dict['server_port']) +config_dict['server_path'] + 'trackID=1 RTSP/1.0\r\n'str_setup_session_header+= 'CSeq:' + str(config_dict['cseq'] + 4) + '\r\n'
if config_dict['auth_method'] == 'Basic':
auth_64= base64.b64encode((config_dict['server_username'] + ":" + config_dict['server_p