0

I have a log file with the following information:

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

where, 'admin' is the username, '******' is the password, '192.168.0.104' is the camera IP and '554' is the camera port. I want to extract these values separately and store these values in different variables, which will later be parsed to the GUI.

Since there are multiple characters in the line, I'm not sure how I can split them. Is there any way to do so?

3
  • Hi, you could give an acknowledge to the answers by upvoting them, at least to give something back for their effort. You're not obliged to do so, but it would be appreciated. Commented May 9, 2020 at 13:11
  • Unfortunately, I don't have enough reputation points to upvote an answer. :/ Commented May 9, 2020 at 17:11
  • Now you should... :D Commented May 10, 2020 at 0:51

2 Answers 2

1

How about regex?

import re

regex = re.compile(r".*//(?P<username>\S+):(?P<password>.*)@(?P<ip_address>.*):(?P<port>.*)/")

data = "RTSP0 rtsp://admin:******@192.168.0.104:554/onvif1"

for match in regex.finditer(data):
    username = match.group('username')
    password = match.group('password')
    ip_address = match.group('ip_address')
    port = match.group('port')

    print(
        "Username: {0}\nPassword: {1}\nIP Address: {2}\nPort: {3}"
        "".format(username, password, ip_address, port)
    )

The result is:

Username: admin
Password: ******
IP Address: 192.168.0.104
Port: 554
Sign up to request clarification or add additional context in comments.

1 Comment

This works perfectly. Thank you! I want to avoid using extra packages like urllib.parse, and this is tge most optimum solution for me.
0

You could use urllib.parse:

>>> from urllib.parse import urlparse
>>> o = urlparse('rtsp://admin:******@192.168.0.104:554/onvif1')
>>> o
ParseResult(scheme='rtsp', netloc='admin:******@192.168.0.104:554', path='/onvif1', params='', query='', fragment='')
>>> o.username
'admin'
>>> o.password
'******'
>>> o.hostname
'192.168.0.104'
>>> o.port
554

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.