0

I have a log file, which currently stores the following information:

RTSP0 rtsp://admin:****@192.168.0.104:554/onvif1
RTSP1 rtsp://admin:****@192.168.0.104:554/onvif2
CAMERA_HASH a586c0c691aa7e3fb37d1ff318bf4d6fdb83b24b
RTSP0 rtsp://admin:****@192.168.0.104:554/onvif1
RTSP1 rtsp://admin:****@192.168.0.104:554/onvif2
CAMERA_HASH a586c0c691aa7e3fb37d1ff318bf4d6fdb83b24b
RTSP0 rtsp://admin:****@192.168.0.104:554/onvif1
RTSP1 rtsp://admin:****@192.168.0.104:554/onvif2
CAMERA_HASH a586c0c691aa7e3fb37d1ff318bf4d6fdb83b24b

Every time a camera is connected, two new RTSP streams (i.e. RTSP0 and RTSP1) and a HASH number is added to the log file. I need to extract the RTSP stream of the most recent camera connected (i.e. the recent RTSP0 stream). Is there a way to read through the file and extract only this specific stream? Currently, I am doing:

searchfile = open('/Eya/pine_onvif/logs/camera_hash.log', 'r')
search = searchfile.readlines()
stream = []
line_cont = []
streamValue = []
for i,line in enumerate(search):
   if 'RTSP0' in line:
      line_cont = line
      stream = line.split(' ')
      streamValue = stream[1]
      filename = streamValue
      print(streamValue)
searchfile.close()

But this method is yielding an output such as the following:

rtsp://admin:****@192.168.0.104:554/onvif1

rtsp://admin:****@192.168.0.104:554/onvif1

rtsp://admin:****@192.168.0.104:554/onvif1

I'm unable to retrieve only the last line of the streamValue array.

2 Answers 2

1

For your second question, you should use the 'reversed' read, that loop from the end of file, then use a simple break once you find the first rtsp line:

for line in reversed(list(searchfile)):
    hash = re.match(r"RTSP[.0-9] (rtsp:\/\/.*)", line.rstrip())
    if hash is not None :
        print(hash.group(1))
        break
Sign up to request clarification or add additional context in comments.

Comments

0

the module re and regex are what your are looking for:

EDITED:

import re
searchfile = open('/Eya/pine_onvif/logs/camera_hash.log', 'r')
for line in searchfile:
    hash = re.match(r"RTSP[.0-9] (rtsp:\/\/.*)", line)
    if hash is not None :
        print(hash.group(1))
searchfile.close()

Also, if you want to search from the end of the file, you can change your loop with:

for line in reversed(list(searchfile)):
    hash = re.match(r"RTSP[.0-9] (rtsp:\/\/.*)", line.rstrip())
    if hash is not None :
        print(hash.group(1))

3 Comments

With this, I'm able to print all the three HASH numbers. But I only need the RTSP0 stream (i.e. rtsp://admin:****@192.168.0.104:554/onvif1). Also, I have to read them from a file. With your method, I will have to use for line in open('/Eya/pine_onvif/logs/camera_hash.log', 'r').readlines() to extract each line from the log file.
The text was a simple copy of the file to make the code 'self' executable. However, I've changed the code. Now you can test it. I've also build the regex to find the rtsp address.
With your new method, the RTSP streams are printed a total of 6 times (3 time for RTSP1 and 3 times for RTSP0). I edited my question as I made some progress in what I need. Could you look at it?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.