Python3 Socket Rtsp wrong response - python-3.x

I am trying read the status code using socket from a rtsp stream.
unfortunately the response is always 401...
'RTSP/1.0 401 Unauthorized\r\nCSeq: 0\r\nWWW-Authenticate: Digest realm="Login to PA6BG0123456", nonce="1234567891234565"\r\n\r\n'
...however i am sure the route and password are correct
rtsp://admin:admin#192.168.0.5:554/cam/realmonitor?channel=1&subtype=0
works perfectly using vlc and ffmpeg.
Snipped code
import socket
PORT = 554
IP = '192.168.0.5'
TIMEOUT = 5
CRED = 'admin:admin'
PATH = '/cam/realmonitor?channel=1&subtype=0'
#create describe packet
global DESCRIBEPACKET
DESCRIBEPACKET = 'DESCRIBE rtsp://%s#%s%s RTSP/1.0\r\n' % (CRED, IP, PATH)
DESCRIBEPACKET += 'CSeq: 2\r\n'
DESCRIBEPACKET += 'Accept: application/sdp\r\n'
DESCRIBEPACKET += 'User-Agent: Mozilla/5.0\r\n\r\n'
print(DESCRIBEPACKET) #for debug
#connect to socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(TIMEOUT)
s.connect((IP, PORT))
s.sendto(DESCRIBEPACKET.encode(), (IP, PORT))
data = repr(s.recv(1024))
print(data) #For debug sends me the wrong 401 response
s.close()
Any ideas what am i doing wrong?

Related

Trouble getting my server to respond back to client | Python | Sockets

I'm trying to repurpose this script, I want the server to send messages to my client, and I want the client to be the one that 'reaches out' and makes the connection. As you can see, with the script I have, the client is the one reaching out but it's the one that can send the messages to the server. I've tried looking at other scripts, moving the send and handle function around, but I feel like my inexperience with sockets got me stumped. Any help would be appreicated.
Server:
import socket
import threading
HEADER = 64
PORT = 5430
SERVER = '192.110.100.189' # socket.gethostbyname(socket.gethostname())
ADDR = (SERVER, PORT)
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = '!END'
# defines the type of connection
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def handle_client(conn, addr):
print(f'[NEW CONNECTIONS] {addr} connected')
connected = True
while connected:
# the header variable here acts as a protocol to give us an idea of how many bytes are being sent
msg_length = conn.recv(HEADER).decode(FORMAT)
if msg_length:
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
if msg == DISCONNECT_MESSAGE:
break
print(f'{addr} | {msg}')
conn.send('msg recieved'.encode(FORMAT))
conn.close()
def start():
server.listen()
print(f'[LISTENING] Server is listening on {SERVER}')
while True:
conn, addr = server.accept()
thread = threading.Thread(target = handle_client, args = (conn, addr))
thread.start()
print(f'[ACTIVE CONNECTIONS] {threading.activeCount() - 1}')
print('[STARTNG] Server is starting')
start()
Client:
import socket
HEADER = 64
PORT = 5430
FORMAT = 'utf-8'
DISCONNECT_MESSAGE = '!END'
SERVER = '192.110.100.189'
ADDR = (SERVER, PORT)
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(msg):
message = msg.encode(FORMAT)
msg_length = len(message)
send_length = str(msg_length).encode(FORMAT)
send_length += b' ' * (HEADER - len(send_length))
client.send(send_length)
client.send(message)
while True:
enter = input('CLIENT - ')
send(enter)
if enter == DISCONNECT_MESSAGE:
break

Get an empty png image using socket in Python

I try to transmit an image from a client to the server,
but it only worked for connection but sending image.
This is the code for the client:
import socket
host = '10.10.40.22'
port = 8888
address = (host, port)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
#connect to the server
s.connect(address)
print('start send image')
with open('doge.png', 'rb') as imgfile:
while True:
imgData = imgfile.readline(1024)
if not imgData:
break
s.send(imgData)
print('end')
and here is the code for the server:
host = '10.10.40.22'
port = 8888
address = (host, port)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(address)
s.listen(1)
print('wait for connection...')
conn, addr = s.accept()
#start to receive picture
with open('Pictures/doge.png', 'wb') as imgfile:
while True:
img_data = conn.recv(1024)
if not img_data:
break
imgfile.write(img_data)
print('writing ...')
conn.close()
s.close()
print('end')
notice that I print a debug message in the code of server that
if it writes any bytes into the image then print the message 'writing'
how can I do now?

