[Twisted-Python] Twisted OpenSSL Server and multiple clients

gary clark burslem2001 at yahoo.com
Sun Sep 27 15:02:37 MDT 2009


Ok I'm answering my own questions:

After browsing around I kinda pieced together what is going on.

Basically the factory object manages the persistent information
of protcols. You can use self.clients.append( newclient )
to store when a connection is made and self.clients.remove( client ) when a client disconnects within the factory object to cycle through all the protocol (clients) in the factory:

In the factory:

for proto in self.clients
    proto.transport.write( message )

I found the below code very useful:

import string
from twisted.application import service, internet
from twisted.internet import reactor, protocol
from twisted.internet.protocol import Protocol, Factory

port = 80

class AirChat(Protocol):
	
	username = ""
	session = False
	
	def connectionMade( self ):
		self.factory.numProtocols = self.factory.numProtocols+1
		self.factory.addClient(  self )
		
	def connectionLost( self , reason ):
		self.factory.numProtocols = self.factory.numProtocols-1
		self.factory.delClient( self )
		
	def dataReceived( self , data ):
		
		# parse out HTTP
		a = data.split( '\x00' )[0].split(' ')
		
		# look for HTTP header
		if a[0] == 'GET' or a[0] == 'POST':
			self.transport.write( """HTTP/1.1 200 OK\r\n\r\n""" )
			self.transport.write( """<html><strong>AIR Chat 1.0</strong></html>""" )
			self.transport.loseConnection()
			return
			
		a = data.split( '\x00' )[0].split( '||' )
		
		if a[0] == '1':
			self.factory.sendAll( '1||' + self.factory.stripChars( a[1] ) + '||' + self.factory.stripChars( a[2] ) + '\x00'  )
			
		elif a[0] == '2':
			self.transport.write( '2||' + str( self.factory.numProtocols )  + '\x00')


class AirChatFactory(Factory):
	protocol = AirChat
	numProtocols = 0
	clients = []
	
	def stripChars(self, textIn):
		textIn = textIn.replace('<', '')
		textIn = textIn.replace('</', '')
		textIn = textIn.replace('>', '')
		textIn = textIn.replace('/>', '')
		return textIn
	
	def addClient(self, newclient):
		self.clients.append( newclient )
	
	def delClient(self, client):
		self.clients.remove( client )
	
	def sendAll(self, message):
		for proto in self.clients:
			proto.transport.write( message )
	
	
class AirChatService( internet.TCPServer ):
	def __init__(self):
		internet.TCPServer.__init__(self, 80, AirChatFactory())


I think thats good for now. I dont have to create my own repository just manipulate the factory object, marvelous. Time for a pint.

Thanks,
Garyc

--- On Sun, 9/27/09, gary clark <burslem2001 at yahoo.com> wrote:

> From: gary clark <burslem2001 at yahoo.com>
> Subject: Twisted OpenSSL Server and multiple clients
> To: twisted-python at twistedmatrix.com
> Date: Sunday, September 27, 2009, 3:08 PM
> Hello,
> 
> I am new to Twisted, hence the red flag and patience card
> pulled out.
> 
> I have written a client that connects perfectly well to a
> twisted server thats using open-ssl to communicate to each
> other. Basically I took the open-ssl echo server example and
> tweaked it.
> 
> Thats all fine and dandy, but I want to have multiple
> clients that can connect to the server.
> 
> So I have a few questions:
> 
> 1) When a client connects to my server? What connection
> information
>    can be collected and stored persistently?
> 
> 
>    Upon a client connecting I want to store
> connection information and
>    I want to remove connection information
> when its disconnected from 
>    the persistent store.
> 
> 2) What persistent store is adopted for twisted if any?
> 
> 3) I want to know when a table has changed so I can then
> pull information
>    from a table and use the connection
> information if present to send data 
>    to my client. How is this accomplished in
> twisted?
> 
> Again I apologise in advance, if someone can shed some
> light into how twisted accomplishes the basic tasks of
> multiple clients thats great.
> 
> Oh boy.
> 
> Thanks,
> Garyc
> 
> 
> 
> 





More information about the Twisted-Python mailing list