Changes between Version 213 and Version 214 of WikiStart


Ignore:
Timestamp:
03/26/2012 01:05:43 PM (6 years ago)
Author:
Itamar Turner-Trauring
Comment:

Switch to tabbed code examples

Legend:

Unmodified
Added
Removed
Modified
  • WikiStart

    v213 v214  
    115115
    116116
    117 == Easy Custom Servers and Clients ==
    118 
    119 Twisted makes it easy to implement custom network applications, both servers and clients. Here's a TCP server that echoes back everything that's written to it:
     117== Code Examples ==
     118
     119{{{
     120#!html
     121<ul id="frontpage-tabs" class="nav nav-tabs">
     122    <li class="active"><a href="#echoserver">Echo Server</a></li>
     123    <li><a href="#webserver">Web server</a></li>
     124    <li><a href="#pubsubserver">Publish/subscribe</a></li>
     125</ul>
     126}}}
     127
     128{{{
     129#!div class="tab-content"
     130
     131{{{
     132#!div class="tab-pane active" id="echoserver"
     133
     134Twisted makes it easy to implement custom network applications. Here's a TCP server that echoes back everything that's written to it:
    120135
    121136{{{
     
    136151
    137152Learn more about [http://twistedmatrix.com/documents/current/core/howto/servers.html writing servers], [http://twistedmatrix.com/documents/current/core/howto/clients.html writing clients] and the [http://twistedmatrix.com/documents/current/core/howto/index.html core networking libraries ], including support for SSL, UDP, scheduled events, unit testing infrastructure, and much more.
    138 
    139 
    140 == Event-Driven Web Applications ==
    141 
    142 Twisted includes an event-driven web server. Here's a sample web application:
     153}}}
     154
     155
     156{{{
     157#!div class="tab-pane" id="webserver"
     158Twisted includes an event-driven web server. Here's a sample web application; notice how the resource object persists in memory, rather than being recreated on each request:
    143159
    144160{{{
     
    161177
    162178Learn more about [http://twistedmatrix.com/documents/current/web/howto/web-in-60/index.html web application development], [http://twistedmatrix.com/documents/current/web/howto/twisted-templates.html templates] and Twisted's [http://twistedmatrix.com/documents/current/web/howto/client.html HTTP client].
     179}}}
     180
     181{{{
     182#!div class="tab-pane" id="pubsubserver"
     183Here's a simple publish/subscribe server, where clients see all messages posted by other clients:
     184
     185{{{
     186#!python
     187from twisted.internet import reactor, protocol
     188from twisted.protocols import basic
     189
     190class PubProtocol(basic.LineReceiver):
     191    delimiter = "\n"
     192
     193    def __init__(self, factory):
     194        self.factory = factory
     195
     196    def connectionMade(self):
     197        self.factory.clients.add(self)
     198
     199    def connectionLost(self, reason):
     200        self.factory.clients.remove(self)
     201
     202    def lineReceived(self, line):
     203        message = "<%s> %s\n" % (self.transport.getHost(), line)
     204        for c in self.factory.clients:
     205            if c != self:
     206                c.transport.write(message)
     207
     208
     209class PubFactory(protocol.Factory):
     210    def __init__(self):
     211        self.clients = set()
     212
     213    def buildProtocol(self, addr):
     214        return PubProtocol(self)
     215
     216
     217if __name__ == '__main__':
     218    reactor.listenTCP(1025, PubFactory())
     219    reactor.run()
     220}}}
     221
     222}}}
     223
     224
     225}}}
    163226
    164227