7.31.2011

Python: Get Google trends queries

Python: Google トレンドの取得

・GetGoogleTrends.py

# Copyright (c) 2011 Mog Project. All rights reserved.
# -*- coding: utf-8 -*-
"""Get queries of Google trends."""

import re
import urllib2

URL = 'http://www.google.co.jp/m/services/trends/get'
PTN_QUERY = '<query>(.*)</query>'

def main():
  rank = 1
  for line in urllib2.build_opener().open(URL):
    x = re.findall(PTN_QUERY, line, re.I)
    if x:
      print '%2d: %s' % (rank, unicode(x[0]))
      rank += 1

if __name__ == '__main__':
  main()

・urllib2 モジュールをインポートして、Google trends の URL をオープン。

・イテレータを使用して一行ずつ読み取り、クエリの正規表現に一致するか、
 re モジュールの findall() でチェック。

・正規表現に一致したら、後方参照の文字列がリストとして取得されている
 ので、それを unicode 変換して表示。

改良バージョンはこちら
http://mogproject.blogspot.com/2011/08/python-get-google-trends-queries-ver2.html

7.29.2011

Google trends

Google トレンド

Google trends を眺めていると非常に興味深い。

日本語:
http://www.google.co.jp/trends

英語:
http://www.google.com/trends

以下のURLでは、データをXML形式で取得できる。

日本語:
http://www.google.co.jp/m/services/trends/get

英語:
http://www.google.com/m/services/trends/get

Python で取得してこようか。

7.27.2011

How to add a persistent static route in Solaris 10

Solaris10: OS再起動後も有効なスタティックルート設定

Solaris 10 では「route -p」オプションでOS再起動後も有効なスタティックルートを設定できる。
実行すると自動的に /etc/inet/static_routes が更新されるようだ。

(例)
ネットワークアドレスに対するルーティング
# route –p add –net 192.168.3.0 –netmask 255.255.255.0 192.168.1.250

特定ホストに対するルーティング
# route –p add –host 192.168.3.1 192.168.1.251

参考:
http://goegoe-solaris.blogspot.com/2008/07/blog-post.html

7.18.2011

Get the current system time in microseconds/nanoseconds

マイクロ秒/ナノ秒単位の時刻の取得

1. [UNIX/Windows] マイクロ秒の表示 – gettimeofday() – <sys/time.h>

・print_time1.c

#include <stdio.h>
#include <sys/time.h>

int main() {
  struct timeval now;
  struct tm *t;
  gettimeofday(&now, NULL);
  t = localtime(&now.tv_sec);

  printf("%04d/%02d/%02d %02d:%02d:%02d.%06ld\n",
      t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
      t->tm_hour, t->tm_min, t->tm_sec,
      now.tv_usec);
  return 0;
}

$ ./print_time1
2011/07/18 02:50:40.333827

gettimeofday() は廃止予定らしい。

http://archive.linux.or.jp/JM/html/LDP_man-pages/man2/gettimeofday.2.html
>> POSIX.1-2008 では gettimeofday() は廃止予定とされており、代わりに clock_gettime(2) の使用が推奨されている。

 

2. [UNIX] ナノ秒の表示 – clock_gettime() – <time.h>

・print_time2.c

#include <stdio.h>
#include <time.h>

int main() {
  struct timespec now;
  struct tm *t;
  clock_gettime(CLOCK_REALTIME, &now);
  t = localtime(&now.tv_sec);

  printf("%04d/%02d/%02d %02d:%02d:%02d.%09ld\n",
      t->tm_year + 1900, t->tm_mon + 1, t->tm_mday,
      t->tm_hour, t->tm_min, t->tm_sec,
      now.tv_nsec);
  return 0;
}
$ ./print_time2
2011/07/18 02:50:43.268259848

コンパイルに失敗した場合は、「-lrt」オプションを付け、librt とリンクさせる。

 

