2016年7月26日 星期二

python 使用 smtplib 透過 gmail 寄信

首先要將 google 帳號切換為
低安全性應用程式,設定網址:
https://www.google.com/settings/security/lesssecureapps
選擇開啟

程式碼範例:
import smtplib
from email.mime.text import MIMEText

DEFAULT_SMTP = "smtp.gmail.com:587"
DEFAULT_ACCOUNT = "xxxxx@gmail.com"
DEFAULT_PASSWORD = "******"

#帳號密碼
strSmtp = DEFAULT_SMTP
strAccount = DEFAULT_ACCOUNT
strPassword = DEFAULT_PASSWORD

#郵件內容
msg = MIMEText(strMsg)
msg["Subject"] = strSubject
msg["From"] = strFrom
msg["To"] = strTo

#傳送
server = smtplib.SMTP(strSmtp)
server.ehlo()
server.starttls()
server.login(strAccount, strPassword)
server.sendmail(strAccount, lstStrTarget, msg.as_string())
server.quit()

2016年7月21日 星期四

[python] 去除 list 裡重覆的資料

source_list = [1,2,3,4,5,6,1,2,3,4]
new_list = list(set(source_list))
print(new_list)

[1,2,3,4,5,6]

2016年7月16日 星期六

Ubuntu 下讓 mysql 綁定在 0.0.0.0 讓非 localhost 的電腦也能連接

修改 /etc/mysql/my.cnf
bind-address = 0.0.0.0

重啟 mysql
sudo service mysql restart

以 root 登入 mysql
mysql -u root -p

建立兩個同名的使用者 接受 localhost 及 % 連線
CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'user_pwd';
CREATE USER 'user_name'@'%' IDENTIFIED BY 'user_pwd';

授權操作權限至 db
GRANT ALL ON db_name.* TO 'user_name'@'localhost'; 
GRANT ALL ON db_name.* TO 'user_name'@'%';

執行
FLUSH PRIVILEGES;

離開
EXIT;

2016年7月10日 星期日

python url encode/decode

環境是 python 2.7 下:

import urllib

#測試初始 unicode 字串物件
strUnicode = u"test data"

#字串型態為 UNICODE
type(strUnicode)
<type 'unicode'>

#開始 url encode,先將 unicode 編碼為 utf-8,再 url encode (quote),得到 utf-8 字串物件
strResult = urllib.quote(strUnicode.encode("utf-8"))
type(srtResult)
<type 'str'>

#開始 url decode,將上面的結果進行 url decode (unquote),再解碼回 unicode 字串物件
 strResult = urllib.unquote(urllib.quote(u_str.encode("utf-8"))).decode("utf-8")
type(srtResult)
<type 'unicode'>

python 之禪,中英文合圖


blogger 文章加入含有 <> 的文字而不會打亂排版

編寫文章時右方的選項打開
撰寫模式選擇 按照字面顯示HTML
即可:



測試
<html>
    <p>我是一個 html</p>
</html>

2016年7月9日 星期六

ubuntu 下改 mysql 編碼


查詢目前資料庫編碼
mysql> status;
...
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8

設定並重啟
$ sudo vi /etc/mysql/conf.d/charset.cnf

[client]
default-character-set=utf8

[mysqld]
character-set-server=utf8
collation-server=utf8_unicode_ci

$ sudo service mysql restart

再查詢一次
mysql> status;
...
Server characterset: utf8
Db characterset: utf8
Client characterset: utf8
Conn. characterset: utf8

ubuntu 下查看 TCP port 被哪個程式使用

  • sudo netstat -tulpn

2016年7月8日 星期五

deploy django + mysql + ubuntu + apache

sudo apt-get update 
sudo apt-get upgrade

安裝 apache
sudo apt-get install apache2

安裝 Django less than 1.9
sudo apt-get install python3-pip
sudo python3 -m pip install "Django<1.9"

安裝 apache 模組 - wsgi
sudo apt-get install libapache2-mod-wsgi-py3

安裝 mysql 過程會設定 mysql root 密碼
sudo apt-get install mysql-server libapache2-mod-auth-mysql

安裝 mysql 官方 python-connector
到 mysql 下載 mysql-connector-python-py3_2.1.3-1ubuntu14.04_all.deb
sudo dpkg -i mysql-connector-python-py3_2.1.3-1ubuntu14.04_all.deb

測試登入 mysql
mysql -u root -p

mysql 設定檔 /etc/mysql/conf.d
修改資料庫編碼 文章連結

新增資料庫
mysql>create datebase db_name;

顯示所有資料庫
mysql>show datebases;

mysql 新增使用者與設定權限
以下為 mysql shell 內使用的命令
use mysql;
INSERT INTO user(host,user,password) VALUES('localhost','user_name',password('user_pwd'));
FLUSH PRIVILEGES;
GRANT ALL ON db_name.* TO 'user_name'@localhost IDENTIFIED BY 'user_pwd';
FLUSH PRIVILEGES;
以上為 mysql shell 內使用的命令

新增 django 專案
django-admin startproject my_proj

同步 django ORM model 到 mysql TABLE
cd my_proj
python3 manage.py makemigrations
python3 manage.py migrate

/etc/apache2/sites-available 下新增 my_proj.conf
<VirtualHost *:80>
 
    WSGIScriptAlias / /home/ubuntu/git_code/my_proj/my_proj/wsgi.py
    <Directory /home/ubuntu/git_code/my_proj/my_proj>
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    Alias /static /home/ubuntu/git_code/my_proj/static
    <Directory /home/ubuntu/git_code/my_proj/static>
        Require all granted
    </Directory>
 
    WSGIDaemonProcess my_proj python-path=/home/ubuntu/git_code/my_proj:/usr/local/lib/python3.4/dist-packages
    WSGIProcessGroup my_proj
 
</VirtualHost>

套用 site 設定
sudo a2ensite my_proj.conf
sudo service apache2 reload

重啟 apache
sudo service apache2 restart

amazon web services 修改主機名稱

在 AWS 申請主機登入後,
bash 的提示符號很醜,ex:

ubuntu@ip-123-456-789-012:~

只要修改 hostname 就可以改為較順眼的顯示方式

sudo hostname bennu.com

重新登入後就變成

ubuntu@bennu.com:~

而如果需要更客製化的提示符
就要去修改 ~/.bashrc 的 PS1 變數了