PythonでURLエンコード

urlibのquote/unquoteを利用する。

$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> text=u"今日の天気は晴れです。"
>>> print text
今日の天気は晴れです。
>>> print urllib.quote(text.encode('utf-8'))
%E4%BB%8A%E6%97%A5%E3%81%AE%E5%A4%A9%E6%B0%97%E3%81%AF%E6%99%B4%E3%82%8C%E3%81%A7%E3%81%99%E3%80%82
>>> print urllib.quote(text.encode('euc-jp'))
%BA%A3%C6%FC%A4%CE%C5%B7%B5%A4%A4%CF%C0%B2%A4%EC%A4%C7%A4%B9%A1%A3
>>> print urllib.quote(text.encode('shift_jis'))
%8D%A1%93%FA%82%CC%93V%8BC%82%CD%90%B0%82%EA%82%C5%82%B7%81B
>>> print urllib.unquote('%E4%BB%8A%E6%97%A5%E3%81%AE%E5%A4%A9%E6%B0%97%E3%81%AF%E6%99%B4%E3%82%8C%E3%81%A7%E3%81%99%E3%80%82')
今日の天気は晴れです。

elasticsearch-pyを試す

ElasticsearchのPython用クライアントであるelasticsearch-pyを試してみる。
pipが利用できる環境であれば、

$ sudo pip install elasticsearch

とすれば利用できるようになる。
あとは、以下のような感じでes.pyとか適当に作って、

# -*- coding: utf-8 -*-
from datetime import datetime
from elasticsearch import Elasticsearch
es = Elasticsearch("localhost:9200")
# データの登録
res1 = es.index(index="sample", doc_type="data",
                  body={"msg":"Hello", "timestamp": datetime.now()}, id="1")
print(res1)
# 検索
res2 = es.search(index="sample", doc_type="data",
                 body={"query":{"match_all":{}}})
print(res2)
# インデックスの削除
res3 = es.indices.delete(index="sample")
print(res3)

これを実行すれば、アクセスすることができます。
結構、簡単に使えますね。