Ver Mensaje Individual
  #2 (permalink)  
Antiguo 28/12/2013, 21:16
Avatar de razpeitia
razpeitia
Moderador
 
Fecha de Ingreso: marzo-2005
Ubicación: Monterrey, México
Mensajes: 7.321
Antigüedad: 19 años, 9 meses
Puntos: 1360
Respuesta: Chat sencillo TCP

Ok, no se cual sea el problema.

Pero si quieres hacer un chat de sockets robusto pero sencillo. Puedes usar select.

server.py
Código Python:
Ver original
  1. import select
  2. import socket
  3. import sys
  4. import Queue
  5.  
  6. # Create a TCP/IP socket
  7. server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  8. server.setblocking(0)
  9.  
  10. # Bind the socket to the port
  11. server_address = ('localhost', 10000)
  12. print >>sys.stderr, 'starting up on %s port %s' % server_address
  13. server.bind(server_address)
  14.  
  15. # Listen for incoming connections
  16. server.listen(5)
  17.  
  18. # Sockets from which we expect to read
  19. inputs = [ server ]
  20.  
  21. # Sockets to which we expect to write
  22. outputs = [ ]
  23.  
  24. # Outgoing message queues (socket:Queue)
  25. message_queues = {}
  26.  
  27. while inputs:
  28.     # Wait for at least one of the sockets to be ready for processing
  29.     print >>sys.stderr, '\nwaiting for the next event'
  30.     readable, writable, exceptional = select.select(inputs, outputs, inputs)
  31.    
  32.     # Handle inputs
  33.     for s in readable:
  34.         if s is server:
  35.             # A "readable" server socket is ready to accept a connection
  36.             connection, client_address = s.accept()
  37.             print >>sys.stderr, 'new connection from', client_address
  38.             connection.setblocking(0)
  39.             inputs.append(connection)
  40.  
  41.             # Give the connection a queue for data we want to send
  42.             message_queues[connection] = Queue.Queue()
  43.         else:
  44.             data = s.recv(1024)
  45.             if data:
  46.                 # A readable client socket has data
  47.                 print >>sys.stderr, 'received "%s" from %s' % (data, s.getpeername())
  48.                 message_queues[s].put(data)
  49.                 # Add output channel for response
  50.                 if s not in outputs:
  51.                     outputs.append(s)
  52.             else:
  53.                 # Interpret empty result as closed connection
  54.                 print >>sys.stderr, 'closing', client_address, 'after reading no data'
  55.                 # Stop listening for input on the connection
  56.                 if s in outputs:
  57.                     outputs.remove(s)
  58.                 inputs.remove(s)
  59.                 s.close()
  60.  
  61.                 # Remove message queue
  62.                 del message_queues[s]
  63.     # Handle outputs
  64.     for s in writable:
  65.         try:
  66.             next_msg = message_queues[s].get_nowait()
  67.         except Queue.Empty:
  68.             # No messages waiting so stop checking for writability.
  69.             print >>sys.stderr, 'output queue for', s.getpeername(), 'is empty'
  70.             outputs.remove(s)
  71.         else:
  72.             print >>sys.stderr, 'sending "%s" to %s' % (next_msg, s.getpeername())
  73.             s.send(next_msg)
  74.     # Handle "exceptional conditions"
  75.     for s in exceptional:
  76.         print >>sys.stderr, 'handling exceptional condition for', s.getpeername()
  77.         # Stop listening for input on the connection
  78.         inputs.remove(s)
  79.         if s in outputs:
  80.             outputs.remove(s)
  81.         s.close()
  82.  
  83.         # Remove message queue
  84.         del message_queues[s]

client.py
Código Python:
Ver original
  1. import socket
  2. import sys
  3.  
  4. messages = [ 'This is the message. ',
  5.              'It will be sent ',
  6.              'in parts.',
  7.              ]
  8. server_address = ('localhost', 10000)
  9.  
  10. # Create a TCP/IP socket
  11. socks = [ socket.socket(socket.AF_INET, socket.SOCK_STREAM),
  12.           socket.socket(socket.AF_INET, socket.SOCK_STREAM),
  13.           ]
  14.  
  15. # Connect the socket to the port where the server is listening
  16. print >>sys.stderr, 'connecting to %s port %s' % server_address
  17. for s in socks:
  18.     s.connect(server_address)
  19.  
  20. for message in messages:
  21.     # Send messages on both sockets
  22.     for s in socks:
  23.         print >>sys.stderr, '%s: sending "%s"' % (s.getsockname(), message)
  24.         s.send(message)
  25.  
  26.     # Read responses on both sockets
  27.     for s in socks:
  28.         data = s.recv(1024)
  29.         print >>sys.stderr, '%s: received "%s"' % (s.getsockname(), data)
  30.         if not data:
  31.             print >>sys.stderr, 'closing socket', s.getsockname()
  32.             s.close()

Supongo que puedes modificar esto para que funcione como un chat.