| 1 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 2 |
# you may not use this file except in compliance with the License. |
| 3 |
# You may obtain a copy of the License at |
| 4 |
# |
| 5 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 |
# |
| 7 |
# Unless required by applicable law or agreed to in writing, software |
| 8 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 9 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 |
# See the License for the specific language governing permissions and |
| 11 |
# limitations under the License. |
| 12 |
|
| 13 |
import logging |
| 14 |
import socket |
| 15 |
import threading |
| 16 |
from io import BytesIO as StringIO |
| 17 |
|
| 18 |
from socket import AF_UNIX, SOCK_STREAM |
| 19 |
|
| 20 |
from ncclient.capabilities import Capabilities |
| 21 |
from ncclient.logging_ import SessionLoggerAdapter |
| 22 |
from ncclient.transport.errors import UnixSocketError |
| 23 |
from ncclient.transport.session import Session |
| 24 |
from ncclient.transport.parser import DefaultXMLParser |
| 25 |
|
| 26 |
logger = logging.getLogger("ncclient.transport.unix") |
| 27 |
|
| 28 |
DEFAULT_TIMEOUT = 120 |
| 29 |
|
| 30 |
BUF_SIZE = 4096 |
| 31 |
|
| 32 |
class UnixSocketSession(Session): |
| 33 |
|
| 34 |
"Implements a NETCONF Session over Unix Socket on local machine." |
| 35 |
|
| 36 |
def __init__(self, device_handler): |
| 37 |
capabilities = Capabilities(device_handler.get_capabilities()) |
| 38 |
Session.__init__(self, capabilities) |
| 39 |
self._connected = False |
| 40 |
self._socket = None |
| 41 |
self._buffer = StringIO() |
| 42 |
self._device_handler = device_handler |
| 43 |
self._message_list = [] |
| 44 |
self._closing = threading.Event() |
| 45 |
self.parser = DefaultXMLParser(self) |
| 46 |
self.logger = SessionLoggerAdapter(logger, {'session': self}) |
| 47 |
|
| 48 |
def _dispatch_message(self, raw): |
| 49 |
self.logger.info("Received message from host") |
| 50 |
self.logger.debug("Received:\n%s", raw) |
| 51 |
return super(UnixSocketSession, self)._dispatch_message(raw) |
| 52 |
|
| 53 |
def close(self): |
| 54 |
self._closing.set() |
| 55 |
self._socket.close() |
| 56 |
self._connected = False |
| 57 |
|
| 58 |
def connect(self, path=None, timeout=DEFAULT_TIMEOUT): |
| 59 |
sock = socket.socket(AF_UNIX, SOCK_STREAM) |
| 60 |
sock.settimeout(timeout) |
| 61 |
|
| 62 |
try: |
| 63 |
sock.connect(path) |
| 64 |
except Exception: |
| 65 |
raise UnixSocketError("Could not connect to %s" % path) |
| 66 |
|
| 67 |
self._socket = sock |
| 68 |
self._connected = True |
| 69 |
self._post_connect() |
| 70 |
|
| 71 |
def _transport_read(self): |
| 72 |
return self._socket.recv(BUF_SIZE) |
| 73 |
|
| 74 |
def _transport_write(self, data): |
| 75 |
return self._socket.send(data) |
| 76 |
|
| 77 |
def _transport_register(self, selector, event): |
| 78 |
selector.register(self._socket, event) |
| 79 |
|
| 80 |
def _send_ready(self): |
| 81 |
# In contrast to Paramiko's `Channel`, pure python sockets do not |
| 82 |
# expose `send_ready()` function. |
| 83 |
return True |