Saverio Serravezza a little stupid journal

python - check if website is online and available

A pythonic way to simple check if a list of website is online. Simply we check the HEAD response from url using httplib.
If we get headers response, well just print it out the headers code and name.
If we get an exception instead, this means the website is Offline, we send email alert.
A more deep usage could be done checking a particular unwanted HTTP status, like 5xx or not found 404.

Here is the simple code:

import httplib
import urllib2
from smtplib import SMTP
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

## defining email alert message
def email_alert(message, urldown):
    toaddrs = ['recipient@email.com']
    msg = MIMEMultipart()
    msg['Subject'] = 'OFFLINE '+str(urldown)
    msg['From'] = 'Sender Name '
    msg['To'] = 'Recipient Name '
    body = str(message)
    msg.attach(MIMEText(body, 'plain'))
    try:
        smtpObj = SMTP('smtp. email.com')
        smtpObj.login('senderusername','password')
        smtpObj.sendmail('Recipient Name ', toaddrs, msg.as_string())
        smtpObj.quit()
        print 'Alert Email sent'
    except Exception,e:
        print 'Error: unable to send email',e


## defining dictionary of url to check
urls=[
    'google.com',
    'yahoo.com',
    'gooxxxxxxxgle.com',
    ]

## first we check if internet is available
try:
    urllib2.urlopen("http://google.com", timeout=2)
    for url in urls:
        try:
            ## check if we get url headers
            conn = httplib.HTTPConnection(url)
            conn.request("HEAD", "/")
            r1 = conn.getresponse()
            ## response is ok
            ## check if we get a server error 5xx
            if str(r1.status).startswith('5'):
                email_alert(str(r1.status)+ ' '+str(r1.reason), url)
                print url, '--- OFFLINE: ',r1.status, r1.reason
            else:
                print url,' ONLINE: ',r1.status, r1.reason
        except Exception,e:
            ## response get an exception, print it and send alert email
            print url, '--- OFFLINE: ',str(e)
            email_alert(str(e), url)
        print '-'*30
except urllib2.URLError:
    # there is no connection
    print 'there is no internet connection'
    

magento - python - reindex only when is needed

As you will know best way to reindex data in Magento is from shell, php cli.
Well,

php /path/to/magento/shell/indexer.php -reindexall
will reindex all data, also when no reindex is needed.. Lot's of waste of time and cpu load!!
Well.. or you can reindex a index only:
php /path/to/magento/shell/indexer.php --reindex catalog_url
But how to check if you need to reindex that particular index? Simply reading the output of:
php /path/to/magento/shell/indexer.php --status
I use the beautifull python "magento" library to connect with Magento Api and sync with ERP, stocks managements, CMS, etc..
So this solution is "pythonic" and only use php to call shell cli.. but can easily converted in pure php (if really needed.. python is so beautiful..)

## s.s. - s@saverioserravezza.it
## Magento - python/php - reindex only when is needed

import os
import subprocess

## dictionary to convert human index name in shell index name
dict_reind={'ProductAttributes':'catalog_product_attribute','ProductPrices':'catalog_product_price','CatalogURLRewrites':'catalog_url','ProductFlatData':'catalog_product_flat','CategoryFlatData':'catalog_category_flat','CategoryProducts':'catalog_category_product','CatalogSearchIndex':'catalogsearch_fulltext','Stockstatus':'cataloginventory_stock','TagAggregationData':'tag_summary'}

def reindex():
    ## check what needed to be reindexed
    needed = subprocess.check_output('php /path/to/magento/shell/indexer.php --status', shell=True)
    needed = needed.replace(' ', '').split('\n')
    for line in needed:
        if line !='':
            if line.split(':')[1]=='RequireReindex':
                needreind=line.split(':')[0]
                print '--->>>> reindexing '+needreind
                os.system('php /var/www/unp/web/shell/indexer.php --reindex '+dict_reind[needreind])

magento missing canonical full path for products

Latest versions of Magento introduced canonical url both for category and product pages.
The problem is that canonical url for product is not really SEO friendly..
You will have "www.domain.com/uniqueproductname.html"
and not "www.domain.com/catname/subcatname/uniqueproductname.html"
as it should be in a SEO vision..

Here is a short workaround to build a full canonical url for products pages:

// s.s. - s@saverioserravezza.it
// Magento - php - canonical full path for products
$product  = Mage::registry('product');
if ($product) {
    $productid = Mage::registry('currentproduct')->getId();
    $cats = $product->getCategoryIds();
    foreach ($cats as $categoryid) {
        $cat = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($categoryid);
            // get last category id
            $catid = $cat->getId();
        }
    $idpath = "product/".$productid."/".$catid;
    $storeId = Mage::app()->getStore()->getStoreId();
    $canonicalpath = Mage::getResourceModel('core/urlrewrite')
        ->getRequestPathByIdPath($idpath, $storeId);
    // get canonical
    $canonicalpath = Mage::getBaseUrl(MageCoreModelStore::URLTYPE_WEB).$canonicalpath;
    echo "";
}
// s.s. canonical full path for products

la stanza dei morti - lalish - Iraq 2011

Lalish village - Iraq [6-06-2011]

Lalish village - Iraq [6-06-2011]

apt-get update && apt-get install felicità

processing triggers for man-db …
setting up felicità
error processing man-db (--unpack):
subprocess installed post-installation script returned error exit status 1

triftling

… triftling all the time triftling all the time

a me medesimo (chi tu sie..)

Chi tu sie ch'io mi credea di conoscerti, tremula imago di me stesso..
Mi sapea di nobili natali, et ho errato.
Ti credeva amato da una bianca dama, et ho errato.
Ti ritenea gran capitano di grande impresa, et ho errato..
Verrò nomato..LO CAVALIERE ERRANTE!
[m. monicelli ]

tutto

6 CO2 + 6 H2O + hv → C6H12O6 + 6 O2

lucania un luogo vero

"La Lucania mi pare più di ogni altro, un luogo vero, uno dei luoghi più veri del mondo. qui ritrovo la misura delle cose. le lotte e i contrasti qui sono cose vere. il pane che manca è un vero pane, la casa che manca è una vera casa, il dolore che nessuno intende un vero dolore. la tensione interna di questo mondo è la ragione della sua verità: in esso storia e mitologia, attualità e eternità sono coincidenti"
Carlo Levi

cattiva dacia!

© 2015 saverio serravezza. All rights reserved.