In addition to its web interface Google also provides access via IMAP. The python imaplib module defines three classes, IMAP4, IMAP4_SSL and IMAP4_stream, which encapsulate a connection to an IMAP4 server and implement a large subset of the IMAP4rev1 client protocol as defined in RFC 2060.
The IMAP4 class implements the actual IMAP4 protocol. The connection is created and protocol version (IMAP4 or IMAP4rev1) is determined when the instance is initialized.
Getting started with Python Imaplib
To start with, we will create a simple python program to login to Gmail via IMAP.
import imaplib IMAP_SERVER='imap.gmail.com' IMAP_PORT=993 M = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT) rc, resp = M.login('username@gmail.com', 'pa$$word') print rc, resp M.logout()
IMAP4.IMAP4_SSL is a subclass derived from IMAP4 that connects over an SSL encrypted socket (to use this class you need a socket module that was compiled with SSL support). If host is not specified, ” (the local host) is used. If port is omitted, the standard IMAP4-over-SSL port (993) is used. keyfile and certfile are also optional – they can contain a PEM formatted private key and certificate chain file for the SSL connection.
If authentication is successful the output will be:
OK ['username@gmail.com authenticated (Success)']
As part of our exercise we will be writing may usefull functions. It is good to create a python class file to put our functions so that at the end of our exercise we will have a cool python gmail library. Lets create a pygmail.py
import imaplib class pygmail: def __init__(self): self.IMAP_SERVER='imap.gmail.com' self.IMAP_PORT=993 self.M = None self.response = None def login(self, username, password): self.M = imaplib.IMAP4_SSL(self.IMAP_SERVER, self.IMAP_PORT) rc, self.response = self.M.login(username, password) return rc def logout(self): self.M.logout()
g = pygmail() g.login('username@gmail.com', 'pa$$word') print g.response
Listing Mailboxes
The IMAP4.list() function list mailbox names in directory matching pattern. The directory defaults to the top-level mail folder, and pattern defaults to match anything. Returned data contains a list of LIST responses.
Add the below function to our pygmial.py
def get_mailboxes(self): rc, self.response = self.M.list() for item in self.response: self.mailboxes.append(item.split()[-1]) return rc
Use:
g.get_mailboxes() for item in g.mailboxes: print item
This will output your Gmail mailboxes
"INBOX" "Sent" "Trash" "/" "[Gmail]/All "[Gmail]/Drafts" "[Gmail]/Sent "[Gmail]/Spam" "[Gmail]/Starred" "[Gmail]/Trash" "freebsd-net" "fsug-tvm" "openflow"
Creating, Renaming, Deleting Mailboxes
IMAP4.create, IMAP4.rename, IMAP4.delete functions will create, rename, delete the mailboxes respectively.
Lets add three more functions to out lib.
def rename_mailbox(self, oldmailbox, newmailbox): rc, self.response = self.M.rename(oldmailbox, newmailbox) return rc def create_mailbox(self, mailbox): rc, self.response = self.M.create(mailbox) return rc def delete_mailbox(self, mailbox): rc, self.response = self.M.delete(mailbox) return rc
Get Mail Count
The IMAP4.select function select a mailbox. Returned data is the count of messages in mailbox (EXISTS response). The default mailbox is 'INBOX'. If the readonly flag is set, modifications to the mailbox are not allowed.
Add the get_mail_count function to out Python class.
def get_mail_count(self, folder='Inbox'): rc, count = self.M.select(folder) return count[0]
Output:
14581
You can specify your mailbox also.
g.get_mail_count('mailbox')
Get Unread Mail Count
The IMAP4.status() function request named status conditions for mailbox. The standard defines these status conditions:
MESSAGES - The number of messages in the mailbox.
RECENT - The number of messages with the Recent flag set.
UIDNEXT - The next unique identifier value of the mailbox.
UIDVALIDITY - The unique identifier validity value of the mailbox.
UNSEEN - The number of messages which do not have the Seen flag set.
Using the UNSEEN condition will return total unread messages in Inbox.
Lets define a function to get unread mail count in our pygmail.py.
def get_unread_count(self, folder='Inbox'): rc, message = self.M.status(folder, "(UNSEEN)") unreadCount = re.search("UNSEEN (\d+)", message[0]).group(1) return unreadCount
Output:
2
OK, enough for a start. In the next parts of this article I will explain sending, searching, retrieving mails from Gmail via Python. So dont forget to subscribe
I am pushing our small Python Gmail library to github hopefully useful to someone.
PyGmail: http://github.com/vinod85/pygmail
To get information about CPU and Memory under FreeBSD use the following commands:
Getting CPU information:
From dmesg:
$ dmesg | grep CPU
Or
$ grep CPU /var/run/dmesg.boot | less
Using sysctl:
CPU model:
$ sysctl hw.model
CPU clock rate:
$ sysctl hw.clockrate
No of cpus:
$ sysctl hw.ncpu
Get all information:
$ sysctl -a | grep -i cpu | less
Getting memory information:
From dmesg:
$ dmesg | grep memory
Or
$ grep memory /var/run/dmesg.boot
Using sysctl:
$ sysctl -a | grep mem | less
FreeBSD find out memory usage
Download perl script which is written by Ralf S. Engelschall:
$ wget http://people.freebsd.org/~rse/dist/freebsd-memory -O free $ chmod +x free $ sudo mv free /usr/local/bin/ $ free
Output
SYSTEM MEMORY INFORMATION: mem_wire: 55160832 ( 52MB) [ 22%] Wired: disabled for paging out mem_active: + 33361920 ( 31MB) [ 13%] Active: recently referenced mem_inactive:+ 140701696 ( 134MB) [ 57%] Inactive: recently not referenced mem_cache: + 15224832 ( 14MB) [ 6%] Cached: almost avail. for allocation mem_free: + 1150976 ( 1MB) [ 0%] Free: fully available for allocation mem_gap_vm: + 663552 ( 0MB) [ 0%] Memory gap: UNKNOWN -------------- ------------ ----------- ------ mem_all: = 246263808 ( 234MB) [100%] Total real memory managed mem_gap_sys: + 4841472 ( 4MB) Memory gap: Kernel?! -------------- ------------ ----------- mem_phys: = 251105280 ( 239MB) Total real memory available mem_gap_hw: + 17330176 ( 16MB) Memory gap: Segment Mappings?! -------------- ------------ ----------- mem_hw: = 268435456 ( 256MB) Total real memory installed SYSTEM MEMORY SUMMARY: mem_used: 111357952 ( 106MB) [ 41%] Logically used memory mem_avail: + 157077504 ( 149MB) [ 58%] Logically available memory -------------- ------------ ----------- ------ mem_total: = 268435456 ( 256MB) [100%] Logically total memory
Linux like free command for FreeBSD
Freecolor is a free replacement that displays free memory graphically as a bargraph. It supports the same options as free. Install freecolor, enter:
# pkg_add -r freecolorTo see memory details, enter:
# freecolor -mOutput
Physical : [#######################............] 67% (162/239) Swap : [##################################.] 99% (599/600)
# freecolor -m -oOutput:
total used free shared buffers cached
Mem: 239 77 162 0 0 0
Swap: 600 0 599
Ever wanted to know that someone is really offline or has just gone invisible in GTalk? Here is a small trick. The bellow peace of python code get the list of invisible users from your GTalk buddy list. It uses XMPP module for python. You can install this module in Ubuntu/Debian via apt. It also requires python dns module.
$ sudo aptitude install python-xmpp python-dnspython
Now here is our script. Open your favorite text editor and save the code as ‘gchat.py’. Dont forget to fill your gtalk username and password in the script.
import xmpp # Google Talk constants FROM_GMAIL_ID = "username@gmail.com" GMAIL_PASS = "secret" GTALK_SERVER = "gmail.com" jid=xmpp.protocol.JID(FROM_GMAIL_ID) C=xmpp.Client(jid.getDomain(),debug=[]) if not C.connect((GTALK_SERVER,5222)): raise IOError('Can not connect to server.') if not C.auth(jid.getNode(),GMAIL_PASS): raise IOError('Can not auth with server.') C.sendInitPresence(requestRoster=1) def myPresenceHandler(con, event): if event.getType() == 'unavailable': print event.getFrom().getStripped() C.RegisterHandler('presence', myPresenceHandler) while C.Process(1): pass
Now simply run:
$ python gchat.py
So , Next time do not let anyone fool you , rather catch him Invisibly .
The python-imaplib module defines three classes, IMAP4, IMAP4_SSL and IMAP4_stream, which encapsulate a connection to an IMAP4 server and implement a large subset of the IMAP4rev1 client protocol as defined in RFC 2060.
Here’s a code sample that returns your gmail quota details.
import getpass, imaplib, re p = re.compile('\d+') IMAP_SERVER='imap.gmail.com' IMAP_PORT=993 IMAP_USERNAME='username@gmail.com' M = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT) M.login(IMAP_USERNAME, getpass.getpass()) quotaStr = M.getquotaroot("INBOX")[1][1][0] r = p.findall(quotaStr) if r == []: print "Unlimited Quota Account" r.append(0) r.append(0) print 'Allotted = %f MB'%(float(r[1])/1024) print 'Used = %f MB'%(float(r[0])/1024) M.logout()
The script will ask your gmail password and the output will be something like this:
Allotted = 7476.760000 MB Used = 961.390000 MB
Beautiful Soup is an HTML/XML parser for Python that can turn even invalid markup into a parse tree. It provides simple, idiomatic ways of navigating, searching, and modifying the parse tree.
Here’s some code demonstrating how to extract data from HTML tables using Beautiful Soup.
Include Beautiful Soup in your application with a line like one of the following:
from BeautifulSoup import BeautifulSoup # For processing HTML
Getting started with BeautifulSoup:
#!/usr/bin/python from BeautifulSoup import BeautifulSoup html = ['<html><body><table><tr><td>row 1, cell 1</td><td>row 1, cell 2</td></tr><tr><td>row 2, cell 1</td><td>row 2, cell 2</td></tr></table></html>'] soup = BeautifulSoup(''.join(html)) print soup.prettify()
The prettify method adds strategic newlines and spacing to make the structure of the document obvious. It also strips out text nodes that contain only whitespace.
The output will be:
<html>
<body>
<table>
<tr>
<td>
row 1, cell 1
</td>
<td>
row 1, cell 2
</td>
</tr>
<tr>
<td>
row 2, cell 1
</td>
<td>
row 2, cell 2
</td>
</tr>
</table>
</body>
</html>Here is the ways to navigate the soup:
table = soup.find('table') rows = table.findAll('tr') for tr in rows: cols = tr.findAll('td') for td in cols: text = ''.join(td.find(text=True)) print text+"|", print
and the output will be:
row 1, cell 1| row 1, cell 2| row 2, cell 1| row 2, cell 2|
You can search the soup for tags with certain properties:
Get the table with id ‘mytable’
table = soup.find('table', id="mytable")
Or
table aligned center:
table = soup.find('table', align="center")
Or
border is 3:
table = soup.find('table', border="3")
If you want to fetch data across the web use the urllib module.
import urllib f = urllib.urlopen("http://somesite/table.html") html = f.read()
Installing BeautifulSoup in Debian/Ubuntu:
$ sudo aptitude install python-beautifulsoup
Read More at BeautifulSoup Documentation Page.
That’s it! Have fun!

