pysnmp-sky/pysnmp/carrier/asyncore/dgram/unix.py

62 lines
1.6 KiB
Python
Raw Normal View History

2015-11-20 21:57:28 +01:00
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2017, Ilya Etingof <etingof@gmail.com>
2015-11-20 21:57:28 +01:00
# License: http://pysnmp.sf.net/license.html
#
import os
import random
2016-04-02 23:43:14 +02:00
try:
from socket import AF_UNIX
except ImportError:
AF_UNIX = None
from pysnmp.carrier.base import AbstractTransportAddress
from pysnmp.carrier.asyncore.dgram.base import DgramSocketTransport
2005-06-14 12:01:40 +02:00
domainName = snmpLocalDomain = (1, 3, 6, 1, 2, 1, 100, 1, 13)
random.seed()
2016-04-02 23:43:14 +02:00
class UnixTransportAddress(str, AbstractTransportAddress):
pass
2016-04-02 23:43:14 +02:00
class UnixSocketTransport(DgramSocketTransport):
sockFamily = AF_UNIX
addressType = UnixTransportAddress
2016-04-02 23:43:14 +02:00
_iface = ''
def openClientMode(self, iface=None):
if iface is None:
# UNIX domain sockets must be explicitly bound
iface = ''
while len(iface) < 8:
iface += chr(random.randrange(65, 91))
iface += chr(random.randrange(97, 123))
iface = os.path.sep + 'tmp' + os.path.sep + 'pysnmp' + iface
2012-07-03 13:09:33 +02:00
if os.path.exists(iface):
os.remove(iface)
DgramSocketTransport.openClientMode(self, iface)
2016-04-02 23:43:14 +02:00
self._iface = iface
return self
def openServerMode(self, iface):
DgramSocketTransport.openServerMode(self, iface)
2016-04-02 23:43:14 +02:00
self._iface = iface
return self
def closeTransport(self):
DgramSocketTransport.closeTransport(self)
try:
2016-04-02 23:43:14 +02:00
os.remove(self._iface)
2014-06-17 17:19:53 +02:00
except OSError:
pass
2016-04-02 23:43:14 +02:00
UnixTransport = UnixSocketTransport
# Compatibility stub
UnixDgramSocketTransport = UnixSocketTransport