このブログは高島法律事務所の弁護士 野田隼人(滋賀弁護士会)が主に弁護士その他法律関係者向けに色々なことを書いてます。

当事務所へのご相談・ご依頼をご検討中の方は,高島法律事務所の事務所ホームページをご参照下さい。

Djangoを使えるようにするまでの設定メモです。

申込→30分でメールが来る→teratermを使いrootでパスワードログイン。

鍵認証を設定

~/.ssh ディレクトリへ移動し,以下のコマンド
ssh-keygen -t rsa
Enter キー→パスフレーズ入力→パスフレーズ再入力
これで,公開鍵 (id_rsa.pub) と秘密鍵 (id_rsa) が作成される。
公開鍵(id_rsa.pub)を
cat id_rsa.pub >> authorized_keys
で認証鍵として追加。
viで秘密鍵を表示させてコピペしてローカルに保存。これを使って認証する。
vi /etc/ssh/sshd_config
で,
PasswordAuthentication no
UsePAM no

iptablesを設定
http://sakura.off-soft.net/blog/sakuravps.html

fastestmirrorをインストール
yum -y install yum-fastestmirror
当初からインストールされるようになったっぽい。2011/03/09

インストールされているパッケージをまとめてアップデート
yum -y update

yup-updatesの代わりにyum-cronを入れる。
yum -y install yum-cron

rootのメールアドレスを設定

http://blog.mobf.net/archives/147

不要なサービスを停止

http://blog.mobf.net/archives/147

Apacheのインストール

http://sakuratan.biz/archives/2492

Pythonのインストール
yum install python*

PHPのインストール
yum install php*

mysqlのインストール
yum install mysql

でもって,mysqlを起動

mod_pythonのインストール
yum -y install mod_python

/home/ユーザ名/tmpにDjangoを落とす。
wget http://www.djangoproject.com/download/1.2.3/tarball/

落としたファイルを解凍
tar zxvf (ファイル名)

ここまでやったところで眠くなったので,今夜は終了。

2010/10/04 続き

Djangoをinstall
 $ cd Django-1.1.2
 $ sudo python setup.py install

Djangoを起動
$ django-admin.py startproject testproject
$ cd testproject
$ python manage.py runserver IPアドレス:ポート

と,まあ,ここまでで動く。
あ,
easy_install mysql-python
したな。
mysql入れた後くらいに。

「10日で覚えるPython入門教室」に取り組む日曜日の午後。

第3日1時限目で停滞。
原因はAmazonAPIの仕様変更。
従来のアクセス方法では,APIを呼ぶことができず,従前のパラメータにSignatureとTimestampを付す必要がある。

ざっと検索してみたけれど,躓きの石状態で直接の解決策未発見。
仕方ないので,本に載っているコードを書き直し。

#! /usr/bin/env python
# coding:utf-8

import base64
import hashlib
import hmac
import urllib

from datetime import datetime
from xml.etree.ElementTree import ElementTree

# Amazon Product Advertising API Version.
# see http://developer.amazonwebservices.com/connect/kbcategory.jspa?categoryID=17
api_version = "2009-11-01"

def main(url):
    node_uri = "{http://webservices.amazon.com/AWSECommerceService/" + api_version + "}"

    xmlfile = urllib.urlopen(url)
    tree = ElementTree(file = xmlfile)
    root = tree.getroot()

    for node in root.getchildren():
        if node.tag == node_uri + "Items":
            for subnode in node.getchildren():
                if subnode.tag == node_uri +"TotalResults":
                    print subnode.text
                elif subnode.tag == node_uri +"Item":
                    for item in subnode:
                        if item.tag == node_uri +"ASIN":
                            print item.text

