Created
July 27, 2011 14:22
-
-
Save iffy/1109449 to your computer and use it in GitHub Desktop.
blkandtx
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
$ python prob.py | |
2011-07-27 08:38:43-0600 [-] Log opened. | |
2011-07-27 08:38:43-0600 [-] Connected to process | |
2011-07-27 08:38:43-0600 [-] [Errno 61] Connection refused | |
2011-07-27 08:38:43-0600 [-] error connecting 0 sleep for 1 second | |
2011-07-27 08:38:44-0600 [-] [Errno 22] Invalid argument | |
2011-07-27 08:38:44-0600 [-] error connecting 1 sleep for 1 second | |
2011-07-27 08:38:45-0600 [-] [Errno 22] Invalid argument | |
2011-07-27 08:38:45-0600 [-] error connecting 2 sleep for 1 second | |
2011-07-27 08:38:46-0600 [-] [Errno 22] Invalid argument | |
2011-07-27 08:38:46-0600 [-] error connecting 3 sleep for 1 second | |
2011-07-27 08:38:47-0600 [-] [Errno 22] Invalid argument | |
2011-07-27 08:38:47-0600 [-] error connecting 4 sleep for 1 second | |
2011-07-27 08:38:48-0600 [-] about to send foo | |
2011-07-27 08:38:48-0600 [-] [Failure instance: Traceback: <class 'socket.error'>: [Errno 32] Broken pipe | |
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py:504:__bootstrap | |
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py:532:__bootstrap_inner | |
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/threading.py:484:run | |
--- <exception caught here> --- | |
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/Twisted-10.2.0-py2.6-macosx-10.3-fat.egg/twisted/python/threadpool.py:207:_worker | |
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/Twisted-10.2.0-py2.6-macosx-10.3-fat.egg/twisted/python/context.py:59:callWithContext | |
/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/Twisted-10.2.0-py2.6-macosx-10.3-fat.egg/twisted/python/context.py:37:callWithContext | |
prob.py:42:sockit | |
] | |
2011-07-27 08:38:48-0600 [-] Process ended | |
2011-07-27 08:38:48-0600 [-] Main loop terminated. |
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
from twisted.internet import reactor, defer, threads | |
from twisted.internet.protocol import Factory, ProcessProtocol | |
from twisted.python import log | |
import sys | |
import socket | |
import time | |
class MyProto(ProcessProtocol): | |
def __init__(self): | |
self.done = defer.Deferred() | |
self.connect_d = defer.Deferred() | |
def connectionMade(self): | |
print 'Connected to process' | |
self.connect_d.callback(self) | |
def processEnded(self, status): | |
print 'Process ended' | |
self.done.callback(status) | |
def sockit(): | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
for i in xrange(5): | |
try: | |
s.connect(('127.0.0.1', 7777)) | |
break | |
except socket.error, e: | |
print e | |
print 'error connecting', i, 'sleep for 1 second' | |
time.sleep(1) | |
print 'about to send foo' | |
s.send('foo') | |
print 'sent foo' | |
r = s.recv(16) | |
print 'recv %s' % r | |
s.close() | |
def main(): | |
proto = MyProto() | |
reactor.spawnProcess(proto, sys.executable, [sys.executable, 'pserv.py']) | |
def startSockit(_): | |
return threads.deferToThread(sockit) | |
d = proto.connect_d.addCallback(startSockit) | |
def kill(r): | |
try: proto.transport.loseConnection() | |
except: pass | |
try: | |
proto.transport.signalProcess('KILL') | |
except: | |
pass | |
log.msg(r) | |
return proto.done | |
d.addBoth(kill) | |
def stop(_): | |
reactor.stop() | |
d.addBoth(stop) | |
if __name__ == '__main__': | |
log.startLogging(sys.stdout) | |
reactor.callWhenRunning(main) | |
reactor.run() |
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
from twisted.internet import reactor | |
from twisted.internet.protocol import Factory, Protocol | |
import sys | |
class EchoProto(Protocol): | |
def dataReceived(self, data): | |
self.transport.write(data) | |
sys.stdout.write('-->%s<--' % data) | |
sys.stdout.flush() | |
def connectionLost(self, reason): | |
print 'connection lost' | |
reactor.stop() | |
f = Factory() | |
f.protocol = EchoProto | |
reactor.listenTCP(7777, f) | |
reactor.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment