Skip to content

Instantly share code, notes, and snippets.

@tothi
Created May 9, 2025 08:11
Show Gist options
  • Save tothi/068426b4e3e9ba0fc121e92efd90046a to your computer and use it in GitHub Desktop.
Save tothi/068426b4e3e9ba0fc121e92efd90046a to your computer and use it in GitHub Desktop.
RTSP client responses for forcing HTTP Basic authentication
!/usr/bin/env python3
import socket
import re
import datetime
HOST = "0.0.0.0"
PORT = 554
MSG_RTSP_OK = "RTSP/1.0 200 OK\r\nCSeq: {}\r\nPublic: OPTIONS, DESCRIBE, PLAY, PAUSE, SETUP, TEARDOWN, SET_PARAMETER, GET_PARAMETER\r\nDate: {}\r\n\r\n"
MSG_RTSP_401 = "RTSP/1.0 401 Unauthorized\r\nCSeq: {}\r\nWWW-Authenticate: Basic\r\nDate: {}\r\n\r\n"
def cseq(msg):
return re.search(b'\r\nCSeq: ([0-9]+)\r\n', msg).group(1).decode()
def mydate():
return datetime.datetime.now(datetime.timezone.utc).strftime("%a, %b %d %Y %H:%M:%S GMT")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(1024)
if not data:
break
if data.startswith(b"OPTIONS rtsp://"):
reply = MSG_RTSP_OK
elif data.startswith(b"DESCRIBE rtsp://"):
reply = MSG_RTSP_401
if b"Authorization:" in data:
print(data.decode())
exit()
else:
break
reply = reply.format(cseq(data), mydate()).encode()
print(data.decode())
conn.sendall(reply)
print(reply.decode())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment