View Single Post
Old 11-07-2004, 12:39 PM   #1 (permalink)
Artsemis
Insane
 
Location: West Virginia
[Python] Simple chat program (EDIT: Now a TELNET Question)

Okay basically I need to write a simple chat program. So far I have the following for the server and client code:

Code:
# server
import socket
import thread

HOST = 'localhost'               # Symbolic name meaning the local host
PORT = 150                       # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))

while True:
    s.listen(1)
    conn, addr = s.accept()
    print 'Connected by', addr
    while 1:
        data = conn.recv(1024)
        if data[:4] == "exit": print 'Parted by', addr
        else:   conn.send(data),
        if not data: break
        print `data`
conn.close()
and...
Code:
# client
import socket
getHOST = (raw_input('Enter the Host: '))
print getHOST
HOST = getHOST
PORT = 150        # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))

NICK = raw_input('Welcome, Enter your name: ')

while True:
    MSG = raw_input('>> ')
    MSG = NICK + ': ' + MSG
    if MSG[5:] == "exit": break
    s.send(MSG)
    data = s.recv(1024)
    print data
s.close()
The main issue is this:
I have no idea to allow it to accept connections from multiple clients. I need it to basically keep checking for a new connection. After that is done, I can already tell I'm going to have an issue with being able to receive messages from other users while I am sitting at the prompt to send a message- since that will be holding up the program.

Anyone have some experience with this?
__________________

- Artsemis
~~~~~~~~~~~~~~~~~~~~
There are two keys to being the best:
1.) Never tell everything you know

Last edited by Artsemis; 11-07-2004 at 05:00 PM..
Artsemis is offline  
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62