title: python socket readline without socket.makefile()
body: <p>I'm trying to parse an HTTP request line (e.g. <code>GET / HTTP/1.1\r\n</code>), which is easy with <code>socket.makefile().readline()</code> (<code>BaseHTTPRequestHandler</code> uses it), like:</p>
  <pre><code>print sock.makefile().readline()
  </code></pre>
  <p>unfortunately, as the <a href="https://docs.python.org/2/library/socket.html#socket.socket.makefile" rel="nofollow noreferrer">documentation</a> says, when using <code>makefile()</code> <em>the socket must be in blocking mode (it can not have a timeout)</em>; how can I implement a <code>readline()</code>-like function that does the same without using <code>makefile()</code> file object interface and not reading more than needed (as it'd discard data I will need after)?</p>
  <p>a pretty inefficient example:</p>
  <pre><code>request_line = &quot;&quot;
  while not request_line.endswith('\n'):
      request_line += sock.recv(1)
  print request_line 
  </code></pre>