Re: [Tutor] Re: Reading text lines from a socket

Discussion in 'Python' started by Lloyd Kvam, Feb 29, 2004.

  1. Lloyd Kvam

    Lloyd Kvam Guest

    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
    Lloyd Kvam, Feb 29, 2004
    #1
    1. Advertising

  2. Lloyd Kvam

    Donn Cave Guest

    Quoth Bob Ippolito <>:
    | The real solution is just to use a layer of abstraction, like
    | asyncore/medusa, Twisted, etc. Relying on the fact that your operating
    | system may or may not allow a socket to act sort of like a file is
    | really not something you should do, from Python or otherwise.
    |
    | socket.makefile is platform specific, at best.

    Is it? Do you know of a platform where it doesn't work? I won't
    laugh -- I use Python on BeOS. But there, where in fact sockets
    can't be treated as files, makefile() works anyway because it makes
    a Python class instance that's like a file object, but uses recv()
    and send() instead of the read(2) and write(2) system calls that the
    file object uses via stdio.

    I don't use it myself, but because I just don't like the way stdio
    buffering works with devices like pipes and sockets. Haven't noticed
    a platform portability problem.

    Donn Cave,
    Donn Cave, Feb 29, 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. Sandip Bhattacharya

    Re: Reading text lines from a socket

    Sandip Bhattacharya, Feb 29, 2004, in forum: Python
    Replies:
    1
    Views:
    323
    Eddie Corns
    Mar 1, 2004
  2. le dahut

    Re: [Tutor] socket and lost data

    le dahut, Jan 27, 2006, in forum: Python
    Replies:
    1
    Views:
    317
    Ben Sizer
    Jan 30, 2006
  3. Laszlo Nagy
    Replies:
    1
    Views:
    4,806
    Mark Wooding
    Jan 27, 2009
  4. Jean-Paul Calderone
    Replies:
    0
    Views:
    960
    Jean-Paul Calderone
    Jan 27, 2009
  5. Laszlo Nagy
    Replies:
    0
    Views:
    542
    Laszlo Nagy
    Feb 1, 2009
Loading...

Share This Page