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 |
import smtplib from email.mime.text import MIMEText as text def test_sendmail(): to_address ='"PersonA"<a.b@gmail.com>' body = "This is the email body!" subject="Hello World" sendmail(to_address, subject, body) def sendmail(to_address, subject, body): from_address='"This Is Me"<a.b@gmail.com>' smtp_server = 'smtp.gmail.com' smtp_port= 587 smtp_user="a.b@gmail.com" smtp_password="abcdefgh" #---------compose------- msg = text(body) msg['Subject'] = subject msg['From'] = from_address msg['To'] = to_address #---------send------- server = smtplib.SMTP(smtp_server, smtp_port) server.ehlo() server.starttls() server.login(smtp_user, smtp_password) server.sendmail(from_address, to_address, msg.as_string()) server.quit() print "done" def help(): print """ remember to turn on "Access for less secure apps" in GMail via Link beforehand https://www.google.com/settings/security/lesssecureapps """ if __name__ == '__main__': help() test_sendmail() |
Category Archives: programming
Play Framework – Activator – Fix for IllegalArgumentException: empty text
In case of calling an Play Server via HTTPS, an strange Exception is thrown as shown ... Read more
JQuery – Alternatives and Drop-In Replacement of jQuery JavaScript Library
The following small javascript libraries can be used instead of and as drop-in replacements ... Read more
Lean Virtual Servers in Germany 2015
The following companies offer virtual servers on transparent, lean and competitive ... Read more
Python – String to Number Hash
1 2 3 4 5 6 7 |
def string2numeric_hash(text): import hashlib return int(hashlib.md5(text).hexdigest()[:8], 16) # test >>print string2numeric_hash('this is a nice string') 1962341389 |
Python – Check if the internet works :)
Just check a google server
74.125.228.100 is ... Read more
1 2 3 4 5 6 7 |
import urllib2 def internet_on(): try: response=urllib2.urlopen('http://74.125.228.100',timeout=1) return True except urllib2.URLError as err: pass return False |
Requests per Second from Logfiles – Nginx, Apache
Requests per Second from Logfiles - Nginx, Apache
As ... Read more
1 2 |
http://stackoverflow.com/questions/4051555/how-do-i-get-the-number-of-requests-per-second-from-apache-log-between-2-dates-t while true; do tail -n0 -f /var/log/wowza/wowzamediaserver_access.log>/tmp/tmp.log & sleep 2; kill $! ; wc -l /tmp/tmp.log | cut -c-2; done 2>/dev/null |
Python – Headless Selenium Performance Tests with Google Chrome, Browsermob-Proxy and Xvfb
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 |
# -*- coding: utf-8 -*- """ # Install xvfb sudo apt-get install -y xvfb # create build directory mkdir -p ~/build/selenium cd ~/build/selenium # Install API for browsermob-proxy and selenium sudo pip install selenium browsermob-proxy --upgrade # download browsermob proxy wget https://github.com/downloads/webmetrics/browsermob-proxy/browsermob-proxy-2.0-beta-6-bin.zip unzip browsermob-proxy-2.0-beta-6-bin.zip # copy browsermob-proxy to /var/lib sudo cp -r browsermob-proxy /var/lib/ sudo chown -R a:a /var/lib/browsermob-proxy # create log directory mkdir -p log # download selenium-server wget http://selenium-release.storage.googleapis.com/2.41/selenium-server-standalone-2.41.0.jar # start selenium-server java /usr/bin/java -jar selenium-server-standalone-2.41.0.jar >> ./log/selenium.$(date +"%Y%d%m").log 2>&1& # download chrome driver wget http://chromedriver.storage.googleapis.com/2.9/chromedriver_linux64.zip unzip chromedriver_linux64.zip # chromedriver """ import sys import os def main(): from xvfbwrapper import Xvfb with Xvfb() as xvfb: #if True: open_page_in_selenium() def open_page_in_selenium(browser='chrome'): import os from selenium import webdriver browsermob_proxy_filepath = "/var/lib/browsermob-proxy/bin/browsermob-proxy" # Create Proxy Server from browsermobproxy import Server server = Server(browsermob_proxy_filepath) server.start() proxy = server.create_proxy() # Create Webdriver #driver = webdriver.Firefox() # Create Chrome Driver - unused chromedriver = "./chromedriver" os.environ["webdriver.chrome.driver"] = chromedriver #driver = webdriver.Chrome(chromedriver) #chrome_options = webdriver.ChromeOptions() #chrome_options.add_argument("--proxy-server={0}".format(proxy.proxy)) #driver = webdriver.Chrome(chrome_options = chrome_options) # Create Profile using Proxy Server profile = webdriver.FirefoxProfile() profile.set_proxy(proxy.selenium_proxy()) driver = webdriver.Firefox(firefox_profile=profile) # Create HAR proxy.new_har("myhar") url = "http://www.python.org" try: from datetime import datetime print "%s: Go %s"%(datetime.now(), url) driver.get(url) print "%s: Finish %s"%(datetime.now(), url) #from selenium.webdriver.common.keys import Keys # submit query #elem = driver.find_element_by_name("q") #elem.send_keys("selenium") #elem.send_keys(Keys.RETURN) web_har = proxy.har # returns a HAR JSON blob print web_har # Get Additional Performance Data performance = driver.execute_script("return window.performance") print performance print "%s: Complete %s"%(datetime.now(), url) finally: driver.close() server.stop() if __name__ == "__main__": main() pass |
Unix – Permanent SSH Tunnel – The easy way :)
OK, this is a dead easy instruction of setting up a permanent ssh tunnel through ... Read more
SED – Custom Separators – Dealing with Filepaths
1 2 3 4 5 6 |
a@t400:~$ echo "hello"| sed -e 's/e/ö/g' höllo a@t400:~$ echo "hello"| sed -e 's#e#ö#g' höllo a@t400:~$ echo "hello"| sed -e 's^e^ö^g' höllo |
Python – Using the Bing Search API
Introduction
Bing is a great search engine with very generous API volumes for ... Read more
Python – Spynner – Test Scripts
Spynner is a stateful programmatic web browser module for Python based on PyQT and ... Read more
Unix – Rename Files recursively using a for-loop and find command
1 2 3 4 5 6 |
find . -name "*.flv.mp4" | while read file; do echo "$file"; mv "$file" "${file%.flv.mp4}.mp4" #mv "$file" "$(expr "$file" : '\(.*\)\.flv.mp4').mp4" done; |
Python – Simple Websockets Example using Flask and gevent
Below is a simple Websockets Echo Server using Flask and gevent 🙂
Installation
requirements.txt ... Read more
Python – Spynner Installation in Ubuntu
Spynner is a stateful programmatic web browser module for Python with Javascript/AJAX ... Read more
Python – Performance Tests of Regular Expressions
Regular Expressions: To Compile or not to Compile ?
When using regular expressions ... Read more
redis – Installation and Simple Test using redis-py in Ubuntu
Installation
Redis is a fast key value store developed by Salvatore Sanfilippo (antirez).
Redis ... Read more
pylibmc – Kestrel Queue Client – Simple Test using Memcache Protocol
Installation
pylibmc is a great Python client for memcached written in C.
Mini ... Read more
1 |
sudo pip install pylibmc |
beanstalkc – beanstalkd queue – python client library
Installation
beanstalkc is a c-based python client library for beanstalkd.
Installation ... Read more
Apache Cassandra CQL3 – Installation and Tests using Python
Installation using PIP
Manual Installation from ... Read more
1 2 3 4 5 |
# install python-pip sudo apt-get install -y python-pip # install cql driver sudo pip install cql |
pycassa – Gettting started with Apache Cassandra using python
Install easy_install and pip
Install pycassa
Test ... Read more
1 2 3 4 5 |
# install easy_install sudo apt-get install -y python-setuptools # install python-pip sudo apt-get install -y python-pip |
1 |
sudo pip install pycassa |
Python – pyinotify – Observe Files and Directories – open, close, delete
1 |
sudo pip install pyinotify |
1 |
a@a:~$ vim notify_test.py |
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 |
import pyinotify class MyEventHandler(pyinotify.ProcessEvent): def process_IN_ACCESS(self, event): print "ACCESS event:", event.pathname def process_IN_ATTRIB(self, event): print "ATTRIB event:", event.pathname def process_IN_CLOSE_NOWRITE(self, event): print "CLOSE_NOWRITE event:", event.pathname def process_IN_CLOSE_WRITE(self, event): print "CLOSE_WRITE event:", event.pathname def process_IN_CREATE(self, event): print "CREATE event:", event.pathname def process_IN_DELETE(self, event): print "DELETE event:", event.pathname def process_IN_MODIFY(self, event): print "MODIFY event:", event.pathname def process_IN_OPEN(self, event): print "OPEN event:", event.pathname def main(): # watch manager wm = pyinotify.WatchManager() wm.add_watch('/var/log', pyinotify.ALL_EVENTS, rec=True) # event handler eh = MyEventHandler() # notifier notifier = pyinotify.Notifier(wm, eh) notifier.loop() if __name__ == '__main__': main() |
1 |
python notify_test.py |
1 2 3 4 5 6 7 8 |
MODIFY event: /var/log/auth.log MODIFY event: /var/log/syslog MODIFY event: /var/log/syslog MODIFY event: /var/log/auth.log MODIFY event: /var/log/auth.log MODIFY event: /var/log/syslog MODIFY event: /var/log/auth.log MODIFY event: /var/log/syslog |
Selenium – Solving the “Can’t load the profile” Exception
I had a wierd "Can't load the profile" exception in a selenium script that simply ... Read more
Lucene 4 – Getting Started – Demo File Search
Download Lucene and Index Files
Demo - File ... Read more
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 |
# set lucene version version="4.4.0" # set base directories basedir=~/build/lucene/lucene-$version indexdir=$basedir/data/index # create direct for Lucene code and binaries mkdir -p $indexdir cd $basedir/.. # download lucene if [ ! -e lucene-$version.zip ]; then wget http://apache.imsam.info/lucene/java/$version/lucene-$version.zip fi; # extract unzip -oq lucene-$version.zip # create directory to save the search index mkdir -p $basedir/data/index # start demo indexing cd lucene-$version # set classpath classpath=$classpath:$basedir/demo/lucene-demo-$version.jar classpath=$classpath:$basedir/core/lucene-core-$version.jar classpath=$classpath:$basedir/analysis/common/lucene-analyzers-common-$version.jar # Do index the directory with lucene documentation java -classpath $classpath org.apache.lucene.demo.IndexFiles -index $indexdir -docs $basedir/docs # check content of index directory echo $indexdir ls -lh $indexdir |
Linux – Install Eclipse IDE
The easiest way to install Eclipse in Ubuntu is through the standard repository
Unfortunately, ... Read more
1 |
sudo apt-get install -y eclipse |
Unix – Count Requests Per Seconds From Access Log File
1 |
egrep "^14:59" access.log|grep exectime| sed 's/.*14:59.\(..\).*/\1/g'|sort |uniq -c|sort -n |
1 |
egrep "^14:" access.log|grep exectime| sed 's/.*14:\(.....\).*/\1/g'|sort |uniq -c|sort -n |
1 |
grep exectime access.log | awk '{print $1,$16,$7}' > se1.log |
1 |
tail -f /var/logs/access.log|grep exectime | awk '{print $1,$16,$7}' |
1 |
grep "job processed" res.log | awk '{ print $1" "$2}'|sort -n|uniq -c|less |
1 2 3 4 5 6 7 8 9 |
6 2013-01-31 18:39:44 4 2013-01-31 18:39:45 34 2013-01-31 18:39:46 150 2013-01-31 18:39:47 11 2013-01-31 18:39:48 20 2013-01-31 18:39:49 1 2013-01-31 18:39:50 17 2013-01-31 18:39:51 4 2013-01-31 18:39:52 |
Unix – Extract multiple rar and zip files
Extract multiple zip files
Extract multiple ... Read more
1 |
for z in *.zip; do unzip -f $z; done |
ffmpeg – Convert Adobe Flash FLV Files to MP4 Files
Simple Conversion of FLV to MP4 using FFMPEG
ffmpeg chews the input buffers - ... Read more
Unix – grep, sort , uniq
Source File
... Read more
1 2 3 4 5 6 7 8 9 10 11 12 13 |
cat genre.txt ./014/735/54/00000000000001473554.xml <Genre name="Pop"> ./014/735/10/00000000000001473510.xml <Genre name="Pop"> ./014/726/18/00000000000001472618.xml <Genre name="Dance"> ./014/726/26/00000000000001472626.xml <Genre name="Rock"> ./014/726/08/00000000000001472608.xml <Genre name="Rap"> ./014/726/42/00000000000001472642.xml <Genre name="90's Rock"> |
1 |
grep Genre genre.txt |sed 's/.*Genre name="\(.*\).>.*/\1/g'|sort|uniq –c |
1 2 |
grep Genre genre.txt |sed 's/.*Genre name="\(.*\).>.*/\1/g'\ |sort|uniq -c|awk '{ print $2"\t"$1 }'|sort |sed 's/&apos//g' |
1 |
grep Genre genre.txt |sed 's/.*Genre name="\(.*\).>.*/\1/g’ |
Unix – Extract File Extension and Filename from Filepath
1 2 |
#file extension for f in *; do fn="${f%.*}";echo "fn=$fn,f=$f"; done; |