Reputation: 27
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?
Upvotes: 0
Views: 78
Reputation: 880
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
Upvotes: 1
Reputation: 3361
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
Upvotes: 0