if __name__ == "__main__":
    amazon_api_url = "http://ecs.amazonaws.jp/onca/xml?"

    #Your Amazon API secret key.
    SecretKey = "★★★"

    # initialize Dictionary
    params = {}

    params["Service"] = "AWSECommerceService"
    params["Operation"] = "ItemSearch"
    params["Version"] = api_version
    params["AWSAccessKeyId"] = "★★★" #YOUR AWS Access Key Id
    params["SearchIndex"] = "Books"
    params["Keywords"] = "Python"

    #Amazon Product Advertising API requires a timestamp and a signature
    #see http://docs.amazonwebservices.com/AWSECommerceService/latest/DG/index.html?rest-signature.html
    #see http://www.python.jp/doc/2.5/lib/module-datetime.html
    params["Timestamp"] = datetime.utcnow().isoformat()

    #creating the string to sign
    string = string_base = "GET\necs.amazonaws.jp\n/onca/xml\n"
    for k,v in sorted(params.items()):
        if string <> string_base:
            string = string + '&'
        string = string + k + '=' + urllib.quote(v)

    #calculate HMAC with the SHA256 hash with using Secret Key
    string = hmac.new(SecretKey, string, hashlib.sha256).digest()
    signature = base64.b64encode(string)

    url = amazon_api_url
    for k,v in sorted(params.items()):
        url = url + k + '=' + v + '&'
    url = url + 'Signature=' + urllib.quote(signature)

    main(url)

URLの作り方など,もっと簡単にできそうだけれども,Amazonの仕様説明に忠実な処理で作成してみました。

<同じ問題で困ったと思われる方々>
Amazon API仕様変更してて使えない(-_-)『10日でおぼえるPython入門教室』第3日 メモ
Python再び・・・・ハッシュを辞書と読んでるんですねぇー
TwitterAPI
Pythonのお勉強~三日目(Web API編①)
http://twitter.com/t2enonu/status/5621601942

翔泳社さん,ここだけでも早めに改定した方が良いですよ。
良い本なのですから。

twitter→はてブで100人中同じ誕生日のいる人の確率は100%に近いらしいにたどり着きました。
沢山の人数集めて実証してしまうというのは,おもしろい発想だと思ったのですが,Pythonの再学習中なのでそのまま計算。

計算式は同じ誕生日のいる人の確率のまま。
考え方のレベルとしては中学校レベルだけれども,コンピュータなしに計算しようとは思わない桁数。

p = 1.0
for i in range(0,100):
    p = p * (365-i) / 365
    print i+1, 1-p

百人まで試行した結果は次の通り。

1 0.0
2 0.0027397260274
3 0.00820416588478
4 0.0163559124666
5 0.0271355736998
6 0.0404624836491
7 0.056235703096
8 0.0743352923517
9 0.0946238338892
10 0.116948177711
11 0.141141378322
12 0.167024788838
13 0.194410275232
14 0.223102512005
15 0.252901319764
16 0.283604005253
17 0.315007665297
18 0.346911417872
19 0.379118526032
20 0.411438383581
21 0.443688335165
22 0.475695307663
23 0.507297234324
24 0.538344257915
25 0.568699703969
26 0.598240820136
27 0.626859282263
28 0.654461472342
29 0.680968537478
30 0.706316242719
31 0.730454633729
32 0.75334752785
33 0.774971854176
34 0.79531686462
35 0.814383238875
36 0.83218210638
37 0.848734008216
38 0.864067821082
39 0.878219664367
40 0.891231809818
41 0.903151611482
42 0.914030471562
43 0.923922855656
44 0.932885368551
45 0.940975899466
46 0.948252843367
47 0.954774402833
48 0.960597972879
49 0.965779609323
50 0.970373579578
51 0.974431993334
52 0.978004509334
53 0.981138113484
54 0.983876962759
55 0.986262288816
56 0.988332354885
57 0.990122459341
58 0.991664979389
59 0.992989448418
60 0.994122660865
61 0.995088798805
62 0.995909574895
63 0.996604386831
64 0.997190478967
65 0.997683107312
66 0.99809570464
67 0.998440042979
68 0.998726391254
69 0.998963666308
70 0.999159575965
71 0.999320753177
72 0.999452880641
73 0.999560805556
74 0.999648644445
75 0.999719878174
76 0.999777437453
77 0.999823779244
78 0.999860954581
79 0.999890668397
80 0.999914331949
81 0.999933108508
82 0.999947952922
83 0.99995964569
84 0.999968822149
85 0.999975997326
86 0.99998158699
87 0.999985925398
88 0.999989280166
89 0.999991864674
90 0.999993848356
91 0.9999953652
92 0.999996520725
93 0.999997397693
94 0.999998060747
95 0.999998560171
96 0.999998934921
97 0.999999215051
98 0.999999423654
99 0.999999578399
100 0.999999692751

57人目で99%に達し,100人目では99.9999%になりました。

Get Adobe Flash playerPlugin by wpburn.com wordpress themes