11-07-2004, 12:39 PM | #1 (permalink) |
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() 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() 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.. |
11-07-2004, 04:11 PM | #2 (permalink) |
Tilted
|
The basic solution (not python specific) is to fork or create a thread for each incoming connection. Somewhere after you do the accept , create a thread and handle all communication from that connection in the thread. The server while loop would then go back to listening for connections and forking/creating threads as it needs.
|
11-07-2004, 05:00 PM | #3 (permalink) |
Insane
Location: West Virginia
|
Well I modified the server code a bit and got it to work somewhat but the issue was still the client would "stall" while waiting on input before it would display anything the other users would input.
One way around this I found was to use Telnet as a client... only problem is as I type each letter, it is being sent to the server; other than that, it works great! Anyone have an idea of the cause of this? And is there a way to go around it? Thanks!
__________________
- Artsemis ~~~~~~~~~~~~~~~~~~~~ There are two keys to being the best: 1.) Never tell everything you know |
11-07-2004, 05:13 PM | #4 (permalink) |
Crazy
Location: Salt Town, UT
|
Actually, I'm not sure how good Python's support of threads or shared memory is.
(which if you are forking, and want to send data between connections, you need some way of sharing memory) So what I would suggest is to use Python's select class, and you just sit there waiting on select, when something happens in the select, if it is your listening socket, you accept a new connection, add it to the list of sockets you are waiting on and go back into the loop. If you get something from one of the "clients" you loop through your connections (except the listening socket) and send the message out to each one. I'm not too familar with Python, but I found this guide by searching on google, and it seems to be pretty good. And it even recommends using threads, so there's another way to do it: http://www.amk.ca/python/howto/sockets/ |
11-07-2004, 09:46 PM | #5 (permalink) |
Insane
Location: West Virginia
|
Code:
# client import socket import thread getHOST = (raw_input('Enter the Host: ')) 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)) NAME = raw_input('Enter your name: ') s.send(NAME) data = s.recv(1024) print data def checkMessage(): while 1: try: data = s.recv(1024) print data except: return None thread.start_new_thread(checkMessage,()) while True: MSG = raw_input('>> ') s.send(MSG) #if MSG == "quit": break s.close()
__________________
- Artsemis ~~~~~~~~~~~~~~~~~~~~ There are two keys to being the best: 1.) Never tell everything you know |
Tags |
chat, program, python, simple |
|
|