本文整理汇总了Python中socket.fromfd方法的典型用法代码示例。如果您正苦于以下问题:Python socket.fromfd方法的具体用法?Python socket.fromfd怎么用?Python socket.fromfd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket
的用法示例。
在下文中一共展示了socket.fromfd方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _fromListeningDescriptor
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def _fromListeningDescriptor(cls, reactor, fd, addressFamily, factory):
"""
Create a new L{Port} based on an existing listening I{SOCK_STREAM}
socket.
Arguments are the same as to L{Port.__init__}, except where noted.
@param fd: An integer file descriptor associated with a listening
socket. The socket must be in non-blocking mode. Any additional
attributes desired, such as I{FD_CLOEXEC}, must also be set already.
@param addressFamily: The address family (sometimes called I{domain}) of
the existing socket. For example, L{socket.AF_INET}.
@return: A new instance of C{cls} wrapping the socket given by C{fd}.
"""
port = socket.fromfd(fd, addressFamily, cls.socketType)
interface = port.getsockname()[0]
self = cls(None, factory, None, interface, reactor)
self._preexistingSocket = port
return self
示例2: recvfd
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def recvfd(socketfd):
"""
Receive a file descriptor from a L{sendmsg} message on the given C{AF_UNIX}
socket.
@param socketfd: An C{AF_UNIX} socket, attached to another process waiting
to send sockets via the ancillary data mechanism in L{send1msg}.
@param fd: C{int}
@return: a 2-tuple of (new file descriptor, description).
@rtype: 2-tuple of (C{int}, C{bytes})
"""
ourSocket = socket.fromfd(socketfd, socket.AF_UNIX, socket.SOCK_STREAM)
data, ancillary, flags = recvmsg(ourSocket)
[(cmsgLevel, cmsgType, packedFD)] = ancillary
# cmsgLevel and cmsgType really need to be SOL_SOCKET / SCM_RIGHTS, but
# since those are the *only* standard values, there's not much point in
# checking.
[unpackedFD] = unpack("i", packedFD)
return (unpackedFD, data)
示例3: killSocket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def killSocket(self):
if not self.currentResponse:
return
try:
socket.fromfd(self.currentResponse.raw.fileno(), socket.AF_INET, socket.SOCK_STREAM).shutdown(socket.SHUT_RDWR)
return
except AttributeError:
pass
except Exception as e:
util.ERROR(err=e)
try:
self.currentResponse.raw._fp.fp._sock.shutdown(socket.SHUT_RDWR)
except AttributeError:
pass
except Exception as e:
util.ERROR(err=e)
示例4: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def __init__(self,
socketFd,
sharedStateAddress):
self.socketFd = socketFd
self.callbackSchedulerFactory = CallbackScheduler.createSimpleCallbackSchedulerFactory()
self.scheduler = self.callbackSchedulerFactory.createScheduler(
"BackendGatewayRequestHandler",
1
)
sharedStateHost, sharedStatePort = sharedStateAddress.split(':')
sharedStateViewFactory = ViewFactory.ViewFactory.TcpViewFactory(
self.callbackSchedulerFactory.createScheduler('SharedState', 1),
sharedStateHost,
int(sharedStatePort)
)
self.subscribableHandler = ConnectionHandler.ConnectionHandler(
self.scheduler,
sharedStateViewFactory,
lambda: TcpChannelFactory.TcpStringChannelFactory(self.scheduler)
)
self.sock = socket.fromfd(socketFd, socket.AF_INET, socket.SOCK_STREAM)
示例5: get_high_socket_fd
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def get_high_socket_fd(self):
if WIN32:
# The child process will not have any socket handles, so
# calling socket.fromfd() should produce WSAENOTSOCK even
# if there is a handle of the same number.
return socket.socket().detach()
else:
# We want to produce a socket with an fd high enough that a
# freshly created child process will not have any fds as high.
fd = socket.socket().detach()
to_close = []
while fd < 50:
to_close.append(fd)
fd = os.dup(fd)
for x in to_close:
os.close(x)
return fd
示例6: _fromListeningDescriptor
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def _fromListeningDescriptor(cls, reactor, fd, addressFamily, factory):
"""
Create a new L{Port} based on an existing listening I{SOCK_STREAM}
socket.
Arguments are the same as to L{Port.__init__}, except where noted.
@param fd: An integer file descriptor associated with a listening
socket. The socket must be in non-blocking mode. Any additional
attributes desired, such as I{FD_CLOEXEC}, must also be set already.
@param addressFamily: The address family (sometimes called I{domain}) of
the existing socket. For example, L{socket.AF_INET}.
@return: A new instance of C{cls} wrapping the socket given by C{fd}.
"""
port = socket.fromfd(fd, addressFamily, cls.socketType)
interface = _getsockname(port)[0]
self = cls(None, factory, None, interface, reactor)
self._preexistingSocket = port
return self
示例7: _fromListeningDescriptor
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def _fromListeningDescriptor(cls, reactor, fd, factory):
"""
Create a new L{Port} based on an existing listening I{SOCK_STREAM}
socket.
Arguments are the same as to L{Port.__init__}, except where noted.
@param fd: An integer file descriptor associated with a listening
socket. The socket must be in non-blocking mode. Any additional
attributes desired, such as I{FD_CLOEXEC}, must also be set already.
@return: A new instance of C{cls} wrapping the socket given by C{fd}.
"""
port = socket.fromfd(fd, cls.addressFamily, cls.socketType)
self = cls(port.getsockname(), factory, reactor=reactor)
self._preexistingSocket = port
return self
示例8: is_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def is_socket(fd):
""" Determine if the file descriptor is a socket.
Return ``False`` if querying the socket type of `fd` raises an
error; otherwise return ``True``.
"""
result = False
file_socket = socket.fromfd(fd, socket.AF_INET, socket.SOCK_RAW)
try:
socket_type = file_socket.getsockopt(
socket.SOL_SOCKET, socket.SO_TYPE)
except socket.error, exc:
exc_errno = exc.args[0]
if exc_errno == errno.ENOTSOCK:
# Socket operation on non-socket
pass
else:
# Some other socket error
result = True
示例9: _is_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def _is_socket(cls, stream):
"""Check if the given stream is a socket."""
try:
fd = stream.fileno()
except ValueError:
# If it has no file descriptor, it's not a socket
return False
sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_RAW)
try:
# This will raise a socket.error if it's not a socket
sock.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE)
except socket.error as ex:
if ex.args[0] != errno.ENOTSOCK:
# It must be a socket
return True
else:
# If an exception wasn't raised, it's a socket
return True
示例10: _get_systemd_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def _get_systemd_socket(self, address):
fds = sd.listen_fds()
if not fds:
return address
elif len(fds) > 1:
raise ValueError('Too many listening sockets', fds)
if isinstance(address, tuple):
port = address[1]
# systemd uses IPv6
if not sd.is_socket_inet(fds[0], family=socket.AF_INET6,
type=socket.SOCK_STREAM,
listening=True, port=port):
raise ValueError(
"FD {} is not TCP IPv6 socket on port {}".format(
fds[0], port
)
)
logger.info('Using systemd socket activation on port %i', port)
sock = socket.fromfd(fds[0], socket.AF_INET6, socket.SOCK_STREAM)
else:
if not sd.is_socket_unix(fds[0], socket.SOCK_STREAM,
listening=True, path=address):
raise ValueError(
"FD {} is not Unix stream socket on path {}".format(
fds[0], address
)
)
logger.info('Using systemd socket activation on path %s', address)
sock = socket.fromfd(fds[0], socket.AF_UNIX, socket.SOCK_STREAM)
if sys.version_info[0] < 3:
# Python 2.7's socket.fromfd() returns _socket.socket
sock = socket.socket(_sock=sock)
return sock
示例11: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def __init__(self, host, port, app, handler=None,
passthrough_errors=False, ssl_context=None, fd=None):
if handler is None:
handler = WSGIRequestHandler
self.address_family = select_ip_version(host, port)
if fd is not None:
real_sock = socket.fromfd(fd, self.address_family,
socket.SOCK_STREAM)
port = 0
HTTPServer.__init__(self, (host, int(port)), handler)
self.app = app
self.passthrough_errors = passthrough_errors
self.shutdown_signal = False
self.host = host
self.port = port
# Patch in the original socket.
if fd is not None:
self.socket.close()
self.socket = real_sock
self.server_address = self.socket.getsockname()
if ssl_context is not None:
if isinstance(ssl_context, tuple):
ssl_context = load_ssl_context(*ssl_context)
if ssl_context == 'adhoc':
ssl_context = generate_adhoc_ssl_context()
# If we are on Python 2 the return value from socket.fromfd
# is an internal socket object but what we need for ssl wrap
# is the wrapper around it :(
sock = self.socket
if PY2 and not isinstance(sock, socket.socket):
sock = socket.socket(sock.family, sock.type, sock.proto, sock)
self.socket = ssl_context.wrap_socket(sock, server_side=True)
self.ssl_context = ssl_context
else:
self.ssl_context = None
示例12: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def __init__(self, address, conf, log, fd=None):
self.log = log
self.conf = conf
self.cfg_addr = address
if fd is None:
sock = socket.socket(self.FAMILY, socket.SOCK_STREAM)
else:
sock = socket.fromfd(fd, self.FAMILY, socket.SOCK_STREAM)
self.sock = self.set_options(sock, bound=(fd is not None))
示例13: is_socket
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def is_socket (fd):
try:
s = socket.fromfd(fd, socket.AF_INET, socket.SOCK_RAW)
except ValueError,e:
# The file descriptor is closed
return False
示例14: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def __init__(self, host, port, app, handler=None,
passthrough_errors=False, ssl_context=None, fd=None):
if handler is None:
handler = WSGIRequestHandler
self.address_family = select_ip_version(host, port)
if fd is not None:
real_sock = socket.fromfd(fd, self.address_family,
socket.SOCK_STREAM)
port = 0
HTTPServer.__init__(self, get_sockaddr(host, int(port),
self.address_family), handler)
self.app = app
self.passthrough_errors = passthrough_errors
self.shutdown_signal = False
self.host = host
self.port = self.socket.getsockname()[1]
# Patch in the original socket.
if fd is not None:
self.socket.close()
self.socket = real_sock
self.server_address = self.socket.getsockname()
if ssl_context is not None:
if isinstance(ssl_context, tuple):
ssl_context = load_ssl_context(*ssl_context)
if ssl_context == 'adhoc':
ssl_context = generate_adhoc_ssl_context()
# If we are on Python 2 the return value from socket.fromfd
# is an internal socket object but what we need for ssl wrap
# is the wrapper around it :(
sock = self.socket
if PY2 and not isinstance(sock, socket.socket):
sock = socket.socket(sock.family, sock.type, sock.proto, sock)
self.socket = ssl_context.wrap_socket(sock, server_side=True)
self.ssl_context = ssl_context
else:
self.ssl_context = None
示例15: testFromFd
# 需要导入模块: import socket [as 别名]
# 或者: from socket import fromfd [as 别名]
def testFromFd(self):
# Testing fromfd()
fd = self.cli_conn.fileno()
sock = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM)
self.addCleanup(sock.close)
msg = sock.recv(1024)
self.assertEqual(msg, MSG)