telnet --- sockets ?

Joshua Muskovitz joshm at taconic.net
Tue Jan 22 19:35:29 EST 2002


"Grant Edwards" <grante at visi.com> wrote in message
news:Y9m38.6275$Wf1.2170152 at ruti.visi.com...
> Ah, he forgot to set the SO_INSTANEOUS flag!

rotflmao.  Thanks for the best chuckle of the day.  :-)

(Note to Max:  SO_* flags are bitwise flags used in the setsockopt() call in
most languages, for setting all sorts of interesting aspects of the socket.
There is no SO_INSTANTANEOUS flag, unfortunately.  It would make coding this
stuff a *lot* easier!)

> > they work.  I stongly recommend _UNIX Network Programming_ by
> > W. Richard Stevens as a book to teach you more than you want to
> > know about them.
>
> It may be more than you want to know -- but, when it comes to
> sockets: more than you want to know is pretty much what it
> takes to do it right.

This is exactly correct, and was the reason I specified a rather thick book
on the subject.  If you want to really understand what's going on at the TCP
and IP layers, you can't just read one of those "Teach yourself TCP in two
hours" books.  There is a *lot* going on behind the scenes.

Max, the following examples are lousy examples of Python code (too many
string concatenations, among other things), but they might help you get the
idea for sockets:

# pushes an entire string to a socket, causes an exception if the connection
breaks
def _SafeSend(sock,msg):
    msglen = len(msg)
    totalsent = 0
    while totalsent < msglen:
        sent = sock.send(msg[totalsent:])
        if sent == 0:
            raise RuntimeError, "connection broken"
        totalsent = totalsent + sent

# handy for reading exactly msglen bytes from a socket (will hang until they
are received)
def _SafeRecv(sock,msglen):
    msg = ""
    while len(msg) < msglen:
        msg = msg + sock.recv(msglen - len(msg))
    return msg

# pulls in one character at a time to simulate readline()
def _ReadLine(sock):
    msg = ""
    while 1:
        c = sock.recv(1)
        if c == "\n":
            return msg
        msg = msg + c

--
# Joshua Muskovitz
# joshm at taconic.net
def lyyrs(sig): return '-'.join(sig.split()+["ly y'rs"])
lyyrs('Hire me!  I need the work!')




-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----



More information about the Python-list mailing list