I have working python script producing following output.
JavaScript
x
import re
import sys
import socket
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect(('10.10.10.10', 25001))
conn.send('statusn')
data = conn.recv(16777216)
conn.close()
print data
Script output
JavaScript
session 3594154272 xwf9VgJQfRSSPBCyXYVRzlw==foo xh_iUVQ6JRieFv70JjDVOnQ==bar 13584
stream 102.22.133.12:33562 173.24.54.19:39814 10.10.10.10:12838 39382628/29008576/68391204
session 3363219694 xiXdeg8oGTQ6MYZ-Z5q4iKw==foo xVBsa0nlZT4-vOLA8P-WxSw==bar 2319
stream 71.22.223.192:43718 115.125.65.61:41126 10.10.10.10:12802 4243204/9624660/13867864
.
.
.
Now i want want any line starting with stream
in output so i am using following logic but its not working.
JavaScript
import re
import sys
import socket
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect(('10.10.10.10', 25001))
conn.send('statusn')
data = conn.recv(16777216)
conn.close()
# parse line contain stream
for line in data:
if re.match(r'stream', line):
print line,
Advertisement
Answer
Your data
is a massive string
instead of a list
of strings
as you expected. Also it’s easier to use str.startswith
instead of regex (less resource hungry). Simply change your code as follows:
JavaScript
# parse line contain stream
for n, line in enumerate(data.split('n')):
if line.startswith('stream '):
print line
Where n
is the total number of lines you have in your data
. If you only wanted to count the specific times you encountered "stream"
:
JavaScript
count = 0
for line in data.split('n'):
if line.startswith('stream '):
print line
count += 1