本文整理汇总了Python中stat.S_ISSOCK属性的典型用法代码示例。如果您正苦于以下问题:Python stat.S_ISSOCK属性的具体用法?Python stat.S_ISSOCK怎么用?Python stat.S_ISSOCK使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类stat
的用法示例。
在下文中一共展示了stat.S_ISSOCK属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: mode_filetype
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def mode_filetype(mode):
mode = stat.S_IFMT(mode)
dic = {
stat.S_ISFIFO: "fifo file",
stat.S_ISCHR: "character device",
stat.S_ISDIR: "directory",
stat.S_ISBLK: "block device",
stat.S_ISREG: "regular file",
stat.S_ISLNK: "symbolic link",
stat.S_ISSOCK: "socket",
stat.S_ISDOOR: "door",
}
for test_func, name in dic.items():
if test_func(mode):
return name
return "???"
示例2: test_unix_socketpair
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def test_unix_socketpair(self):
p = psutil.Process()
num_fds = p.num_fds()
assert not p.connections(kind='unix')
with unix_socket_path() as name:
server, client = unix_socketpair(name)
try:
assert os.path.exists(name)
assert stat.S_ISSOCK(os.stat(name).st_mode)
self.assertEqual(p.num_fds() - num_fds, 2)
self.assertEqual(len(p.connections(kind='unix')), 2)
self.assertEqual(server.getsockname(), name)
self.assertEqual(client.getpeername(), name)
finally:
client.close()
server.close()
示例3: test_unix_socketpair
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def test_unix_socketpair(self):
p = psutil.Process()
num_fds = p.num_fds()
assert not p.connections(kind='unix')
name = self.get_testfn()
server, client = unix_socketpair(name)
try:
assert os.path.exists(name)
assert stat.S_ISSOCK(os.stat(name).st_mode)
self.assertEqual(p.num_fds() - num_fds, 2)
self.assertEqual(len(p.connections(kind='unix')), 2)
self.assertEqual(server.getsockname(), name)
self.assertEqual(client.getpeername(), name)
finally:
client.close()
server.close()
示例4: test_pathServer
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def test_pathServer(self):
"""
The I{--path} option to L{makeService} causes it to return a service
which will listen on the server address given by the I{--port} option.
"""
path = FilePath(self.mktemp())
path.makedirs()
port = self.mktemp()
options = Options()
options.parseOptions(['--port', 'unix:' + port, '--path', path.path])
service = makeService(options)
service.startService()
self.addCleanup(service.stopService)
self.assertIsInstance(service.services[0].factory.resource, File)
self.assertEqual(service.services[0].factory.resource.path, path.path)
self.assertTrue(os.path.exists(port))
self.assertTrue(stat.S_ISSOCK(os.stat(port).st_mode))
示例5: convert
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def convert(self, value, param, ctx):
src_type = False
try:
mode = os.stat(value).st_mode
except OSError:
try:
src_name = Endpoint().convert(value, param, ctx)
except ValueError:
pass
else:
src_type = 'tcp'
else:
src_name = value
if S_ISSOCK(mode):
src_type = 'sock'
elif S_ISREG(mode):
src_type = 'dump'
if not src_type:
raise ValueError('Dump file or socket address required.')
return (src_type, src_name)
示例6: __init__
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def __init__(self, loop, pipe, protocol, waiter=None, extra=None):
super().__init__(extra)
self._extra['pipe'] = pipe
self._loop = loop
self._pipe = pipe
self._fileno = pipe.fileno()
mode = os.fstat(self._fileno).st_mode
if not (stat.S_ISFIFO(mode) or
stat.S_ISSOCK(mode) or
stat.S_ISCHR(mode)):
raise ValueError("Pipe transport is for pipes/sockets only.")
_set_nonblocking(self._fileno)
self._protocol = protocol
self._closing = False
self._loop.call_soon(self._protocol.connection_made, self)
# only start reading when connection_made() has been called
self._loop.call_soon(self._loop.add_reader,
self._fileno, self._read_ready)
if waiter is not None:
# only wake up the waiter when connection_made() has been called
self._loop.call_soon(futures._set_result_unless_cancelled,
waiter, None)
示例7: is_special_file
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def is_special_file(path):
"""
This function checks to see if a special file. It checks if the
file is a character special device, block special device, FIFO, or
socket.
"""
mode = os.stat(path).st_mode
# Character special device.
if stat.S_ISCHR(mode):
return True
# Block special device
if stat.S_ISBLK(mode):
return True
# FIFO.
if stat.S_ISFIFO(mode):
return True
# Socket.
if stat.S_ISSOCK(mode):
return True
return False
示例8: check_socket
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def check_socket(fn):
dn = os.path.dirname(fn)
try:
os.mkdir(dn, 0o750)
except OSError:
pass
try:
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
try:
s.connect(fn)
finally:
s.close()
except socket.error:
try:
assert S_ISSOCK(os.lstat(fn).st_mode), fn + " exists as non-socket"
os.unlink(fn)
except OSError:
pass
return
raise Exception("Socket %s already listening" % (fn,))
示例9: __get_socket_file
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def __get_socket_file(self):
"""Gets the Docker API socket file and validates that it is a UNIX socket
"""
# make sure the API socket exists and is a valid socket
api_socket = self._config.get("api_socket")
try:
st = os.stat(api_socket)
if not stat.S_ISSOCK(st.st_mode):
raise Exception()
except Exception:
raise Exception(
"The file '%s' specified by the 'api_socket' configuration option does not exist or is not a socket.\n\tPlease make sure you have mapped the docker socket from the host to this container using the -v parameter.\n\tNote: Due to problems Docker has mapping symbolic links, you should specify the final file and not a path that contains a symbolic link, e.g. map /run/docker.sock rather than /var/run/docker.sock as on many unices /var/run is a symbolic link to the /run directory."
% api_socket
)
return api_socket
示例10: test_groupOwnedUNIXSocket
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def test_groupOwnedUNIXSocket(self):
"""
When a L{GroupOwnedUNIXServer} is started, it will change the group of
its socket.
"""
alternateGroup = determineAppropriateGroupID()
if alternateGroup is None:
self.skipTest ((
"This test requires that the user running it is a member of at"
" least two unix groups."
))
socketName = self.mktemp()
gous = GroupOwnedUNIXServer(alternateGroup, socketName, ServerFactory(), mode=0660)
gous.privilegedStartService()
self.addCleanup(gous.stopService)
filestat = os.stat(socketName)
self.assertTrue(stat.S_ISSOCK(filestat.st_mode))
self.assertEquals(filestat.st_gid, alternateGroup)
self.assertEquals(filestat.st_uid, os.getuid())
# Tests for the various makeService_ flavors:
示例11: __init__
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def __init__(self, loop, pipe, protocol, waiter=None, extra=None):
super().__init__(extra)
self._extra['pipe'] = pipe
self._loop = loop
self._pipe = pipe
self._fileno = pipe.fileno()
mode = os.fstat(self._fileno).st_mode
if not (stat.S_ISFIFO(mode) or
stat.S_ISSOCK(mode) or
stat.S_ISCHR(mode)):
raise ValueError("Pipe transport is for pipes/sockets only.")
_set_nonblocking(self._fileno)
self._protocol = protocol
self._closing = False
self._loop.call_soon(self._protocol.connection_made, self)
# only start reading when connection_made() has been called
self._loop.call_soon(self._loop.add_reader,
self._fileno, self._read_ready)
if waiter is not None:
# only wake up the waiter when connection_made() has been called
self._loop.call_soon(waiter._set_result_unless_cancelled, None)
示例12: bind_unix_socket
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def bind_unix_socket(path: str, *, mode=0o666, backlog=100) -> socket.socket:
"""Create unix socket.
:param path: filesystem path
:param backlog: Maximum number of connections to queue
:return: socket.socket object
"""
"""Open or atomically replace existing socket with zero downtime."""
# Sanitise and pre-verify socket path
path = os.path.abspath(path)
folder = os.path.dirname(path)
if not os.path.isdir(folder):
raise FileNotFoundError(f"Socket folder does not exist: {folder}")
try:
if not stat.S_ISSOCK(os.stat(path, follow_symlinks=False).st_mode):
raise FileExistsError(f"Existing file is not a socket: {path}")
except FileNotFoundError:
pass
# Create new socket with a random temporary name
tmp_path = f"{path}.{secrets.token_urlsafe()}"
sock = socket.socket(socket.AF_UNIX)
try:
# Critical section begins (filename races)
sock.bind(tmp_path)
try:
os.chmod(tmp_path, mode)
# Start listening before rename to avoid connection failures
sock.listen(backlog)
os.rename(tmp_path, path)
except: # noqa: E722
try:
os.unlink(tmp_path)
finally:
raise
except: # noqa: E722
try:
sock.close()
finally:
raise
return sock
示例13: remove_unix_socket
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def remove_unix_socket(path: str) -> None:
"""Remove dead unix socket during server exit."""
if not path:
return
try:
if stat.S_ISSOCK(os.stat(path, follow_symlinks=False).st_mode):
# Is it actually dead (doesn't belong to a new server instance)?
with socket.socket(socket.AF_UNIX) as testsock:
try:
testsock.connect(path)
except ConnectionRefusedError:
os.unlink(path)
except FileNotFoundError:
pass
示例14: bind_unix_socket
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def bind_unix_socket(file, mode=0o600, backlog=_DEFAULT_BACKLOG):
"""Creates a listening unix socket.
If a socket with the given name already exists, it will be deleted.
If any other file with that name exists, an exception will be
raised.
Returns a socket object (not a list of socket objects like
`bind_sockets`)
"""
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
set_close_exec(sock.fileno())
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.setblocking(0)
try:
st = os.stat(file)
except OSError as err:
if errno_from_exception(err) != errno.ENOENT:
raise
else:
if stat.S_ISSOCK(st.st_mode):
os.remove(file)
else:
raise ValueError("File %s exists and is not a socket", file)
sock.bind(file)
os.chmod(file, mode)
sock.listen(backlog)
return sock
示例15: _check_sock_file
# 需要导入模块: import stat [as 别名]
# 或者: from stat import S_ISSOCK [as 别名]
def _check_sock_file(vhostuser_socket):
mode = os.stat(vhostuser_socket).st_mode
return stat.S_ISSOCK(mode)