Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if path is a socket in Python 2.7

What would be the best way in Python 2.7 to find out if a path is a socket?

os.path has is... functions for directories, normal files and links. The stat module offers some S_IS... functions like S_ISSOCK(mode) which I used as

import os, stat

path = "/path/to/socket"
mode = os.stat(path).st_mode
isSocket = stat.S_ISSOCK(mode)

print "%s is socket: %s" % (path, isSocket)

Is this the prefered way?

like image 226
trapicki Avatar asked Jul 26 '13 09:07

trapicki


People also ask

How do I check socket status in Python?

How do you check socket is connected or not? If you need to determine the current state of the connection, make a nonblocking, zero-byte Send call. If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected.

How do you check if a file exists in a path?

os.path. exists() to check if a file or directory exists using Python. We further use this method to check if a particular file path refers to an already open descriptor or not.

What is Setsockopt Python?

The setsockopt() function provides an application program with the means to control socket behavior. An application program can use setsockopt() to allocate buffer space, control timeouts, or permit socket data broadcasts. The <sys/socket. h> header defines the socket-level options available to setsockopt().


1 Answers

Well, this is straight forward and works, so I take this as the canonical way.

like image 101
trapicki Avatar answered Oct 14 '22 12:10

trapicki