2014年7月30日 星期三

Flask return csv from sql

from flask import Response, send_file
    try:
        from cStringIO import StringIO                                                         
    except ImportError:
        from StringIO import StringIO

    output = StringIO()

    # 查詢
    results = Users.query.all()

    # 寫入 csv file
    for user in results:
    output.write("%s,%s,%s\n" % (user.id, user.name, user.email))

    # 輸出
    filename = address.csv

    response = Response(output.getvalue(), mimetype='text/csv')
    response.headers['Content-Disposition'] = 'attachment; filename=' + filename

    output.seek(0)
    return send_file(output, as_attachment=True, attachment_filename=filename)




References :
flask note - 我的Java生活 - ITeye技术网站
Returning MySQL query data in a csv file with Flask

GPG Key 簽署流程


撤銷 GPG key

妥善保護您密匙固然最重要,但您也要為萬一不幸的事作預備。一般的做法是您要事前先產生一個撤銷憑證 (Revoke Certificate) ,當您的 GnuPG 密匙不幸被盜去或遺失了後,您就可以用這撤銷憑證 去宣告原先的 GnuPG 鑰匙無效。 



# 產生撤銷憑證
$ gpg --output ~/myrevoke.asc --gen-revoke your_user_id

# 撤銷 GPG key
$ gpg --import ~/myrevoke.asc

$ gpg --keyserver pgp.mit.edu --send-keys your_user_id 




References :
產生 GnuPG 撤銷憑證 (Revoke Certificate) - FlossDoc
Revoking a GPG Key Pair | Carthik Sharma

GPG key 過期處理

$ gpg --list-keys
pub 4096R/ABCD1234 2013-09-26 [expired: 2013-12-31]

$ gpg --edit-key ABCD1234

gpg> list

pub 4096R/ABCD1234 created: 2013-09-26 expired: 2013-12-31 usage: SC
trust: unknown validity: expired
sub 4096R/EFGH5678 created: 2013-09-26 expired: never usage: E

gpg> key 0    # 只選擇 primary key,取消選擇 sub key

gpg> expire
Changing expiration time for the primary key.
Please specify how long the key should be valid.
0 = key does not expire
= key expires in n days
w = key expires in n weeks
m = key expires in n months
y = key expires in n years
Key is valid for? (0) 2014-12-31    # 可輸入到期日期
Key expires at Wed Dec 31 00:00:00 2014 CST
Is this correct? (y/N) y

gpg> save

$ gpg --keyserver keys.gnupg.net --send-keys ABCD1234




References :
How to change the expiration date of a GPG key | G-Loaded Journal

2014年7月25日 星期五

Sqlalchemy delete in subquery

InvalidRequestError: Could not evaluate current criteria in Python. Specify 'fetch' or False for the synchronize_session parameter.


sl = DBSession.query(Puesto.id).filter(Puesto.locales_id == id).subquery() DBSession.query(Servicio).filter(Servicio.puestos_id.in_(sl)) \ .delete(synchronize_session='fetch')




References :
python - Sqlalchemy delete subquery - Stack Overflow

2014年7月16日 星期三

ldap.OPERATIONS_ERROR: {'info': '000004DC: LdapErr: DSID-0C090724, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v23f0', 'desc': 'Operations error'}

ldap.OPERATIONS_ERROR: {'info': '000004DC: LdapErr: DSID-0C090724, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v23f0', 'desc': 'Operations error'}

 
import ldap
connect = ldap.open('127.0.0.1')
connect.set_option(ldap.OPT_REFERRALS, 0)connect.bind_s(USERNAME, PASSWORD)
result = connect.search_s(BASE_DN, ldap.SCOPE_SUBTREE, SEARCH_FILTER)
print result


References :
active directory - Python-ldap not able to bind successfully - Stack Overflow