Category: python
-
Python – Send Email via Google GMail and Python Script
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,…
-
Python – String to Number Hash
def string2numeric_hash(text): import hashlib return int(hashlib.md5(text).hexdigest()[:8], 16) # test >>print string2numeric_hash(‘this is a nice string’) 1962341389 References 1. http://stackoverflow.com/questions/2511058/persistent-hashing-of-strings-in-python
-
Python – Check if the internet works 🙂
Just check a google server 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 74.125.228.100 is one of the IP-addresses for google.com. Change http://74.125.228.100 to whatever site can be expected to respond quickly. Using a numerical IP-address avoids a DNS lookup, which may block the urllib2.urlopen call for more…
-
Python – Headless Selenium Performance Tests with Google Chrome, Browsermob-Proxy and Xvfb
# -*- 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…
-
Python – Using the Bing Search API
Introduction Bing is a great search engine with very generous API volumes for developers. Currently 5.000 queries per month can be called for free. Compare with Google: 100 per day * 30 = 3.000 per month. There a couple of Python libraries which used to work with Bing API. I tried pybing but the the…
-
Python – Spynner – Test Scripts
Spynner is a stateful programmatic web browser module for Python based on PyQT and WebKit. How to install Spynner – easy 🙂 > cat spynner-test.py # -*- coding: utf-8 -*- import spynner import pyquery import os os.environ[‘DISPLAY’] = ‘:0.0’ def main(): browse_via_jquery() browse_using_xpath() browse_via_webkit() download_image() def browse_via_jquery(): browser = spynner.Browser(debug_level=spynner.DEBUG) browser.create_webview() browser.show() #browser.hide() browser.load(“http://www.wordreference.com”) browser.load_jquery(True)…
-
Python – Simple Websockets Example using Flask and gevent
Below is a simple Websockets Echo Server using Flask and gevent 🙂 Installation requirements.txt – easily build all depencies cat > requirements.txt
-
Python – Spynner Installation in Ubuntu
Spynner is a stateful programmatic web browser module for Python with Javascript/AJAX support based upon the QtWebKit framework. Quick Install # build LibQt4 dependencies sudo apt-get build-dep -y libqt4-dev # install python Qt4 library sudo apt-get install -y python-qt4 # x11 sudo apt-get install -y libxtst-dev xvfb x11-xkb-utils # fonts sudo apt-get install -y xfonts-100dpi…
-
Python – Performance Tests of Regular Expressions
Regular Expressions: To Compile or not to Compile ? When using regular expressions one often has to decide whether to compile the expressions before applying them. Below is a simple test I ran and the results. tee ./regextest.py python regextest.py python regextest.py ————————————————– 1 calls in 0.194 ms – compiled_once 1 calls in 0.124 ms…
-
redis – Installation and Simple Test using redis-py in Ubuntu
Installation Redis is a fast key value store developed by Salvatore Sanfilippo (antirez). Redis can be viewed as a next level memcached server. It is easy to install and has lots of cool features. Ubuntu Repository Installation # standard ubuntu repository installs an old redis version – redis 2.4.15 sudo apt-get install -y redis-server Installation…