Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7309057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:39:41+00:00 2026-05-28T23:39:41+00:00

How can I read the first four lines from readlines() , I am getting

  • 0

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()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:39:42+00:00Added an answer on May 28, 2026 at 11:39 pm

    Using with ensures good closure of the log.
    You can iterate through sys.stdin as with any file-type object in Python, which is faster as it doesn’t need to create a list.

    with open('/tmp/redirect.log', 'a') as log:
        while True: #If you need to continuously check for more.
            for line in sys.stdin:
                if line.startswith(("GET", "User-Agent")):
                    log.write(line)
    

    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.

    with open('/tmp/redirect.log', 'a') as log:
        while True: #If you need to continuously check for more.
            needed = {"GET", "User-Agent"}
            for line in sys.stdin:
                for item in needed:
                    if line.startswith(item):
                        log.write(line)
                        break
                needed.remove(item)
                if not needed: #The set is empty, we have found all the lines we need.
                    break
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I read the first line from a text file using a Windows
How you can read a file (text or binary) from a batch file? There
I'm trying to implement a simple method to read new lines from a log
Having just read the first four chapters of Refactoring: Improving the Design of Existing
Anyone can read the GoF book to learn what design patterns are and how
I can read the MySQL documentation and it's pretty clear. But, how does one
You can read about the 64-bit calling convention here . x64 functions are supposed
You can read this question where I ask about the best architecture for a
I can read unmanaged memory in C# using UnmanagedMemoryStream, but how can I do
I can read settings like this, for example: final String mytest = System.getString(this.getContentResolver(), System.AIRPLANE_MODE_ON);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.