3. [Windows] ミリ秒の表示 – GetLocalTime() – <windows.h>

・print_time3.c

#include <stdio.h>
#include <windows.h>

int main() {
  SYSTEMTIME now;
  GetLocalTime(&now);

  printf("%04d/%02d/%02d %02d:%02d:%02d.%03d\n",
      now.wYear, now.wMonth, now.wDay,
      now.wHour, now.wMinute, now.wSecond,
      now.wMilliseconds);
  return 0;
}
>.\print_time3.exe
2011/07/18 03:31:53.722

参考:
http://www.bugbearr.jp/?C%E8%A8%80%E8%AA%9E%2F%E6%97%A5%E6%99%82
http://archive.linux.or.jp/JM/html/LDP_man-pages/man2/clock_gettime.2.html

Installing gcc on Solaris 11 exp

Solaris 11 Express: gcc のインストール

# pkg publisher
発行元                             タイプ 状態   URI
solaris                  (優先)     起点   online   http://pkg.oracle.com/solaris/release/
# pkg install gcc-3
               インストールするパッケージ:     4
           ブート環境の作成: いいえ
ダウンロード                     パッケージ ファイル  転送 (MB)
Completed                                  4/4   2278/2278    33.7/33.7
 
フェーズ                             アクション
インストールフェーズ             2831/2831
 
フェーズ                                  項目
パッケージ状態の更新フェーズ         4/4
イメージ状態の更新フェーズ          2/2
# gcc -v
Reading specs from /usr/sfw/lib/gcc/i386-pc-solaris2.11/3.4.3/specs
Configured with: /builds2/sfwnv-gate/usr/src/cmd/gcc/gcc-3.4.3/configure --prefix=/usr/sfw --with-as=/usr/sfw/bin/gas --with-gnu-as --with-ld=/usr/ccs/bin/ld --without-gnu-ld --enable-languages=c,c++,f77,objc --enable-shared
Thread model: posix
gcc version 3.4.3 (csl-sol210-3_4-20050802)

参考:
http://linuxscoop.blogspot.com/2010/12/how-to-install-gcc-on-solaris-11.html

7.17.2011

Enable FTP server in Solaris 11 exp

Solaris 11 Express: FTPサーバの有効化

Solaris 10 同様、デフォルトでFTPパッケージはインストール済みだが、無効になっている。

# inetadm |grep ftp
disabled  disabled       svc:/network/ftp:default
# inetadm -l ftp
SCOPE    NAME=VALUE
         name="ftp"
         endpoint_type="stream"
         proto="tcp6"
         isrpc=FALSE
         wait=FALSE
         exec="/usr/sbin/in.ftpd -a"
         user="root"
default  bind_addr=""
default  bind_fail_max=-1
default  bind_fail_interval=-1
default  max_con_rate=-1
default  max_copies=-1
default  con_rate_offline=-1
default  failrate_cnt=40
default  failrate_interval=60
default  inherit_env=TRUE
default  tcp_trace=FALSE
default  tcp_wrappers=FALSE
default  connection_backlog=10
default  tcp_keepalive=FALSE

普通に有効化するだけでは、ffftp で接続したときにサーバ側のファイル一覧が表示されない現象が発生する。
これは、FTPサーバが返す日付に日本語が含まれていることが原因のようだ。
ftpd 起動ユーザのロケールが C 以外、かつ inherit_env のパラメータが TRUE の場合に事象が発生する。
inherit_env を FALSE に変更する。

# inetadm -m ftp inherit_env=FALSE
# inetadm -l ftp
SCOPE    NAME=VALUE
         name="ftp"
         endpoint_type="stream"
         proto="tcp6"
         isrpc=FALSE
         wait=FALSE
         exec="/usr/sbin/in.ftpd -a"
         user="root"
