Friday, June 22, 2018

Python Tips and Tricks

1. Install Python 2.7 / 3.0 on AWS EC2 Instance



[[email protected] pexpect-2.3]$ python --version
Python 2.7.14



[[email protected] pexpect-2.3]$ sudo yum install python
Loaded plugins: priorities, update-motd, upgrade-helper
amzn-main                                                                             | 2.1 kB  00:00:00
amzn-updates                                                                          | 2.5 kB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package python26.x86_64 0:2.6.9-2.89.amzn1 will be installed
--> Processing Dependency: libpython2.6.so.1.0()(64bit) for package: python26-2.6.9-2.89.amzn1.x86_64
--> Running transaction check
---> Package python26-libs.x86_64 0:2.6.9-2.89.amzn1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================
 Package                    Arch                Version                         Repository              Size
=============================================================================================================
Installing:
 python26                   x86_64              2.6.9-2.89.amzn1                amzn-main              5.8 M
Installing for dependencies:
 python26-libs              x86_64              2.6.9-2.89.amzn1                amzn-main              697 k

Transaction Summary
=============================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 6.4 M
Installed size: 21 M
Is this ok [y/d/N]: ^Cn
Exiting on user command
Your transaction was saved, rerun it with:
 yum load-transaction /tmp/yum_save_tx.2018-06-21.18-42.2ccPPk.yumtx

[[email protected] pexpect-2.3]$ python --version
Python 2.7.14
[[email protected] pexpect-2.3]$ sudo apt-get install python3
sudo: apt-get: command not found
[[email protected] pexpect-2.3]$ sudo yum install python
Loaded plugins: priorities, update-motd, upgrade-helper
amzn-main                                                                             | 2.1 kB  00:00:00
amzn-updates                                                                          | 2.5 kB  00:00:00
Package python26-2.6.9-2.89.amzn1.x86_64 already installed and latest version
Nothing to do

[[email protected] pexpect-2.3]$ sudo yum list | grep python3
mod24_wsgi-python34.x86_64            3.5-1.25.amzn1                amzn-updates
mod24_wsgi-python35.x86_64            3.5-1.25.amzn1                amzn-updates
mod24_wsgi-python36.x86_64            3.5-1.25.amzn1                amzn-updates
python34.x86_64                       3.4.8-1.39.amzn1              amzn-updates
python34-devel.x86_64                 3.4.8-1.39.amzn1              amzn-updates
python34-docs.noarch                  3.4.3-1.23.amzn1              amzn-main
python34-libs.i686                    3.4.8-1.39.amzn1              amzn-updates
python34-libs.x86_64                  3.4.8-1.39.amzn1              amzn-updates
python34-pip.noarch                   9.0.3-1.26.amzn1              amzn-updates
python34-setuptools.noarch            36.2.7-1.33.amzn1             amzn-main
[[email protected] pexpect-2.3]$ sudo yum install python34
Loaded plugins: priorities, update-motd, upgrade-helper
Resolving Dependencies
--> Running transaction check
---> Package python34.x86_64 0:3.4.8-1.39.amzn1 will be installed
--> Processing Dependency: python34-libs(x86-64) = 3.4.8-1.39.amzn1 for package: python34-3.4.8-1.39.amzn1.x86_64
--> Processing Dependency: libpython3.4m.so.1.0()(64bit) for package: python34-3.4.8-1.39.amzn1.x86_64
--> Running transaction check
---> Package python34-libs.x86_64 0:3.4.8-1.39.amzn1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

=============================================================================================================
 Package                   Arch               Version                         Repository                Size
=============================================================================================================
Installing:
 python34                  x86_64             3.4.8-1.39.amzn1                amzn-updates              54 k
Installing for dependencies:
 python34-libs             x86_64             3.4.8-1.39.amzn1                amzn-updates             9.8 M

Transaction Summary
=============================================================================================================
Install  1 Package (+1 Dependent package)