If PDF is electronic paper, then pdftk is an electronic staple-remover, hole-punch, binder, secret-decoder-ring, and X-Ray-glasses. Pdftk is a simple tool for doing everyday things with PDF documents. Pdftk allows you to manipulate PDF easily and freely. It does not require Acrobat, and it runs on Linux, Windows, Mac OS X, FreeBSD and Solaris.
In Debian/Ubuntu you can install pdftk via apt:
$ sudo aptitude install pdftk
Examples:
Merge Two or More PDFs into a New Document
$ pdftk 1.pdf 2.pdf 3.pdf cat output 123.pdfor (Using Handles):
$ pdftk A=1.pdf B=2.pdf cat A B output 12.pdf
or (Using Wildcards):
$ pdftk *.pdf cat output combined.pdf
Split Select Pages from Multiple PDFs into a New Document
$ pdftk A=one.pdf B=two.pdf cat A1-7 B1-5 A8 output combined.pdf
Rotate the First Page of a PDF to 90 Degrees Clockwise
$ pdftk in.pdf cat 1E 2-end output out.pdf
Rotate an Entire PDF Document’s Pages to 180 Degrees
$ pdftk in.pdf cat 1-endS output out.pdf
Encrypt a PDF using 128-Bit Strength (the Default) and Withhold All Permissions (the Default)
$ pdftk mydoc.pdf output mydoc.128.pdf owner_pw foopass
Same as Above, Except a Password is Required to Open the PDF
$ pdftk mydoc.pdf output mydoc.128.pdf owner_pw foo user_pw baz
Same as Above, Except Printing is Allowed (after the PDF is Open)
$ pdftk mydoc.pdf output mydoc.128.pdf owner_pw foo user_pw baz allow printing
Decrypt a PDF
$ pdftk secured.pdf input_pw foopass output unsecured.pdf
Join Two Files, One of Which is Encrypted (the Output is Not Encrypted)
$ pdftk A=secured.pdf mydoc.pdf input_pw A=foopass cat output combined.pdf
Uncompress PDF Page Streams for Editing the PDF Code in a Text Editor
$ pdftk mydoc.pdf output mydoc.clear.pdf uncompress
Repair a PDF’s Corrupted XREF Table and Stream Lengths (If Possible)
$ pdftk broken.pdf output fixed.pdf
Burst a Single PDF Document into Single Pages and Report its Data to doc_data.txt
$ pdftk mydoc.pdf burst
Report on PDF Document Metadata, Bookmarks and Page Labels
$ pdftk mydoc.pdf dump_data output report.txt
Poppler is a PDF rendering library based on the xpdf-3.0 code base. The poppler-utils package contains pdftops (PDF to PostScript converter), pdfinfo (PDF document information extractor), pdfimages (PDF image extractor), pdftohtml (PDF to HTML converter), pdftotext (PDF to text converter), and pdffonts (PDF font analyzer).
Debian/Ubuntu users can install pdftk via apt:
$ sudo aptitude install poppler-utils
Convert PDF to TEXT
Pdftotext converts Portable Document Format (PDF) files to plain text.
$ pdftotext example.pdf example.txt
If textfile is not specified, pdftotext converts file.pdf to file.txt. If text-file is ´-’, the text is sent to stdout.
To convert page from 3 to 7 (including 3 and 7) use:
$ pdftotext -f 3 -l 7 example.pdf example.txt
To extract only 3rd page
$ pdftotext -f 3 -l 3 example.pdf example.txt
$ pdftotext -layout example.pdf example.txtMaintain the original physical layout of the text and output the text in reading order.
Set the -nopgbrk option if you don’t want insert page breaks.
Uset -opw (owner password) or -upw (user password) options if the PDF file is password protected.
Extract Images From PDF
Pdfimages saves images from a Portable Document Format (PDF) file as Portable Pixmap (PPM), Portable Bitmap (PBM), or JPEG files.
Pdfimages reads the PDF file, scans one or more pages, and writes one PPM, PBM, or JPEG file for each image, image-root-nnn.xxx, where nnn is the image number and xxx is the image type (.ppm, .pbm, .jpg).
Pdfimages extracts the raw image data from the PDF file, without performing any additional transforms. Any rotation, clipping, color inversion, etc. done by the PDF content stream is ignored.
$ pfdimages example.pdf exampleimage
The above command will extract all images from example.pdf. The images will be saved in PPM format.
Use -j option to save images as JPG format
$ pfdimages -j example.pdf exampleimageUse the -f and -l options to specify the startpage and lastpage to scan. To scan pages 3 to 7 (including 3 and 7) use:
$ pfdimages -f 3 -l 7 example.pdf exampleimage
To scan only one specific page use:
$ pfdimages -f 3 -l 3 example.pdf exampleimage
If the PDF file is password protected use -opw or -upw option:
-opw Owner password
-upw User password
Convert PDF to HTML
pdftohtml is a program that converts pdf documents into html. It generates its output in the current working directory.
Usage:
$ pdftohtml file.pdf file.html
If you want to see graphics, you’ll need to use the -c (as in “complex”) option:
$ pdftohtml -c file.pdf file.html
Convert PDF to Image
First you need to have ImageMagick installed in your machine.
To install ImageMagick in Debian/Ubuntu run the following command:
$ sudo aptitude install imagemagick
To convert pdf file to image use the ‘convert‘ command:
$ convert doc.pdf doc.jpeg
convert to tiff
$ convert doc.pdf doc.tiff
for more information look here.
You can make a local copy of the package and debconf selection states using dpkg and debconf-get-selections command. The debconf-get-selections command output the content of current debconf database.
You will require the “debconf-utils” packages to do that.
Install debconf-utils:
$ sudo aptitude install debconf-utils
$ sudo dpkg --get-selections '*' > selection.dpkg $ sudo debconf-get-selections > selection.debconf
Here, “*” makes “selection.dpkg” to include package entries for “purge” too.
You can transfer these 2 files to another computer, and install there with the following.
$ sudo dselect update $ sudo debconf-set-selections < myselection.debconf $ sudo dpkg --set-selections < myselection.dpkg $ sudo apt-get -u dselect-upgrade # or dselect install
If you are thinking about managing many servers in a cluster with practically the same configuration, you should consider to use specialized package such as fai to manage the whole system.
There are several cases where two packages provide two different versions of a program, both of which provide the same core functionality. Users might prefer one over another out of habit, or because the user interface of one package is somehow more pleasing than the interface of another. Other users on the same system might make a different choice.
For example, there might exist two different versions of newsreaders on a system. Which program is invoked is determined by a link pointing from a file with the virtual package name /etc/alternatives/vim to the selected file, e.g., /usr/bin/vim.gtk.
The Debian system has mechanism to install somewhat overlapping programs peacefully using update-alternatives(8). The Perl script update-alternatives provides a way of ensuring that all the files associated with a specified package are selected as a system default.
For example, you can make the vi command select to run vim while installing both vim and nvi packages.
The Debian alternatives system keeps its selection as symlinks in “/etc/alternatives/“. The selection process uses corresponding file in “/var/lib/dpkg/alternatives/“. The “/etc/alternatives” defines default applications for the Debian system – such as, the default application to handle editor, to browse the web etc.
For example, to check what executables provide `vim’, run:
$ sudo update-alternatives --display vim vim - status is auto. link currently points to /usr/bin/vim.gtk /usr/bin/vim.tiny - priority 10 /usr/bin/vim.basic - priority 30 /usr/bin/vim.gtk - priority 50 Current `best' version is /usr/bin/vim.gtk.
or use the ls command
$ ls -l $(type -p vim) lrwxrwxrwx 1 root root 21 2008-09-27 13:10 /usr/bin/vim -> /etc/alternatives/vim $ ls -l /etc/alternatives/vim lrwxrwxrwx 1 root root 16 2010-01-23 14:06 /etc/alternatives/vim -> /usr/bin/vim.gtk
If you want to change it, run:
$ sudo update-alternatives --config vim There are 3 alternatives which provide `vim'. Selection Alternative ----------------------------------------------- 1 /usr/bin/vim.tiny 2 /usr/bin/vim.basic *+ 3 /usr/bin/vim.gtk Press enter to keep the default[*], or type selection number:
And follow the instructions on the screen (basically, press the number next to the entry you’d like better).
To change the default web browser run:
$ sudo update-alternatives --config x-www-browser There are 3 alternatives which provide `x-www-browser'. Selection Alternative ----------------------------------------------- * 1 /usr/bin/iceweasel + 2 /usr/bin/epiphany-gecko 3 /usr/bin/google-chrome Press enter to keep the default[*], or type selection number:
To change the default editor run:
$ sudo update-alternatives --config editor
There is also a GTK program for doing this named galternatives.
Install galternatives:
$ sudo apt-get install galternatives

