/usr/share/pyshared/pysnmp/v2/role.py is in python-pysnmp2 2.0.9-3build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | """
Single-session, blocking network I/O classes.
Written by Ilya Etingof <ilya@glas.net>, 1999-2002
"""
import socket
import select
# Import package components
import error
class Error(error.Generic):
"""Base class for role module
"""
pass
class BadArgument(Error):
"""Bad argument given
"""
pass
class NetworkError(Error):
"""Network transport error
"""
pass
class NoResponse(NetworkError):
"""No response arrived before timeout
"""
pass
class NoRequest(NetworkError):
"""No request arrived before timeout
"""
pass
class manager:
"""Network client: send data item to server and receive a response
"""
def __init__(self, agent=None, iface=('0.0.0.0', 0)):
# Initialize defaults
self.agent = agent
self.iface = iface
self.socket = None
self.timeout = 1.0
self.retries = 3
def __del__(self):
"""Close socket on object termination
"""
try:
self.close()
except error.TransportError:
pass
def get_socket(self):
"""
get_socket() -> socket
Return socket object previously created with open() method.
"""
return self.socket
def open(self):
"""
open()
Initialize transport layer (UDP socket) to be used
for further communication with server.
"""
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except socket.error, why:
raise NetworkError('socket() error: ' + str(why))
# See if we need to bind to specific interface on client machine
try:
self.socket.bind(self.iface)
except socket.error, why:
raise NetworkError('bind() error: %s: %s' % (self.iface, why))
# Connect to default destination if given
if self.agent is not None:
try:
self.socket.connect(self.agent)
except socket.error, why:
raise NetworkError('connect() error: %s: %s' % (self.agent, why))
return self.socket
def send(self, request, dst=None):
"""
send(req[, dst])
Send "req" message (string) to server by address specified on
object creation or by "dst" address given in socket module
notation.
"""
# Make sure the connection is established, open it otherwise
if not self.socket:
self.open()
try:
if dst:
self.socket.sendto(request, dst)
else:
self.socket.send(request)
except socket.error, why:
raise NetworkError('send() error: ' + str(why))
def read(self):
"""
read() -> (message, src)
Read data from the socket (assuming there's some data ready
for reading), return a tuple of response message (as string)
and source address 'src' (in socket module notation).
"""
# Make sure the connection exists
if not self.socket:
raise NetworkError('Socket not initialized')
try:
(message, src) = self.socket.recvfrom(65536)
except socket.error, why:
raise NetworkError('recv() error: ' + str(why))
return (message, src)
def receive(self):
"""
receive() -> (message, src)
Wait for incoming data from network or timeout (and return
a tuple of None's).
Return a tuple of received data item (as string) and source address
'src' (in socket module notation).
"""
# Make sure the connection exists
if not self.socket:
raise NetworkError('Socket not initialized')
# Initialize sockets map
r, w, x = [self.socket], [], []
# Wait for response
r, w, x = select.select(r, w, x, self.timeout)
# Timeout occurred?
if r:
return self.read()
# Return nothing on timeout
return(None, None)
def send_and_receive(self, message, dst=None):
"""
send_and_receive(data[, dst]) -> (data, src)
Send data item to remote entity by address specified on object
creation or 'dst' address and receive a data item in response
or timeout (and raise NoResponse exception).
Return a tuple of data item (as string) and source address
'src' (in socket module notation).
"""
# Initialize retries counter
retries = 0
# Send request till response or retry counter hits the limit
while retries < self.retries:
# Send a request
self.send(message, dst)
# Wait for response
(response, src) = self.receive()
# See if it's succeeded
if response:
return(response, src)
# Otherwise, try it again
retries = retries + 1
# No answer, raise an exception
raise NoResponse('No response arrived before timeout')
def close(self):
"""
close()
Terminate communication with remote server.
"""
# See if it's opened
if self.socket:
try:
self.socket.close()
except socket.error, why:
raise NetworkError('close() error: ' + str(why))
# Initialize it to None to indicate it's closed
self.socket = None
class agent:
"""Network client: receive requests, send back responses
"""
def __init__(self, ifaces=[('0.0.0.0', 161)]):
# Block on select() waiting for request by default
self.timeout = None
# Initialize defaults
self.ifaces = ifaces
self.socket = None
def __del__(self):
"""Close socket on object termination
"""
try:
self.close()
except error.TransportError:
pass
def get_socket(self):
"""
get_socket() -> socket
Return socket object previously created with open() method.
"""
return self.socket
def open(self):
"""
open()
Initialize transport internals to be used for further
communication with client.
"""
try:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
except socket.error, why:
raise NetworkError('socket() error: ' + str(why))
# Bind to specific interfaces at server machine
for iface in self.ifaces:
try:
self.socket.bind(iface)
except socket.error, why:
raise NetworkError('bind() error: %s: %s' % (iface, why))
return self.socket
def send(self, message, dst):
"""
send(rsp, dst)
Send response message (given as string) to client process
by 'dst' address given in socket module notation.
"""
# Make sure the connection is established, open it otherwise
if not self.socket:
raise NetworkError('Socket not initialized')
try:
self.socket.sendto(message, dst)
except socket.error, why:
raise NetworkError('send() error: ' + str(why))
def read(self):
"""
read() -> (req, src)
Read data from the socket (assuming there's some data ready
for reading), return a tuple of request (as string) and
source address 'src' (in socket module notation).
"""
# Make sure the connection exists
if not self.socket:
raise NetworkError('Socket not initialized')
try:
(message, peer) = self.socket.recvfrom(65536)
except socket.error, why:
raise NetworkError('recvfrom() error: ' + str(why))
return (message, peer)
def receive(self):
"""
receive() -> (req, src)
Wait for and receive request message from remote process
or timeout.
Return a tuple of request message (as string) and source address
'src' (in socket module notaton).
"""
# Attempt to initialize transport stuff
if not self.socket:
self.open()
# Initialize sockets map
r, w, x = [ self.socket ], [], []
# Wait for response
r, w, x = select.select(r, w, x, self.timeout)
# Timeout occurred?
if r:
return self.read()
raise NoRequest('No request arrived before timeout')
def receive_and_send(self, callback):
"""
receive_and_send(callback)
Wait for request from a client process or timeout (and raise
NoRequest exception), pass request to the callback function
to build a response, send response back to client process.
"""
if not callable (callback):
raise BadArgument('Bad callback function')
while 1:
# Wait for request to come
(request, src) = self.receive()
if not request:
raise NoRequest('No request arrived before timeout')
# Invoke callback function
(response, dst) = callback(self, (request, src))
# Send a response if any
if (response):
# Reply back by either source address or alternative
# destination whenever given
if dst:
self.send(response, dst)
else:
self.send(response, src)
def close(self):
"""
close()
Close UDP socket used for communication with client.
"""
# See if it's opened
if self.socket:
try:
self.socket.close()
except socket.error, why:
raise NetworkError('close() error: ' + str(why))
# Initialize it to None to indicate it's closed
self.socket = None
|