twisted: split allocate_ports() out to util.py
so it can be used by downstream projects
This commit is contained in:
parent
eb18b1359e
commit
dc65b4354d
|
@ -1,34 +1,15 @@
|
|||
import json
|
||||
from twisted.trial import unittest
|
||||
from twisted.internet import defer, protocol, endpoints, reactor
|
||||
from twisted.internet import defer
|
||||
from twisted.application import service
|
||||
from ..servers.relay import RelayServer
|
||||
from ..twisted.transcribe import SymmetricWormhole, UsageError
|
||||
from ..twisted.util import allocate_ports
|
||||
from .. import __version__
|
||||
#from twisted.python import log
|
||||
#import sys
|
||||
#log.startLogging(sys.stdout)
|
||||
|
||||
def allocate_port():
|
||||
ep = endpoints.serverFromString(reactor, "tcp:0:interface=127.0.0.1")
|
||||
d = ep.listen(protocol.Factory())
|
||||
def _listening(lp):
|
||||
port = lp.getHost().port
|
||||
d2 = lp.stopListening()
|
||||
d2.addCallback(lambda _: port)
|
||||
return d2
|
||||
d.addCallback(_listening)
|
||||
return d
|
||||
|
||||
def allocate_ports():
|
||||
d = defer.DeferredList([allocate_port(), allocate_port()])
|
||||
def _done(results):
|
||||
port1 = results[0][1]
|
||||
port2 = results[1][1]
|
||||
return (port1, port2)
|
||||
d.addCallback(_done)
|
||||
return d
|
||||
|
||||
class Basic(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.sp = service.MultiService()
|
||||
|
|
21
src/wormhole/twisted/util.py
Normal file
21
src/wormhole/twisted/util.py
Normal file
|
@ -0,0 +1,21 @@
|
|||
from twisted.internet import defer, protocol, endpoints, reactor
|
||||
|
||||
def allocate_port():
|
||||
ep = endpoints.serverFromString(reactor, "tcp:0:interface=127.0.0.1")
|
||||
d = ep.listen(protocol.Factory())
|
||||
def _listening(lp):
|
||||
port = lp.getHost().port
|
||||
d2 = lp.stopListening()
|
||||
d2.addCallback(lambda _: port)
|
||||
return d2
|
||||
d.addCallback(_listening)
|
||||
return d
|
||||
|
||||
def allocate_ports():
|
||||
d = defer.DeferredList([allocate_port(), allocate_port()])
|
||||
def _done(results):
|
||||
port1 = results[0][1]
|
||||
port2 = results[1][1]
|
||||
return (port1, port2)
|
||||
d.addCallback(_done)
|
||||
return d
|
Loading…
Reference in New Issue
Block a user