本文整理匯總了Python中socket.SO_TYPE屬性的典型用法代碼示例。如果您正苦於以下問題:Python socket.SO_TYPE屬性的具體用法?Python socket.SO_TYPE怎麽用?Python socket.SO_TYPE使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在類socket
的用法示例。
在下文中一共展示了socket.SO_TYPE屬性的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: is_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SO_TYPE [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
示例2: _is_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SO_TYPE [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
示例3: test_create_sockets
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SO_TYPE [as 別名]
def test_create_sockets(self):
with create_sockets() as socks:
fams = collections.defaultdict(int)
types = collections.defaultdict(int)
for s in socks:
fams[s.family] += 1
# work around http://bugs.python.org/issue30204
types[s.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE)] += 1
self.assertGreaterEqual(fams[socket.AF_INET], 2)
if supports_ipv6():
self.assertGreaterEqual(fams[socket.AF_INET6], 2)
if POSIX and HAS_CONNECTIONS_UNIX:
self.assertGreaterEqual(fams[socket.AF_UNIX], 2)
self.assertGreaterEqual(types[socket.SOCK_STREAM], 2)
self.assertGreaterEqual(types[socket.SOCK_DGRAM], 2)
示例4: check_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SO_TYPE [as 別名]
def check_socket(self, sock, conn=None):
"""Given a socket, makes sure it matches the one obtained
via psutil. It assumes this process created one connection
only (the one supposed to be checked).
"""
if conn is None:
conn = self.get_conn_from_sock(sock)
check_connection_ntuple(conn)
# fd, family, type
if conn.fd != -1:
self.assertEqual(conn.fd, sock.fileno())
self.assertEqual(conn.family, sock.family)
# see: http://bugs.python.org/issue30204
self.assertEqual(
conn.type, sock.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE))
# local address
laddr = sock.getsockname()
if not laddr and PY3 and isinstance(laddr, bytes):
# See: http://bugs.python.org/issue30205
laddr = laddr.decode()
if sock.family == AF_INET6:
laddr = laddr[:2]
if sock.family == AF_UNIX and OPENBSD:
# No addresses are set for UNIX sockets on OpenBSD.
pass
else:
self.assertEqual(conn.laddr, laddr)
# XXX Solaris can't retrieve system-wide UNIX sockets
if sock.family == AF_UNIX and HAS_CONNECTIONS_UNIX:
cons = thisproc.connections(kind='all')
self.compare_procsys_connections(os.getpid(), cons)
return conn
示例5: check_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SO_TYPE [as 別名]
def check_socket(self, sock):
"""Given a socket, makes sure it matches the one obtained
via psutil. It assumes this process created one connection
only (the one supposed to be checked).
"""
conn = self.get_conn_from_sock(sock)
self.check_connection_ntuple(conn)
# fd, family, type
if conn.fd != -1:
self.assertEqual(conn.fd, sock.fileno())
self.assertEqual(conn.family, sock.family)
# see: http://bugs.python.org/issue30204
self.assertEqual(
conn.type, sock.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE))
# local address
laddr = sock.getsockname()
if not laddr and PY3 and isinstance(laddr, bytes):
# See: http://bugs.python.org/issue30205
laddr = laddr.decode()
if sock.family == AF_INET6:
laddr = laddr[:2]
if sock.family == AF_UNIX and OPENBSD:
# No addresses are set for UNIX sockets on OpenBSD.
pass
else:
self.assertEqual(conn.laddr, laddr)
# XXX Solaris can't retrieve system-wide UNIX sockets
if sock.family == AF_UNIX and HAS_CONNECTIONS_UNIX:
cons = thisproc.connections(kind='all')
self.compare_procsys_connections(os.getpid(), cons, kind='all')
return conn
示例6: is_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SO_TYPE [as 別名]
def is_socket(fd):
"""Determine if the file descriptor is a socket."""
file_socket = socket.fromfd(fd, socket.AF_INET, socket.SOCK_RAW)
try:
file_socket.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE)
except socket.error as ex:
return ex.args[0] != errno.ENOTSOCK
else:
return True
示例7: is_socket
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SO_TYPE [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:
file_socket.getsockopt(
socket.SOL_SOCKET, socket.SO_TYPE)
except socket.error as exc:
exc_errno = exc.args[0]
if exc_errno == errno.ENOTSOCK:
# Socket operation on non-socket
pass
else:
# Some other socket error
result = True
else:
# No error getting socket type
result = True
return result
示例8: testSO_TYPE
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SO_TYPE [as 別名]
def testSO_TYPE(self):
for socket_type in [socket.SOCK_STREAM, socket.SOCK_DGRAM]:
s = socket.socket(socket.AF_INET, socket_type)
self.failUnlessEqual(s.getsockopt(socket.SOL_SOCKET, socket.SO_TYPE), socket_type)
示例9: _create
# 需要導入模塊: import socket [as 別名]
# 或者: from socket import SO_TYPE [as 別名]
def _create(cls, sock, server_side=False, do_handshake_on_connect=True,
suppress_ragged_eofs=True, server_hostname=None,
context=None, session=None):
if sock.getsockopt(SOL_SOCKET, SO_TYPE) != SOCK_STREAM:
raise NotImplementedError("only stream sockets are supported")
if server_side:
if server_hostname:
raise ValueError("server_hostname can only be specified "
"in client mode")
if session is not None:
raise ValueError("session can only be specified in "
"client mode")
if context.check_hostname and not server_hostname:
raise ValueError("check_hostname requires server_hostname")
kwargs = dict(
family=sock.family, type=sock.type, proto=sock.proto,
fileno=sock.fileno()
)
self = cls.__new__(cls, **kwargs)
super(SSLSocket, self).__init__(**kwargs)
self.settimeout(sock.gettimeout())
sock.detach()
self._context = context
self._session = session
self._closed = False
self._sslobj = None
self.server_side = server_side
self.server_hostname = context._encode_hostname(server_hostname)
self.do_handshake_on_connect = do_handshake_on_connect
self.suppress_ragged_eofs = suppress_ragged_eofs
# See if we are connected
try:
self.getpeername()
except OSError as e:
if e.errno != errno.ENOTCONN:
raise
connected = False
else:
connected = True
self._connected = connected
if connected:
# create the SSL object
try:
self._sslobj = self._context._wrap_socket(
self, server_side, self.server_hostname,
owner=self, session=self._session,
)
if do_handshake_on_connect:
timeout = self.gettimeout()
if timeout == 0.0:
# non-blocking
raise ValueError("do_handshake_on_connect should not be specified for non-blocking sockets")
self.do_handshake()
except (OSError, ValueError):
self.close()
raise
return self