default  bind_addr=""
default  bind_fail_max=-1
default  bind_fail_interval=-1
default  max_con_rate=-1
default  max_copies=-1
default  con_rate_offline=-1
default  failrate_cnt=40
default  failrate_interval=60
         inherit_env=FALSE
default  tcp_trace=FALSE
default  tcp_wrappers=FALSE
default  connection_backlog=10
default  tcp_keepalive=FALSE

ftp 有効化。

# inetadm -e ftp
# inetadm |grep ftp
enabled   online         svc:/network/ftp:default

参考:
http://blog.bluegold.me/2010/02/ffftp-cannot-list-files-with-opensolaris-ftpd/
http://kb2.adobe.com/jp/cps/228/228596.html
http://www.kichise.com/modules/pico/content0064.html

Installing Oracle Solaris 11 Express pt.3

Solaris 11 Express のインストール その3

前回(http://mogproject.blogspot.com/2011/07/installing-oracle-solaris-11-express_17.html)からの続き。

・VMware Tools のインストール

image VMware Tools のメディアをセットした後、
端末から root ユーザで以下のコマンドを実行。

# cd /tmp
# gunzip –c /cdrom/vmwaretools/vmware-solaris-tools.tar.gz | tar xf -
# cd vmware-tools-distrib
# ./vmware-install.pl
image 応答は全てデフォルトのままで問題なし。
Enter キー連打。
image root から一般ユーザに戻って、
$ vmware-user
を実行。
failed to … というメッセージが出るが問題ない。
image ちなみに、root ユーザのまま「vmware-user」を実行すると、
Gtk-WARNING **: cannot open display:
が出現する。

環境変数 DISPLAY が正しく定義されていないためだろう。
image root 権限でツールボックスを起動するには、
$ sudo vmware-toolbox
とすればよい。

・ユーザ登録 ⇒ 失敗

image デスクトップ上の「Oracle Solaris の登録」を起動。
image Use My Account –
I accept the terms of use for registering Oracle programs.
にチェックを付け、「Register Now」をクリック。
image 自分のIDでサインイン。
image エラーとなってしまった。
諦めることにする。

参考:
http://osmania.blog.so-net.ne.jp/2010-11-25

Installing Oracle Solaris 11 Express pt.2 (Set static ip address)

Solaris 11 Express のインストール その2 ~固定IPアドレスの設定~

前回(http://mogproject.blogspot.com/2011/07/installing-oracle-solaris-11-express.html)からの続き。

image root でのログインは許可されていない。
image 初回セットアップ。
OK。
image ログインセッションは「gnome」でよい。
OK。
image 言語は「日本語」。
OK。
image ログイン完了。
背景が白から黒へ転移するのが美しい。
image 端末から「su -」を行い、root ユーザのパスワードを設定。

ちなみに、root に変わった後で「passwd」コマンドをオプション
なしで実行すると、root になる前のユーザのパスワード変更
処理が行われる。
root のパスワードを再設定するには、「passwd root」とする
必要がある。
image 固定IPアドレスを設定するためには、まず
nwamd - Network Auto-Magic サービスを無効にする必要がある。
以下のコマンドを実行。
svcadm disable svc:/network/physical:nwam
svcadm enable svc:/network/physical:default
image DNSを利用できるようにするため、nsswitch.conf を更新する。
既に雛形があるので、それを上書きすればよい。
cp /etc/nsswitch.dns /etc/nsswitch.conf
image システム - システム管理 – Network を選択。
image root ユーザのパスワードを入力。
image NIC を選択し、プロパティを開く。
image IPアドレスを手動設定。
「ブート時に有効にする」のチェックを付ける。
image DNS サーバを指定。
image この時点では、NICを有効にしようとするとエラーとなってしまう。
とりあえず、OS再起動してみる。
image 再起動後、無事、インターネットに接続できるようになった。
image IPアドレスの設定や確認は、Solaris 11 新登場の「ipadm」コマンド
でも行える。
IPアドレス設定の表示は、「ipadm show-addr」。
image TeraTerm でSSH接続しようとすると、このようなエラーとなる。
暗号アルゴリズムが見つからないようだ。
image SSH の設定で、「AES256-CTR(SSH2)」を
「<以下の暗号アルゴリズムは使用されません>」よりも上に
移動すればよい。

その3 へ続く。
http://mogproject.blogspot.com/2011/07/installing-oracle-solaris-11-express_3309.html

参考:
http://osmania.blog.so-net.ne.jp/2010-11-25
http://nkjmkzk.net/?p=2056
http://aikotobaha.blogspot.com/2011/04/solaris-11-expresssnv151a-ip.html
http://hondou.homedns.org/pukiwiki/index.php?OpenSolaris%20%B8%C7%C4%EAIP
http://www.kinusati.net/2009/10/14/opensolaris-2009-6-%E3%81%ABteraterm-4-63%E3%81%A7ssh%E6%8E%A5%E7%B6%9A%E3%81%A7%E3%81%8D%E3%81%AA%E3%81%84/

Installing Oracle Solaris 11 Express pt.1

Solaris 11 Express のインストール その1

VMware 上で、Solaris 11 Express をインストールしてみる。

・ISOファイルのダウンロード
 今回は、Oracle Solaris 11 Express 2010.11 LiveCD for x86 を使用。
http://www.oracle.com/technetwork/server-storage/solaris11/downloads/index.html

・仮想マシンの設定
ゲストOS: Sun Solaris 10 (64-bit)
メモリ: 540MB
CPU: 2個
HDD: 8GB キャッシュはパフォーマンス優先
メモリ: 512MB
CPU: 2コア
ドライブ: CD => sol-11-exp-201011-live-x86.iso, FDDなし, USBあり

・仮想マシン起動

・インストール手順

image デフォルト(一番上)を選択
image 起動中・・・
image 23. Japanese を選択
image 15. Japanese を選択
image いきなり、Solaris 11が起動!
フォルダの名前は英語のままのほうが便利そうなので「古い名前のままにする」を選択。
image 右クリック - 「画面の解像度」 から解像度を少し小さくしておく。
image この時点で、本物のOSが立ち上がっているのは非常に新鮮。
ホスト名は「solaris」、ユーザIDは「jack」となっている。
ログインシェルは bash のようだ。
半楕円チックなスクロールバーもユニーク。
image デスクトップにある「Oracle Solaris 入門」を
実行したところ。
VMのせいか、立ち上がりは1分くらいかかった。
「日本語」を選ぶ。
image 一応、Firefoxもちゃんと動く。研究というのは直訳っぽい。
image 動きが遅かったのは、メモリ不足が原因のようだ。

image 寄り道はこのくらいにして、
「Oracle Solaris をインストールする」を実行。
日本語のインストーラが立ち上がる。
次へ。
image とりあえず、提案されるがままの構成で。
パーティションタイプは「Solaris 2」「Extended」が選択可能。
次へ。
image ディスク容量が推奨サイズ(10GB)を下回っていた。
OK。
image 地図で東京を選ぶ。
次へ。
image 言語が「日本語」、地域が「日本」であることを確認。
なぜか時計がUTC表示になっている。が、気にしない。
次へ。
image 管理者ユーザの登録画面。
image 「root」は使えない。
「自分の名前」(空白でも可)、
「ログイン名」、「ユーザーパスワード」とその確認、
「コンピュータ名」(デフォルトはsolaris)を入力。
次へ。
image 内容の確認。
インストール。

image インストール開始。
image 20分ほどでインストール完了。
リブート。
image GRUB画面。
image 起動中。
image 5分ほどでログイン画面が表示。

その2へ続く。
http://mogproject.blogspot.com/2011/07/installing-oracle-solaris-11-express_17.html

参考:
http://osmania.blog.so-net.ne.jp/2010-11-25