Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
twisted socket module python
01-24-2010, 03:48 PM (This post was last modified: 01-24-2010 05:33 PM by codecaine.)
Post: #1
twisted socket module python
Python sockets is already easy but this really dumbs down the sockets. Has support for all types of sockets like aim, icq, jabber, tcp, ssh, etc...
http://twistedmatrix.com/trac/

here is a example of making a servers. It is so plain and simple. I can't believe its not butter Smile
http://twistedmatrix.com/documents/curre...rvers.html
Example of a chat server so you don't have to build one you can just modify off this one.
Code:
"""The most basic chat protocol possible.

run me with twistd -y chatserver.py, and then connect with multiple
telnet clients to port 1025
"""

from twisted.protocols import basic


#chat class
class MyChat(basic.LineReceiver):
    #if somebody connects to the server add them to the client list
    def connectionMade(self):
        print "Got new client!"
        self.factory.clients.append(self)

    #detect if a connection is lost if so remove the client from the list
    def connectionLost(self, reason):
        print "Lost a client!"
        self.factory.clients.remove(self)

    #recieve a message from a client and then broadcast it to all the other clients
    def lineReceived(self, line):
        print "received", repr(line)
        for c in self.factory.clients:
            c.message(line)

    #write message to server log file
    def message(self, message):
        self.transport.write(message + '\n')


from twisted.internet import protocol
from twisted.application import service, internet

factory = protocol.ServerFactory()
#Mychat is the protocol
factory.protocol = MyChat
#start with empty client list
factory.clients = []
#set application service
application = service.Application("chatserver")
#set the chat port on 1025 and start it
internet.TCPServer(1025, factory).setServiceParent(application)
Visit this user's website Find all posts by this user
Quote this message in a reply
01-24-2010, 09:19 PM
Post: #2
RE: twisted socket module python
Very interesting ! It is so easy to work in Python with sockets. Almost the same as in Delphi !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: