[Tutor] Re: Reading text lines from a socket

Lloyd Kvam pythontutor at venix.com
Sat Feb 28 20:28:59 EST 2004


Well, I've never used it, but sockets do have the makefile method.  That
would seem to fit what you're trying to do.

Sandip Bhattacharya wrote:

> [Reposting to the general list too]
> 
> Lloyd Kvam wrote:
> 
>> Sockets deal with packetized data.  The network protocols do not 
>> guarantee
>> keeping the data in line oriented chunks - even if the data starts out 
>> that way.
>>
>> You need to deal with extracting lines from chunks.  So long as the 
>> connection is
>> working properly, this is easy.  The challenge occurs when the 
>> remainder of a line
>> never gets delivered.  The best strategy depends upon the details of 
>> what you are
>> doing.
> 
> 
> I understand that. In Python using sockets is like using sockets in C.
> 
> However, I was looking for a python equivalent of the perl idiom of 
> opening a socket and getting a file handle ... and then reading this 
> file handle as any other line oriented file using $socket->readline
> 
> I am trying to write a simple Netcraft style script which tries the HEAD 
> method on a webserver and reads the Server: header from the response. I 
> would not like to use urllib, because even a call to something like 
> urllib.info() reads in the complete webpage on an open().
> 
> Even if this particular problem could be done elegantly, I would be 
> interested to know the answer to my original question because the 
> feature of reading a line at a time from a socket will help me in a lot 
> of other places in the future.
> 
> - Sandip
> 

-- 
Lloyd Kvam
Venix Corp.
1 Court Street, Suite 378
Lebanon, NH 03766-1358

voice:	603-653-8139
fax:	801-459-9582




More information about the Tutor mailing list