Created
June 10, 2016 12:36
-
-
Save adiroiban/609065fc492925b575f218cd6d47d9f5 to your computer and use it in GitHub Desktop.
Used to test https://twistedmatrix.com/trac/ticket/8422
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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