Count IP Address Appears Most in Log File

1.Problem

之前做运维的笔试题,有一道很经典的统计日志文件中出现最多的 IP 地址及次数的题。其日志格式为

122.224.6.43 - - [30/Oct/2012:21:29:42 +0800] "GET /phpMyAdmin/scripts/setup.php HTTP/1.1" 404 162 "-" "ZmEu"
122.224.6.43 - - [30/Oct/2012:21:29:42 +0800] "GET /MyAdmin/scripts/setup.php HTTP/1.1" 404 162 "-" "ZmEu"
122.224.6.43 - - [30/Oct/2012:21:29:42 +0800] "GET /pma/scripts/setup.php HTTP/1.1" 404 162 "-" "ZmEu"
122.224.6.43 - - [30/Oct/2012:21:29:42 +0800] "GET /myadmin/scripts/setup.php HTTP/1.1" 404 162 "-" "ZmEu"
58.251.60.248 - - [30/Oct/2012:23:05:46 +0800] "GET http://www.baidu.com/ HTTP/1.1" 200 612 "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 551; .NET CLR 2.0.50727; CIBA)"
58.251.60.248 - - [30/Oct/2012:23:06:37 +0800] "" 400 0 "-" "-"

这是 nginx 日志 /var/log/nginx/access.log 中的一个片段。

Read More

Python Virtualenv Wrapper and Pip

1.Install

Arch 下默认是 python3, 而如果强制用软链接改为 python2 的话,一些程序会有问题的,之前都是小脚本,所以手动把第一行改为 #!/usr/bin/env python2 或者 #!/usr/bin/python2 就行,现在要做 GAE 发现手动改了好多还是不行,于是便有了 Python virtualenv 等。

Install virtualenv virtualenvwrapper and pip

# pacman -S python2-virtualenv
# pacman -S python2-virtualenvwrapper
# pacman -S python2-pip

Read More

OpenCV Basis

1.Load an image

In [1]: import cv

In [2]: im1 = cv.LoadImageM('foo.jpg')

In [3]: print type(im1)
<type 'cv2.cv.cvmat'>

In [4]: im2 = cv.LoadImage('foo.jpg')

In [5]: print type(im2)
<type 'cv2.cv.iplimage'>

In [6]: cv.SaveImage('foo1.png', im1)

In [7]: cv.SaveImage('foo2.png', im2)

可以看到LoadImageM得到的是一个cvmat图像,而LoadImage得到的是一个iplimage图像,两者均可以输出为图像

Read More

Install OpenEXR for Python

Using OpenEXR’s Python bindings we can make a simple image viewer.
So, we should install OpenEXR first, following OpenEXR bindings for Python
Make sure you have already installed OpenEXR’s Prerequisite: Python 2.5+ and the OpenEXR C++ library(openexr).

wget http://excamera.com/files/OpenEXR-1.2.0.tar.gz

tar zxvf OpenEXR-1.2.0.tar.gz

cd OpenEXR-1.2.0

(sudo python2 setup.py build)

sudo python2 setup.py install

Then, we can follow OpenCV and OpenEXR

References: Python how to install setup.py

Wonderful Octopress

Today, I install Octopress, it is wonderful.
I follow the official docs
setup
deploy
config
blogging
and the other two blogs:
Octopress介绍与安装
使用Octopress将博客从wordpress迁移到GitHub上

The following command is in common use(in the project root directory):

rake new_post["title"]
rake new_page[super-awesome]
rake new_page[super-awesome/page.html]

rake preview
rake generate
rake deploy

git add .
git commit -m 'your message'
git push origin source

Read More