Be lazy - implment your internal tools with python

From time to time you need to write small program for internal use. The program can be a client that check something , an internal tool for the developers, a small program that need to analysis an internal data ...

You can do it in your " main programming language " , but in case that your language is Java or C# (Or similar ) than you need to write a lot of code , download and add reference, compile , class-path ...
Too much headache even for small internal program.

These are the cases that scripting languages like python , ruby, groovy can help you.
Python is a very simple and powerful language , which has plenty of module that expand it. You can find great module for almost any purpose. You can find Facebook, Twitter,Google API written in Python.
You can find great Networking and Image processing module ... The main power of Python is the huge amount of available libraries. Unforgettably python doesn't shipped with package manager (In contrast to Ruby), so we need to download one of the python package manager available out there. We can use easy_install or PIP (there are more) for this purpose. If you are Linux citizens your life is simple (on this point). Once you install PIP than download package for python is very easy  - pip install ...

For windows citizens  download and use in python 3rd party libraries is more complicated and will require from you to spent more time and difficulty ,mainly because some that some of the packages required c compiler.
You can follow this link that will help you to install easy_install python package manager for windows or you can visit the following to check if Christoph Gohlke provide a binary distribution for the package you are looking for.

The official one is good enough, so this isn't the target here .My goal is to show some simple and powerful things that you can do in python.

Sniff Network
Few lines and you have sniffer in python.You can add filter to the sniffer or sniff only limit amount of packets.


def addPacket(packet)
    src = packet.sprintf("%IP.src%")
    dst = packet.sprintf("%IP.dst%")
    sport = packet.sprintf("")
    dport = packet.sprintf("%IP.dport%")
    raw = packet.sprintf("%Raw.load%")

Pcap Analysis 
You can dump the network traffic by tcpdump or wireshark and than easly analysis the Netwrok traffic with pyton.

import dpkt
from dpkt.ip import IP
from dpkt.tcp import TCP

for ts, raw_pkt in pcap.pcap(file_path):
    ip = IP(raw_pkt[14:])
    if(type(ip) != IP):
    tcp =
    if(type(tcp) != TCP):

Tcp client server
    import socket
    HOST = ''    # The remote host
    PORT = 80              # The same port as used by the server
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    s.send('GET / HTTP/1.1\r\nHost:\r\n\r\n')
    data = s.recv(1024)
    print 'Received', repr(data

Working with DB
The following demonstarte the easly of working with DB. You can see the executemany command that get a enumerator as a second parameter. You can easily create enumerator on a file or any other input.

con = sqlite3.connect(":memory:")
cur = con.cursor()
cur.execute("create table characters(c)")
cur.executemany("insert into characters(c) values (?)", char_generator())

Working With File
You can easily read all lines , read them directly to formatted structured , filter lines or iterate line by line when dealing with big files.
with open("log.txt") as infile:
      for line in infile: 

Easily expose web service
import web

urls = (
    '/(.*)', 'hello'
app = web.application(urls, globals())

class hello:        
    def GET(self, name):
        if not name: 
            name = 'world'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":

תגובה 1:

  1. Good ponints and examples. Instead last example I prefer to use Flask ( for the same purposes. Also take a look at the Flask installation section - to find how to configure easy_install in simple way on Windows(link you sent is broken). And if you continue to develop in Python - virtualenv is your friend.taras