How can I read the first four lines from readlines(), I am getting a STDIN from proxy to my script:
GET http://www.yum.com/ HTTP/1.1
Host: www.yum.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0.1) Gecko/20100101 Firefox/9.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Proxy-Connection: keep-alive
i read it using sys.stdin.readlines() and log it to file, but i want to log only GET and User-Agent lines to the file.
while True:
line = sys.stdin.readlines()
for l in line:
log = open('/tmp/redirect.log', 'a')
log.write(l)
log.close()
Using
withensures good closure of the log.You can iterate through
sys.stdinas with any file-type object in Python, which is faster as it doesn’t need to create a list.The following is an efficient method as it doesn’t check for the same lines again and again, and only checks while there are lines it needs left. Probably not needed given the situation, but if you had more items to check for, and more things to sort through, could be worth doing. It also means you keep track of the parts you have, and don’t read beyond the lines you need. This could be valuable if reading is an expensive operation.
The set is unordered, but we can assume the lines will come in order, and hence get logged in order.
This kind of set-up may also be needed for more complex checking of lines (e.g: with regular expressions). In your case however, the first example is concise and should work well.