Re: Reading text lines from a socket

Discussion in 'Python' started by Sandip Bhattacharya, Feb 29, 2004.

  1. [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

    --
    Sandip Bhattacharya
    sandip (at) puroga.com
    Puroga Technologies Pvt. Ltd.
    Work: http://www.puroga.com Home: http://www.sandipb.net

    GPG: 51A4 6C57 4BC6 8C82 6A65 AE78 B1A1 2280 A129 0FF3
    Sandip Bhattacharya, Feb 29, 2004
    #1
    1. Advertising

  2. Sandip Bhattacharya

    Eddie Corns Guest

    Sandip Bhattacharya <> writes:


    >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


    socket.makefile() does that.

    Eddie
    Eddie Corns, Mar 1, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lloyd Kvam
    Replies:
    1
    Views:
    336
    Donn Cave
    Feb 29, 2004
  2. Laszlo Nagy
    Replies:
    1
    Views:
    4,811
    Mark Wooding
    Jan 27, 2009
  3. Jean-Paul Calderone
    Replies:
    0
    Views:
    962
    Jean-Paul Calderone
    Jan 27, 2009
  4. Laszlo Nagy
    Replies:
    0
    Views:
    542
    Laszlo Nagy
    Feb 1, 2009
  5. Steve Holden
    Replies:
    0
    Views:
    657
    Steve Holden
    Feb 1, 2009
Loading...

Share This Page