本文整理汇总了Python中twisted.internet.fdesc._setCloseOnExec函数的典型用法代码示例。如果您正苦于以下问题:Python _setCloseOnExec函数的具体用法?Python _setCloseOnExec怎么用?Python _setCloseOnExec使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_setCloseOnExec函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: doRead
def doRead(self):
"""Called when my socket is ready for reading.
This accepts a connection and calls self.protocol() to handle the
wire-level protocol.
"""
try:
if platformType == "posix":
numAccepts = self.numberAccepts
else:
# win32 event loop breaks if we do more than one accept()
# in an iteration of the event loop.
numAccepts = 1
for i in range(numAccepts):
# we need this so we can deal with a factory's buildProtocol
# calling our loseConnection
if self.disconnecting:
return
try:
skt, addr = self.socket.accept()
except socket.error, e:
if e.args[0] in (EWOULDBLOCK, EAGAIN):
self.numberAccepts = i
break
elif e.args[0] == EPERM:
# Netfilter on Linux may have rejected the
# connection, but we get told to try to accept()
# anyway.
continue
elif e.args[0] in (EMFILE, ENOBUFS, ENFILE, ENOMEM, ECONNABORTED):
# Linux gives EMFILE when a process is not allowed
# to allocate any more file descriptors. *BSD and
# Win32 give (WSA)ENOBUFS. Linux can also give
# ENFILE if the system is out of inodes, or ENOMEM
# if there is insufficient memory to allocate a new
# dentry. ECONNABORTED is documented as possible on
# both Linux and Windows, but it is not clear
# whether there are actually any circumstances under
# which it can happen (one might expect it to be
# possible if a client sends a FIN or RST after the
# server sends a SYN|ACK but before application code
# calls accept(2), however at least on Linux this
# _seems_ to be short-circuited by syncookies.
log.msg("Could not accept new connection (%s)" % (
errorcode[e.args[0]],))
break
raise
fdesc._setCloseOnExec(skt.fileno())
protocol = self.factory.buildProtocol(self._buildAddr(addr))
if protocol is None:
skt.close()
continue
s = self.sessionno
self.sessionno = s+1
transport = self.transport(skt, protocol, addr, self, s, self.reactor)
protocol.makeConnection(transport)
else:
示例2: create_stream_socket
def create_stream_socket(addressFamily, shared=False):
"""
Create a new socket for use with Twisted's IReactor.adoptStreamPort.
:param addressFamily: The socket address family.
:type addressFamily: One of socket.AF_INET, socket.AF_INET6, socket.AF_UNIX
:param shared: If `True`, request to create a shared, load-balanced socket.
When this feature is not available, throw an exception.
:type shared: bool
:returns obj -- A socket.
"""
s = socket.socket(addressFamily, socket.SOCK_STREAM)
s.setblocking(0)
fdesc._setCloseOnExec(s.fileno())
if platformType == "posix" and sys.platform != "cygwin":
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if shared:
if addressFamily not in [socket.AF_INET, socket.AF_INET6]:
raise Exception("shared sockets are only supported for IPv4 and IPv6")
if _HAS_SHARED_LOADBALANCED_SOCKET:
if sys.platform.startswith('linux'):
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
elif sys.platform == 'win32':
# http://stackoverflow.com/questions/14388706/socket-options-so-reuseaddr-and-so-reuseport-how-do-they-differ-do-they-mean-t/14388707#14388707
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# s.setsockopt(socket.SOL_SOCKET, socket.SO_EXCLUSIVEADDRUSE, 1)
else:
raise Exception("logic error")
else:
raise Exception("shared sockets unsupported on this system")
return s
示例3: makesock
def makesock(sock, protocol, reactor=None):
if not reactor:
from twisted.internet import reactor
fdesc._setCloseOnExec(sock.fileno())
transport = Paired(sock, protocol, reactor)
protocol.makeConnection(transport)
return transport
示例4: create_stream_socket
def create_stream_socket(addressFamily, shared=False):
"""
Create a new socket for use with Twisted's IReactor.adoptStreamPort.
:param addressFamily: The socket address family.
:type addressFamily: One of socket.AF_INET, socket.AF_INET6, socket.AF_UNIX
:param shared: If `True`, request to create a shared, load-balanced socket.
When this feature is not available, throw an exception.
:type shared: bool
:returns obj -- A socket.
"""
s = socket.socket(addressFamily, socket.SOCK_STREAM)
s.setblocking(0)
fdesc._setCloseOnExec(s.fileno())
if platformType == "posix" and sys.platform != "cygwin":
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
if shared:
if addressFamily not in [socket.AF_INET, socket.AF_INET6]:
raise Exception("shared sockets are only supported for TCP")
if _HAS_SHARED_LOADBALANCED_SOCKET:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
else:
raise Exception("shared sockets unsupported on this system")
return s
示例5: createInternetSocket
def createInternetSocket(self):
"""(internal) Create a non-blocking socket using
self.addressFamily, self.socketType.
"""
s = socket.socket(self.addressFamily, self.socketType)
s.setblocking(0)
fdesc._setCloseOnExec(s.fileno())
return s
示例6: __init__
def __init__(self, reactor):
"""Initialize.
"""
self.reactor = reactor
self.i, self.o = os.pipe()
fdesc.setNonBlocking(self.i)
fdesc._setCloseOnExec(self.i)
fdesc.setNonBlocking(self.o)
fdesc._setCloseOnExec(self.o)
self.fileno = lambda: self.i
示例7: test_unsetCloseOnExec
def test_unsetCloseOnExec(self):
"""
A file descriptor passed to L{fdesc._unsetCloseOnExec} is inherited by
a new process image created with one of the exec family of functions.
"""
with open(self.mktemp(), 'wb') as fObj:
fdesc._setCloseOnExec(fObj.fileno())
fdesc._unsetCloseOnExec(fObj.fileno())
status = self._execWithFileDescriptor(fObj)
self.assertTrue(os.WIFEXITED(status))
self.assertEqual(os.WEXITSTATUS(status), 20)
示例8: __init__
def __init__(self, interface, super_socket=None, timeout=5):
abstract.FileDescriptor.__init__(self, reactor)
if interface == "auto":
interface = getDefaultIface()
if not super_socket:
super_socket = conf.L3socket(iface=interface, promisc=True, filter="")
# super_socket = conf.L2socket(iface=interface)
self.protocols = []
fdesc._setCloseOnExec(super_socket.ins.fileno())
self.super_socket = super_socket
示例9: __init__
def __init__(self, interface, super_socket=None, timeout=5):
abstract.FileDescriptor.__init__(self, reactor)
if interface == 'auto':
interface = getDefaultIface()
if not super_socket and sys.platform == 'darwin':
super_socket = conf.L3socket(iface=interface, promisc=True, filter='')
elif not super_socket:
super_socket = conf.L3socket(iface=interface)
self.protocols = []
fdesc._setCloseOnExec(super_socket.ins.fileno())
self.super_socket = super_socket
示例10: __init__
def __init__(self, reactor=None):
FileDescriptor.__init__(self, reactor=reactor)
# Smart way to allow parametrization of libc so I can override
# it and test for the system errors.
self._fd = self._inotify.init()
fdesc.setNonBlocking(self._fd)
fdesc._setCloseOnExec(self._fd)
# The next 2 lines are needed to have self.loseConnection()
# to call connectionLost() on us. Since we already created the
# fd that talks to inotify we want to be notified even if we
# haven't yet started reading.
self.connected = 1
self._writeDisconnected = True
self._buffer = ''
self._watchpoints = {}
self._watchpaths = {}
示例11: get_socket
def get_socket(self):
'''
Return listening socket.
'''
if self.SYSTEMD.LISTEN_PID in os.environ:
# looks like we've been started by systemd socket activation
if os.environ.get(self.SYSTEMD.LISTEN_FDS, None) == '1':
fd = self.SYSTEMD.LISTEN_FDS_START + 0
log_info(
'Using socket at FD %s activated by PID %s' % (
fd, os.environ[self.SYSTEMD.LISTEN_PID]
)
)
sckt = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
else:
# we expect exactly one socket to be initialized by Systemd
message = 'Environmental variable %s must point to exactly '\
'one file descriptor, got %s intead' % (
self.SYSTEMD.LISTEN_FDS,
os.environ.get(self.SYSTEMD.LISTEN_FDS, None),
)
log_err(message)
raise EnvironmentError(message)
else:
log_info('Binding to %s:%s' % (self.args.addr, self.args.port))
sckt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sckt.bind((self.args.addr, self.args.port))
# socket initilization for Twisted
sckt.setblocking(0)
fdesc._setCloseOnExec(sckt.fileno())
sckt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sckt.listen(50)
return sckt
示例12: doRead
def doRead(self):
"""Called when my socket is ready for reading.
This accepts a connection and calls self.protocol() to handle the
wire-level protocol.
"""
try:
if platformType == "posix":
numAccepts = self.numberAccepts
else:
# win32 event loop breaks if we do more than one accept()
# in an iteration of the event loop.
numAccepts = 1
for i in range(numAccepts):
# we need this so we can deal with a factory's buildProtocol
# calling our loseConnection
if self.disconnecting:
return
try:
skt, addr = self.socket.accept()
except socket.error as e:
if e.args[0] in (EWOULDBLOCK, EAGAIN):
self.numberAccepts = i
break
elif e.args[0] == EPERM:
# Netfilter on Linux may have rejected the
# connection, but we get told to try to accept()
# anyway.
continue
elif e.args[0] in (EMFILE, ENOBUFS, ENFILE, ENOMEM, ECONNABORTED):
# Linux gives EMFILE when a process is not allowed to
# allocate any more file descriptors. *BSD and Win32
# give (WSA)ENOBUFS. Linux can also give ENFILE if the
# system is out of inodes, or ENOMEM if there is
# insufficient memory to allocate a new dentry.
# ECONNABORTED is documented as possible on all
# relevant platforms (Linux, Windows, macOS, and the
# BSDs) but occurs only on the BSDs. It occurs when a
# client sends a FIN or RST after the server sends a
# SYN|ACK but before application code calls accept(2).
# On Linux, calling accept(2) on such a listener
# returns a connection that fails as though the it were
# terminated after being fully established. This
# appears to be an implementation choice (see
# inet_accept in inet/ipv4/af_inet.c). On macOS X,
# such a listener is not considered readable, so
# accept(2) will never be called. Calling accept(2) on
# such a listener, however, does not return at all.
log.msg("Could not accept new connection (%s)" % (
errorcode[e.args[0]],))
break
raise
fdesc._setCloseOnExec(skt.fileno())
protocol = self.factory.buildProtocol(self._buildAddr(addr))
if protocol is None:
skt.close()
continue
s = self.sessionno
self.sessionno = s+1
transport = self.transport(skt, protocol, addr, self, s, self.reactor)
protocol.makeConnection(transport)
else:
self.numberAccepts = self.numberAccepts+20
except:
# Note that in TLS mode, this will possibly catch SSL.Errors
# raised by self.socket.accept()
#
# There is no "except SSL.Error:" above because SSL may be
# None if there is no SSL support. In any case, all the
# "except SSL.Error:" suite would probably do is log.deferr()
# and return, so handling it here works just as well.
log.deferr()