Total download size: 9.8 M
Installed size: 27 M
Is this ok [y/d/N]: y
Downloading packages:
(1/2): python34-3.4.8-1.39.amzn1.x86_64.rpm                                           |  54 kB  00:00:00
(2/2): python34-libs-3.4.8-1.39.amzn1.x86_64.rpm                                      | 9.8 MB  00:00:00
-------------------------------------------------------------------------------------------------------------
Total                                                                         20 MB/s | 9.8 MB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : python34-3.4.8-1.39.amzn1.x86_64                                                          1/2
  Installing : python34-libs-3.4.8-1.39.amzn1.x86_64                                                     2/2
  Verifying  : python34-libs-3.4.8-1.39.amzn1.x86_64                                                     1/2
  Verifying  : python34-3.4.8-1.39.amzn1.x86_64                                                          2/2

Installed:
  python34.x86_64 0:3.4.8-1.39.amzn1

Dependency Installed:
  python34-libs.x86_64 0:3.4.8-1.39.amzn1

Complete!
[[email protected] pexpect-2.3]$
[[email protected] pexpect-2.3]$ python --version
Python 2.7.14
[[email protected] pexpect-2.3]$ python3 --version
Python 3.4.8
[[email protected] pexpect-2.3]$





2. Use Python Pexpect module to do SSH/FTP connection




[[email protected] ~]$ wget http://pexpect.sourceforge.net/pexpect-2.3.tar.gz
--2018-06-21 15:38:31--  http://pexpect.sourceforge.net/pexpect-2.3.tar.gz
Resolving pexpect.sourceforge.net (pexpect.sourceforge.net)... 216.105.38.10
Connecting to pexpect.sourceforge.net (pexpect.sourceforge.net)|216.105.38.10|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 150868 (147K) [application/x-gzip]
Saving to: ‘pexpect-2.3.tar.gz’

pexpect-2.3.tar.gz          100%[========================================>] 147.33K   680KB/s    in 0.2s

2018-06-21 15:38:31 (680 KB/s) - ‘pexpect-2.3.tar.gz’ saved [150868/150868]

[[email protected] ~]$ tar xzf pexpect-2.3.tar.gz
[[email protected] ~]$ cd pexpect-2.3
[[email protected] pexpect-2.3]$ sudo python ./setup.py install
running install
running build
running build_py
creating build
creating build/lib
copying pexpect.py -> build/lib
copying pxssh.py -> build/lib
copying fdpexpect.py -> build/lib
copying FSM.py -> build/lib
copying screen.py -> build/lib
copying ANSI.py -> build/lib
running install_lib
copying build/lib/FSM.py -> /usr/local/lib/python2.7/site-packages
copying build/lib/fdpexpect.py -> /usr/local/lib/python2.7/site-packages
copying build/lib/pexpect.py -> /usr/local/lib/python2.7/site-packages
copying build/lib/ANSI.py -> /usr/local/lib/python2.7/site-packages
copying build/lib/pxssh.py -> /usr/local/lib/python2.7/site-packages
copying build/lib/screen.py -> /usr/local/lib/python2.7/site-packages
byte-compiling /usr/local/lib/python2.7/site-packages/FSM.py to FSM.pyc
byte-compiling /usr/local/lib/python2.7/site-packages/fdpexpect.py to fdpexpect.pyc
byte-compiling /usr/local/lib/python2.7/site-packages/pexpect.py to pexpect.pyc
byte-compiling /usr/local/lib/python2.7/site-packages/ANSI.py to ANSI.pyc
byte-compiling /usr/local/lib/python2.7/site-packages/pxssh.py to pxssh.pyc
byte-compiling /usr/local/lib/python2.7/site-packages/screen.py to screen.pyc
running install_egg_info
Writing /usr/local/lib/python2.7/site-packages/pexpect-2.3.egg-info


[[email protected] pexpect-2.3]$ sudo yum install pexpect.noarch
Loaded plugins: priorities, update-motd, upgrade-helper
amzn-main                                                                             | 2.1 kB  00:00:00
amzn-updates                                                                          | 2.5 kB  00:00:00
No package pexpect.noarch available.
Error: Nothing to do
[[email protected] pexpect-2.3]$ sudo apt-get install python-pexpect
sudo: apt-get: command not found
[[email protected] pexpect-2.3]$ python
Python 2.7.14 (default, May  2 2018, 18:31:34)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
















No comments:

Post a Comment