Skip to content

Instantly share code, notes, and snippets.

@adiroiban
Created June 10, 2016 12:36
Show Gist options
  • Save adiroiban/609065fc492925b575f218cd6d47d9f5 to your computer and use it in GitHub Desktop.
Save adiroiban/609065fc492925b575f218cd6d47d9f5 to your computer and use it in GitHub Desktop.
#
# Run it on python 2.7 on Linux with
# 1) LANG=C python serverFromString-example.py
# 2) LANG=en_US.UTF-8 python serverFromString-example.py
from twisted.internet import reactor, endpoints
from twisted.internet.protocol import Protocol, Factory
class Echo(Protocol):
def __init__(self, factory):
self.factory = factory
def connectionMade(self):
self.factory.numProtocols = self.factory.numProtocols+1
self.transport.write(
"Welcome! There are currently %d open connections.\n" %
(self.factory.numProtocols,))
def connectionLost(self, reason):
self.factory.numProtocols = self.factory.numProtocols-1
def dataReceived(self, data):
self.transport.write(data)
class EchoFactory(Factory):
def buildProtocol(self, addr):
return Echo()
server = endpoints.serverFromString(
reactor,
u"unix:/tmp/jalepe\xf1o:backlog=7:mode=0123:lockfile=1"
)
server.listen(EchoFactory())
reactor.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment