Category: python

Average, variance & standard deviation in Python

Here is a quick python script which calculates average, variance and standard deviation. This is also part of codeacademy work. I must say last time I worked with variance and standard deviation it was more than 10 years ago in statistics course. Hence a bit of reminder here for me too: (Some are from wikipedia
Read More »

a simple battleship python script

I have written a simple python game script to sink a battleship. It is very simple as it was almost a year ago last time when I wrote a long python script. Thanks to codeacademy that it helps to brush up my python skills. I think it is also good to put the script here
Read More »

Fragmented IP packet forwarding

I couldn’t really find a suitable topic for this post actually but I will try to find answers for the following questions: How can we fragment an IP packet manually in scapy How does a fragmented packet look like and how the transport layer (TCP/UDP) header is located How do we forward fragmented packets, do
Read More »

Port Scanner in Python

Python is a great tool to do some socket operations. I have written a piece of code by which I can scan a port range. It is very basic and missing bunch of checks as aim is the simplicity here. #!/usr/bin/python import socket,sys try: sys.argv[3] except: print "Usage: [hostname|IP] [port_start] [port_end]" sys.exit() host =
Read More »

How to sort a python dictionary by value

If you want to search a dictionary by value in python, here is a handy one line using also lambda: mydict = {'ver1':'2002','ver2':'2000','ver3':'2020'} dict_v_srt=sorted(mydict.items(),key=lambda sort_ver: sort_ver[1]) print dict_v_srt If you run this script output will be ; [('ver2', '2000'), ('ver1', '2002'), ('ver3', '2020')]

searching dictionary by value in python

The following list comprehension is a life saver for me to search a dictionary by value. Searching dictionary by value may not seem to be sensible as you may get multiple values but in case needed, it is a wonderful one line of code. list_values = [ key for key,val in mydict.items() if val==value ]
Read More »

How to turn a list into string

Here is a quote from describing how we can use the join method. str.join(iterable) Return a string which is the concatenation of the strings in the iterable iterable. The separator between elements is the string providing this method. As it can be seen it isn’t only for strings but for something called “iterable”. Again
Read More »

Python list comprehensions

I don’t know if you have used list comprehension in Python. It is a great feature. You can go through a list and do some small changes with very little effort. Here is an example; For example you have a list of user names and you would like to shorten these user names to 8
Read More »

How to preserve quoted strings in python split

If you want to analyse for example apache log files and split the lines by space by using the usual “split” method, you will see that split doesn’t respect quoted strings. For example if you have a line like below; – – [06/Mar/2012:10:02:22 +0100] “GET /2011/10/19/jncip-sec-exam/ HTTP/1.1” 200 3331 “-” “mm” You can’t get
Read More »