[Edu-sig] RE : walky-talky

Arthur Siegel siegel@eico.com
Wed, 5 Apr 2000 18:49:50 -0400


David Scherer got the walky-talky done in 30 lines of code.

I ran two localhost sessions and had my DOS prompt talking to
my DOS prompt.

Putting aside the technical stuff related to the socket module, the
thirty lines cover an awful lot of Python syntax.

I think a lot of students would be all ears in getting a handle on this kind
of magic.

Below is David's e-mail:





-----Original Message-----
From: David Scherer [mailto:dscherer@cmu.edu]
Sent: Wednesday, April 05, 2000 11:09 AM
To: asiegel
Subject: RE: [Edu-sig] Walky-talky help




Arthur,

A good place to start is the Python library reference for the socket
module,
which has some example code.  Starting there, I was able to whip up the
following in just a few minutes.  It's not going to replace IRC or AIM any
time soon, but I assume that's not what you were looking for.

Dave

# Primitive Walky-talky (sp?)
# Based on the example code for 'socket' in the Python
#   library reference

from socket import *
HOST = 'localhost'        # Computer to connect to
LOCAL = ''                # Symbolic name for localhost
PORT = 50007              # Arbitrary non-privileged server

try:   # try to be the client
    print 'Trying to connect.'
    s = socket(AF_INET, SOCK_STREAM)
    s.connect((HOST,PORT))
    print 'Walky-talky connected to', HOST
except error:
    # Didn't work, try to be the server
    print 'Waiting for a connection.'
    s = socket(AF_INET, SOCK_STREAM)
    s.bind((HOST, PORT))
    s.listen(1)
    s, addr = s.accept()
    print 'Walky-talky connected to', addr[0]
    s.send('Hello!\n')   # someone needs to send first

print 'You must take turns speaking.'
print 'You will see a > when it is your turn to speak.'

try:
    while 1:
        data = s.recv(1024)
        if not data: break
        print data,

        data = raw_input("> ")
        s.send(data+'\n')

    s.close()
except error:
    pass

print 'Connection terminated.'