Skip to content

Commit

Permalink
Removed reference to python2 (#262)
Browse files Browse the repository at this point in the history
* Removed reference to python2

* Removed support for lower versions of python

* Updated EOS version to the minimum supported

* Addressed formatting and error handling comments

* Refactored extra python2 check
  • Loading branch information
chieto-arista authored May 26, 2023
1 parent 7282e16 commit bf9976f
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 63 deletions.
4 changes: 2 additions & 2 deletions docs/requirements.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
Requirements
############

* Arista EOS 4.12 or later
* Arista EOS 4.22 or later
* Arista eAPI enabled for at least one transport (see Official EOS Config Guide
at arista.com for details)
* Python 2.7 or 3.4+ (Python 3 support is work in progress)
* Python 3.7+
* Pyeapi requires the netaddr Python module

.. Note:: netaddr gets installed automatically if you use pip to install pyeapi
12 changes: 3 additions & 9 deletions pyeapi/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,9 @@

from functools import lru_cache

try:
# Try Python 3.x import first
# Note: SafeConfigParser is deprecated and replaced by ConfigParser
from configparser import ConfigParser as SafeConfigParser
from configparser import Error as SafeConfigParserError
except ImportError:
# Use Python 2.7 import as a fallback
from ConfigParser import SafeConfigParser
from ConfigParser import Error as SafeConfigParserError
# Note: SafeConfigParser is deprecated and replaced by ConfigParser
from configparser import ConfigParser as SafeConfigParser
from configparser import Error as SafeConfigParserError

from pyeapi.utils import load_module, make_iterable, debug, CliVariants

Expand Down
57 changes: 17 additions & 40 deletions pyeapi/eapilib.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
for sending and receiving calls over eAPI using a HTTP/S transport.
"""

import sys
import socket
import base64
import logging
Expand All @@ -51,14 +50,8 @@
except ImportError:
import json

try:
# Try Python 3.x import first
from http.client import HTTPConnection, HTTPSConnection
from http.cookies import SimpleCookie
except ImportError:
# Use Python 2.7 import as a fallback
from httplib import HTTPConnection, HTTPSConnection
from Cookie import SimpleCookie
from http.client import HTTPConnection, HTTPSConnection
from http.cookies import SimpleCookie

from pyeapi.utils import make_iterable

Expand Down Expand Up @@ -250,22 +243,24 @@ def connect(self):
Redefined/copied and extended from httplib.py:1105 (Python 2.6.x).
This is needed to pass cert_reqs=ssl.CERT_REQUIRED as parameter
to ssl.wrap_socket(), which forces SSL to check server certificate
against our client certificate.
to ssl.wrap_socket() (Now changed to ssl.SSLContext.wrap_socket()
as the former has been deprecated from python 3.7), which forces
SSL to check server certificate against our client certificate.
"""
sock = socket.create_connection((self.host, self.port), self.timeout)
if self._tunnel_host:
self.sock = sock
self._tunnel()
# If there's no CA File, don't force Server Certificate Check
if self.ca_file:
self.sock = ssl.wrap_socket(sock, self.key_file, self.cert_file,
ca_certs=self.ca_file,
cert_reqs=ssl.CERT_REQUIRED)
self.sock = ssl.SSLContext.wrap_socket(sock, self.key_file,
self.cert_file,
ca_certs=self.ca_file,
cert_reqs=ssl.CERT_REQUIRED)
else:
self.sock = ssl.wrap_socket(sock, self.key_file,
self.cert_file,
cert_reqs=ssl.CERT_NONE)
self.sock = ssl.SSLContext.wrap_socket(sock, self.key_file,
self.cert_file,
cert_reqs=ssl.CERT_NONE)


class EapiConnection(object):
Expand Down Expand Up @@ -302,16 +297,8 @@ def authentication(self, username, password):
"""
_auth_text = '{}:{}'.format(username, password)

# Work around for Python 2.7/3.x compatibility
if int(sys.version[0]) > 2:
# For Python 3.x
_auth_bin = base64.encodebytes(_auth_text.encode())
_auth = _auth_bin.decode()
else:
# For Python 2.7
_auth = str(base64.encodestring(_auth_text))
_auth = _auth.replace('\n', '')
_auth_bin = base64.encodebytes(_auth_text.encode())
_auth = _auth_bin.decode().replace('\n', '')
self._auth = ("Authorization", "Basic %s" % _auth)

_LOGGER.debug('Authentication string is: {}:***'.format(username))
Expand Down Expand Up @@ -436,24 +423,15 @@ def send(self, data):
# debug('eapi_request: %s' % data)

self.transport.putrequest('POST', '/command-api')

self.transport.putheader('Content-type', 'application/json-rpc')
self.transport.putheader('Content-length', '%d' % len(data))

if self._auth:
self.transport.putheader(*self._auth)

if int(sys.version[0]) > 2:
# For Python 3.x compatibility
data = data.encode()
data = data.encode()

self.transport.endheaders(message_body=data)

try: # Python 2.7: use buffering of HTTP responses
response = self.transport.getresponse(buffering=True)
except TypeError: # Python 2.6: older, and 3.x on
response = self.transport.getresponse()

response = self.transport.getresponse()
response_content = response.read()
_LOGGER.debug('Response: status:{status}, reason:{reason}'.format(
status=response.status,
Expand Down Expand Up @@ -739,8 +717,7 @@ def authentication(self, username, password):
self.transport.putheader("Content-type", "application/json")
self.transport.putheader("Content-length", "%d" % len(data))

if int(sys.version[0]) > 2:
data = data.encode()
data = data.encode()
self.transport.endheaders(message_body=data)
resp = self.transport.getresponse()
if resp.status != 200:
Expand Down
10 changes: 2 additions & 8 deletions pyeapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,8 @@
import logging.handlers

from collections.abc import Iterable
from itertools import tee

try:
# Try Python 3.x import first
from itertools import zip_longest
except ImportError:
# Use Python 2.7 import as a fallback
from itertools import izip_longest as zip_longest
from itertools import tee, zip_longest


_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.DEBUG)
Expand Down
5 changes: 1 addition & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@

'License :: OSI Approved :: BSD License',

'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3 :: Only'
],

keywords='networking arista eos eapi',
Expand Down

0 comments on commit bf9976f

Please sign in to comment.