The apt package comes with its own cron script “/etc/cron.daily/apt” to support the automatic download of packages. This script can be enhanced to perform the automatic upgrade of packages by installing the unattended-upgrades package. These can be customized by parameters in “/etc/apt/apt.conf.d/02backup” and “/etc/apt/apt.conf.d/50unattended-upgrades” as described in “/usr/share/doc/unattended-upgrades/README”.
The unattended-upgrades package is mainly intended for the security upgrade for the stable system. If the risk of breaking an existing stable system by the automatic upgrade is smaller than that of the system broken by the intruder using its security hole which has been closed by the security update, you should consider using this automatic upgrade with configuration parameters as the following.
APT::Periodic::Update-Package-Lists "1"; APT::Periodic::Download-Upgradeable-Packages "1"; APT::Periodic::Unattended-Upgrade "1";
If you are running an unstable system, you do not want to use the automatic upgrade since it certainly breaks system some day. Even for such unstable case, you may still want to download packages in advance to save time for the interactive upgrade with configuration parameters as the following.
APT::Periodic::Update-Package-Lists "1"; APT::Periodic::Download-Upgradeable-Packages "1"; APT::Periodic::Unattended-Upgrade "0";
You can easily change your vim color scheme by :colorscheme, but it can be hard to compare several schemes to decide what’s best for you. But the Vim Color Sampler Pack can help you to choose which color scheme is best for you. This package was put together simply to save others time in downloading the color schemes.
This package is simply to help people who want to try out a lot of color schemes. It is the top 100 rated color schemes on vim.sf.net as of Jan 20th, 2010 that are are not “evil” (binding keys, changing fonts, etc) — zipped up in a single package. Every single theme was updated to its newest revision, and converted to unix formatted line endings.
Check out these screenshots before installing the color sampler pack.
Installing Vim Color Sampler Pack
Download the Color Sampler Pack From here. Simply unzip, and place the files in ~/.vim/plugin and ~/.vim/colors — it will unzip with correct dir structure, so you can just unzip to ~/.vim
The pack comes with an organized GUI menu, but no tool for console users. Console users can download the ScrollColor.vim plugin to walk through installed color schemes. Drop ScrollColors.vim into your plugin directory. Type :SCROLL and use arrow keys to walk through color schemes.
You can map two keys of your choice to NextColor and PrevColor actions. Choose pair of shortcut keys (for example
map <silent><F3> :NEXTCOLOR<cr> map <silent><F2> :PREVCOLOR<cr>
Put the above maps in your ~/.vimrc
Happy coloring



(4 votes, average: 3.50 out of 5)















Twitter
Facebook
plurk
Recent Comments