本文整理汇总了Python中twisted.internet.abstract.FileDescriptor方法的典型用法代码示例。如果您正苦于以下问题:Python abstract.FileDescriptor方法的具体用法?Python abstract.FileDescriptor怎么用?Python abstract.FileDescriptor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.internet.abstract
的用法示例。
在下文中一共展示了abstract.FileDescriptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connectionLost
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def connectionLost(self, reason):
"""See abstract.FileDescriptor.connectionLost().
"""
# Make sure we're not called twice, which can happen e.g. if
# abortConnection() is called from protocol's dataReceived and then
# code immediately after throws an exception that reaches the
# reactor. We can't rely on "disconnected" attribute for this check
# since twisted.internet._oldtls does evil things to it:
if not hasattr(self, "socket"):
return
abstract.FileDescriptor.connectionLost(self, reason)
self._closeSocket(not reason.check(error.ConnectionAborted))
protocol = self.protocol
del self.protocol
del self.socket
del self.fileno
protocol.connectionLost(reason)
示例2: __init__
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def __init__(self, interface, proto, maxPacketSize=8192, reactor=None,
system=None):
if ethernet.IEthernetProtocol.providedBy(proto):
self.ethernet = 1
self._mode = TunnelFlags.IFF_TAP
else:
self.ethernet = 0
self._mode = TunnelFlags.IFF_TUN
assert raw.IRawPacketProtocol.providedBy(proto)
if system is None:
system = _RealSystem()
self._system = system
abstract.FileDescriptor.__init__(self, reactor)
self.interface = interface
self.protocol = proto
self.maxPacketSize = maxPacketSize
logPrefix = self._getLogPrefix(self.protocol)
self.logstr = "%s (%s)" % (logPrefix, self._mode.name)
示例3: configure
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def configure(protocol, port, pipes, interface):
remove_all()
reactor.addSystemEventTrigger('after', 'shutdown', remove_all)
# gets default (outward-facing) network interface (e.g. deciding which of
# eth0, eth1, wlan0 is being used by the system to connect to the internet)
if interface == "auto":
interface = netifaces.gateways()['default'][netifaces.AF_INET][1]
else:
if interface not in netifaces.interfaces():
raise ValueError("Given interface does not exist.", interface)
add(protocol, port, interface)
manager = libnetfilter_queue.Manager()
manager.bind(UP_QUEUE, packet_handler(manager, pipes.up))
manager.bind(DOWN_QUEUE, packet_handler(manager, pipes.down))
reader = abstract.FileDescriptor()
reader.doRead = manager.process
reader.fileno = lambda: manager.fileno
reactor.addReader(reader)
示例4: __init__
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def __init__(self, reactor, protocol, name, addr, dev=b'/dev/net/tun', mtu=1400, mode=TUN):
abstract.FileDescriptor.__init__(self, reactor)
self.queue = deque()
self.mtu = mtu
self.name = name
self.fd = os.open(dev, os.O_RDWR | os.O_NONBLOCK)
try:
# We don't need packet info
mode |= self.IFF_NO_PI
fcntl.ioctl(self.fd, self.TUNSETIFF, struct.pack('16sH', bytes(name, 'ascii'), mode))
with closing(IPRoute()) as ip:
ifidx = ip.link_lookup(ifname=name)[0]
_addr, _mask = addr.split('/')
ip.link('set', index=ifidx, state='up', mtu=self.mtu)
ip.addr('add', index=ifidx, address=_addr, prefixlen=int(_mask))
except Exception:
os.close(self.fd)
raise
# Connect protocol
self.protocol = protocol
self.protocol.makeConnection(self)
self.connected = 1
self.startReading()
示例5: doWrite
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def doWrite(self):
# Retry disconnecting
if self.disconnected:
# This case is triggered when "disconnected" is set to True by a
# call to _postLoseConnection from FileDescriptor.doWrite (to which
# we upcall at the end of this overridden version of that API). It
# means that while, as far as any protocol connected to this
# transport is concerned, the connection no longer exists, the
# connection *does* actually still exist. Instead of closing the
# connection in the overridden _postLoseConnection, we probably
# tried (and failed) to send a TLS close alert. The TCP connection
# is still up and we're waiting for the socket to become writeable
# enough for the TLS close alert to actually be sendable. Only
# then will the connection actually be torn down. -exarkun
return self._postLoseConnection()
if self._writeDisconnected:
return self._closeWriteConnection()
if self.readBlockedOnWrite:
self.readBlockedOnWrite = 0
self._resetReadWrite()
return Connection.doWrite(self)
示例6: connectionLost
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def connectionLost(self, reason):
"""
Called when the serial port disconnects.
Will call C{connectionLost} on the protocol that is handling the
serial data.
"""
self.reactor.removeEvent(self._overlappedRead.hEvent)
self.reactor.removeEvent(self._overlappedWrite.hEvent)
abstract.FileDescriptor.connectionLost(self, reason)
self._serial.close()
self.protocol.connectionLost(reason)
示例7: __init__
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def __init__(self, skt, protocol, reactor=None):
abstract.FileDescriptor.__init__(self, reactor=reactor)
self.socket = skt
self.socket.setblocking(0)
self.fileno = skt.fileno
self.protocol = protocol
示例8: __init__
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def __init__(self, protocol, deviceNameOrPortNumber, reactor,
baudrate = 9600, bytesize = EIGHTBITS, parity = PARITY_NONE,
stopbits = STOPBITS_ONE, timeout = 0, xonxoff = 0, rtscts = 0):
abstract.FileDescriptor.__init__(self, reactor)
self._serial = self._serialFactory(
deviceNameOrPortNumber, baudrate=baudrate, bytesize=bytesize,
parity=parity, stopbits=stopbits, timeout=timeout,
xonxoff=xonxoff, rtscts=rtscts)
self.reactor = reactor
self.flushInput()
self.flushOutput()
self.protocol = protocol
self.protocol.makeConnection(self)
self.startReading()
示例9: connectionLost
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def connectionLost(self, reason):
"""
Called when the serial port disconnects.
Will call C{connectionLost} on the protocol that is handling the
serial data.
"""
abstract.FileDescriptor.connectionLost(self, reason)
self._serial.close()
self.protocol.connectionLost(reason)
示例10: write
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def write(self, data):
self.stopReading()
abstract.FileDescriptor.write(self, data)
示例11: connectionLost
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def connectionLost(self, reason):
"""
See abstract.FileDescriptor.connectionLost.
"""
# At least on OS X 10.4, exiting while stdout is non-blocking can
# result in data loss. For some reason putting the file descriptor
# back into blocking mode seems to resolve this issue.
fdesc.setBlocking(self.fd)
abstract.FileDescriptor.connectionLost(self, reason)
self.proc.childConnectionLost(self.name, reason)
示例12: __init__
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def __init__(self, reactor, proc, name, fileno):
"""
Initialize, specifying a process to connect to.
"""
abstract.FileDescriptor.__init__(self, reactor)
fdesc.setNonBlocking(fileno)
self.proc = proc
self.name = name
self.fd = fileno
self.startReading()
示例13: test_shutdownDisconnectsCleanly
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def test_shutdownDisconnectsCleanly(self):
"""
A L{IFileDescriptor.connectionLost} implementation which raises an
exception does not prevent the remaining L{IFileDescriptor}s from
having their C{connectionLost} method called.
"""
lostOK = [False]
# Subclass FileDescriptor to get logPrefix
class ProblematicFileDescriptor(FileDescriptor):
def connectionLost(self, reason):
raise RuntimeError("simulated connectionLost error")
class OKFileDescriptor(FileDescriptor):
def connectionLost(self, reason):
lostOK[0] = True
reactor = self.buildReactor()
# Unfortunately, it is necessary to patch removeAll to directly control
# the order of the returned values. The test is only valid if
# ProblematicFileDescriptor comes first. Also, return these
# descriptors only the first time removeAll is called so that if it is
# called again the file descriptors aren't re-disconnected.
fds = iter([ProblematicFileDescriptor(), OKFileDescriptor()])
reactor.removeAll = lambda: fds
reactor.callWhenRunning(reactor.stop)
self.runReactor(reactor)
self.assertEqual(len(self.flushLoggedErrors(RuntimeError)), 1)
self.assertTrue(lostOK[0])
示例14: testSendingEmptyFile
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def testSendingEmptyFile(self):
fileSender = basic.FileSender()
consumer = abstract.FileDescriptor()
consumer.connected = 1
emptyFile = BytesIO(b'')
d = fileSender.beginFileTransfer(emptyFile, consumer, lambda x: x)
# The producer will be immediately exhausted, and so immediately
# unregistered
self.assertIsNone(consumer.producer)
# Which means the Deferred from FileSender should have been called
self.assertTrue(d.called,
'producer unregistered with deferred being called')
示例15: test_doubleProducer
# 需要导入模块: from twisted.internet import abstract [as 别名]
# 或者: from twisted.internet.abstract import FileDescriptor [as 别名]
def test_doubleProducer(self):
"""
Verify that registering a non-streaming producer invokes its
resumeProducing() method and that you can only register one producer
at a time.
"""
fd = abstract.FileDescriptor()
fd.connected = 1
dp = DummyProducer()
fd.registerProducer(dp, 0)
self.assertEqual(dp.events, ['resume'])
self.assertRaises(RuntimeError, fd.registerProducer, DummyProducer(), 0)