本文整理汇总了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:
开发者ID:antong,项目名称:twisted,代码行数:60,代码来源:tcp.py
示例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
开发者ID:RonaldChristopher,项目名称:crossbar,代码行数:35,代码来源:sharedport.py
示例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
开发者ID:pombreda,项目名称:rmake3,代码行数:7,代码来源:socketpair.py
示例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
开发者ID:GoodgameStudios,项目名称:crossbar,代码行数:28,代码来源:sharedport.py
示例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
开发者ID:dansgithubuser,项目名称:constructicon,代码行数:8,代码来源:tcp.py
示例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
开发者ID:hensing,项目名称:twisted,代码行数:10,代码来源:posixbase.py
示例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)
开发者ID:AmirKhooj,项目名称:VTK,代码行数:11,代码来源:test_fdesc.py
示例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
开发者ID:samsmith,项目名称:ooni-probe,代码行数:12,代码来源:txscapy.py
示例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
开发者ID:TheTorProject,项目名称:ooni-probe,代码行数:13,代码来源:txscapy.py
示例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 = {}
开发者ID:0004c,项目名称:VTK,代码行数:20,代码来源:inotify.py
示例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
开发者ID:nott,项目名称:httip,代码行数:38,代码来源:httip.py
示例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()
开发者ID:dansgithubuser,项目名称:constructicon,代码行数:73,代码来源:tcp.py
注:本文中的twisted.internet.fdesc._setCloseOnExec函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论