Python
Learning Python scratch pad
Vim as Python IDE
Ubuntu comes with vim-tiny package that has most of things disabled, so get it uninstalled
sudo apt-get purge vim-tiny sudo apt-get install vim #py3 support sudo apt-get install vim-nox-py2 #py2 support vim --version | grep python #verify you should see eg: +python -python3
Optimally you may need to set alternatives
sudo update-alternatives --set vim /usr/bin/vim.nox-py2
Now, open vim and check :python import sys; print(sys.version)
you should see similar output
2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
Simple HTTP Server
It provides the local files browser over http protocol and access logs
sudo python -m SimpleHTTPServer 80
print()
Print in v3 is a function, it uses arguments with variables like below:
%s - String (or any object with a string representation, like numbers) %d - Integers %f - Floating point numbers %.<number of digits>f - Floating point numbers with a fixed amount of digits to the right of the dot. %x/%X - Integers in hex representation (lowercase/uppercase)
Eaxmple:
print("%s is %d years old." % (name, age))
Install Selenium lib on Ubuntu
$ which python python3
/usr/bin/python
/usr/bin/python3
sudo apt-get install python3-pip # install pip for python3
sudo python3 -m pip install -U selenium # install selenium for python3
sudo python3 -m pip install -U pip #upgrade pip
Install geckodriver on Ubuntu
It's required if you use Selenium in Python, webdriver function:
default_browser = webdriver.Firefox()
Install
## Geckodriver (verified on Ubuntu 16.04 running on Vagrant)
wget https://github.com/mozilla/geckodriver/releases/download/v0.21.0/geckodriver-v0.21.0-linux64.tar.gz
sudo sh -c 'tar -x geckodriver -zf geckodriver-v0.21.0-linux64.tar.gz -O > /usr/bin/geckodriver'
sudo chmod +x /usr/bin/geckodriver
rm geckodriver-v0.21.0-linux64.tar.gz
## Chromedriver
wget https://chromedriver.storage.googleapis.com/2.29/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
sudo chmod +x chromedriver
sudo mv chromedriver /usr/bin/
rm chromedriver_linux64.zip
Login and scrape website
Script below demonstrate usage of Selenium automation through Mozilla Geckodriver to login to fill in a form, click a login button and take a screenshot.
Credentials file
cat creds.yaml
wpUser: user
wpPass: pass123
Main script
#!/usr/bin/python3
from selenium import webdriver
import yaml
driver = webdriver.Firefox()
creds = yaml.safe_load(open('creds.yaml','r'))
driver.get('http://wiki.ciscolinux.co.uk/index.php?title=Special:UserLogin&returnto=Main+Page')
user_field = driver.find_element_by_name("wpName")
pass_field = driver.find_element_by_name("wpPassword")
user_field.send_keys(creds['wpUser'])
pass_field.send_keys(creds['wpPass'])
# find login button and click on it
driver.find_element_by_name('wploginattempt').click()
driver.save_screenshot('screenshot.png')
driver.close
References
- A Beginner's Python Tutorial Wikibooks
- List of free Python learning resources
- VIM and Python - a Match Made in Heaven Vim as Python IDE
- Vundle Vim bundle/plugins manager