In my attempts to learn networking in Python, I have created a simple server-client program. The server creates a socket, and checks for connections, then sends a message to all connected entities. Like so,
import socket,os
ip=''
port=242
sock = socket.socket()
sock.bind((ip,port))
sock.listen(5)
clients=[]
while True:
clients.append(sock.accept())
os.system('cls')
print 'connected to: '
for client in clients:
print client[1]
message=raw_input('> ')
for client in clients:
try:
client[0].send(message)
except:
clients.remove(client)
The program however appears to stop, until it receives a connection. This is somewhat of a predicament if I wish to implement the chatting element of a chat. I understand that I can place the socket.accept()
outside of the loop, however I intend to have multiple clients (hence the .append()
).
I have provided my client code in case that has any affect.
import socket
port=int(raw_input('enter the socket adress: '))
sock=socket.socket()
sock.connect(('localhost',port))
connected=True
while connected:
data=sock.recv(1024)
if data=='#exit':
connected=False
else:
print data
Please when writing responses, take into account that I am very much a beginner in regard to this (If that is not obvious from the format of my program... :'[ )
Notice: I am trying to avoid the select()
method, because I do not yet understand it.