خرید بک لینک

Vote count: 0

I found this code for a simple chat server here http://www.ibm.com/developerworks/linux/tutorials/l-pysocks/ and it works using telnet. It works so beautifully and i really want to modify it to have a GUI.
I don't understand how do i have the clients type in the messages through a GUI window instead of the terminal using telnet.I am attaching the code for ease :

import socket
import select

class ChatServer:

  def __init__( self, port ):
    self.port = port;

    self.srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
    self.srvsock.setsockopt<( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
    self.srvsock.bind( ("", port) )
    self.srvsock.listen( 5 )

    self.descriptors = [self.srvsock]
    print 'ChatServer started on port %s' % port

  def run( self ):
    while 1:

      # Await an event on a readable socket descriptor
      (sread, swrite, sexc) = select.select( self.descriptors, [], [] )

      # Iterate through the tagged read descriptors
      for sock in sread:

        # Received a coect to the server (listening) socket
        if sock == self.srvsock:
          self.accept_new_coection()
        else:

          # Received something on a client socket
          str = sock.recv(100)

          # Check to see if the peer socket closed
          if str == '':
            host,port = sock.getpeeame()
            str = 'Client left %s:%s' % (host, port)
            self.broadcast_string( str, sock )
            sock.close
            self.descriptors.remove(sock)
          else:
            host,port = sock.getpeeame()
            newstr = '[%s:%s] %s' % (host, port, str)
            self.broadcast_string( newstr, sock )

  def broadcast_string( self, str, omit_sock ):
    for sock in self.descriptors:
      if sock != self.srvsock and sock != omit_sock:
        sock.send(str)

    print str,

  def accept_new_coection( self ):
    newsock, (remhost, remport) = self.srvsock.accept()
    self.descriptors.append( newsock )

    newsock.send("You're coected to the Python chatserver")
    str = 'Client joined %s:%s' % (remhost, remport)
    self.broadcast_string( str, newsock )


myServer = ChatServer( 2626 )
myServer.run()

Please guide me as to how I may add in the GUI using tkinter. Should there be separate client script or some thing else?

asked 1 min ago

- - , .
.

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 21 تير 1395 ساعت: 14:07

صفحه بندی