12

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?

trapicki
  • 1,881
  • 1
  • 18
  • 24

1 Answers1

15

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

trapicki
  • 1,881
  • 1
  • 18
  • 24