Can't receive an answer from python socket

I have a working command for linux that sends a request and gets a response:
echo -n -e '\x9e\x4c\x23\x00\x00\xff\xff\xce\xf2\x3b\x18\x80' | nc -u -w 1 -p 11244 127.0.0.1 11235
I'm trying to do the same through the python:
import socket
IP = "127.0.0.1"
PORT = 11235
MESSAGE = "9e4c230000ffffcef23b1880"
BINARY_MESSAGE = MESSAGE.decode('hex')
print ("BINARY_MESSAGE: [%s]" % BINARY_MESSAGE) # <- message is the same as echo -en does
srvsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
srvsock.settimeout(3) # 3 seconds
srvsock.connect((IP, PORT))
srvsock.sendall(BINARY_MESSAGE)
data = srvsock.recv(4096)
print ("Received message: [%s]" % data)
srvsock.close()
I receive a blank responce:
BINARY_MESSAGE: [�L#����;�]
Received message: []
I guess I'm doing something wrong.
Your nc command line is using UDP (-u). Your Python code is using TCP (socket.SOCK_STREAM). You want to create a UDP socket:
srvsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
You will want to get rid of the call to connect, but you will need a call bind if you expect to receive messages on that socket. For example, the following code sends a message then waits for a reply on the same socket:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('127.0.0.1', 2222))
s.sendto('this is a test', ('127.0.0.1', 3333))
data, srcaddr = s.recvfrom(1024)
print(data, srcaddr)
Remember that UDP is a connectionless protocol. The other side of this conversation can't "reply" to the message; it needs to explicitly send to the address/port on which the above code is listening. In Python the other side of this conversation might look like:
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(('127.0.0.1', 3333))
data, srcaddr = s.recvfrom(1024)
print(data)
s.sendto('that was a test', srcaddr)
The above code waits for a message then sends a response back.

Can't set timeout for python 3 recv

I am trying to get a time out on the code below. But it just hangs at the recv and never times out. Can someone point to what I am doing wrong? I have looked and I can't seem to find too much on it.
import socket
host = "localhost"
port = 8888
# create socket object
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# give object the ip and port to listen on
server_address = (host, port)
print('starting up on %s port %s' % server_address)
sock.bind(server_address)
# how many listeners
sock.listen(0)
# sets the time out
sock.settimeout(10)
while True:
print('waiting for a connection')
try:
#this waits for a connection from the sending side.
connection, client_address = sock.accept()
print('connection from', client_address)
start = False
message = ""
while client_address != "":
#this listens and waits for data to be sent and sets it to the data variable
data = connection.recv(32000).decode()
You have set an accept timeout on the listening socket, not a read timeout on the receiving socket.

python 3 tcp server and client string transfer error

I try to develop a simple server and client program, I run the code with python 3.4.2.( on Debian 8.6 ) . The server run well, the client program connect's to the server but when I pass a text in terminal to send to server and send back with time stamp, I get this error in the client terminal window
Traceback (most recent call last):
File "tcp_client", line 15, in
tcpCliSock.send(data)
TypeError: 'str' does not support the buffer interface
this is the server code
from socket import *
from time import ctime
HOST = '192.168.0.141'
PORT = 21577
BUFSIZ = 1024
ADDR = (HOST, PORT)
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
while True:
print('waiting for connection...')
tcpCliSock, addr = tcpSerSock.accept()
print('....connected from :', addr)
while True:
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
tcpCliSock.send('[%s] %s' % (bytes(ctime(), 'utf-8'), data))
tcpCliSock.close()
tcpSerSock.close()
and this it the client code
from socket import *
HOST = '192.168.0.141'
PORT = 21577
BUFSIZ = 1024
ADDR = (HOST, PORT)
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
while True:
data = input('> ')
if not data:
break
tcpCliSock.send(data)
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
print(data.decode('utf-8'))
tcpCliSock.close()

Resources