python 3.4 下執行出現的 exception 為:
ImportError: cannot import name '_win32stdio'
原因是 twisted 套件本身的問題,只要到:
https://github.com/twisted/twisted/tree/trunk/src/twisted/internet
下載兩個原始碼 _win32stdio.py 和 _pollingfile.py
放到 python34 下的 C:\Python34\Lib\site-packages\twisted\internet
並安裝 pypiwin32
python3 -m pip install pypiwin32
即可順利執行
2017年1月13日 星期五
2017年1月10日 星期二
mongodb 設定 root 帳號密碼
首先一般啟動 db
C:\"Program Files"\MongoDB\Server\3.2\bin\mongod.exe --dbpath "C:\Program Files\MongoDB\Server\3.2\data\db" --bind_ip 0.0.0.0
啟動 mongo shell
C:\"Program Files"\MongoDB\Server\3.2\bin\mongo.exe
在 shell 輸入以下
use admin
db.createUser({user:"root_username", pwd:"root_password", roles:[{role:"root", db:"admin"}]});
關掉 db 再以 --auth 重新啟動 db
C:\"Program Files"\MongoDB\Server\3.2\bin\mongod.exe --auth --dbpath "C:\Program Files\MongoDB\Server\3.2\data\db" --bind_ip 0.0.0.0
之後連線都需要有 root_username 及 root_password
mongodb://root_username:root_password@127.0.0.1/
C:\"Program Files"\MongoDB\Server\3.2\bin\mongod.exe --dbpath "C:\Program Files\MongoDB\Server\3.2\data\db" --bind_ip 0.0.0.0
啟動 mongo shell
C:\"Program Files"\MongoDB\Server\3.2\bin\mongo.exe
在 shell 輸入以下
use admin
db.createUser({user:"root_username", pwd:"root_password", roles:[{role:"root", db:"admin"}]});
關掉 db 再以 --auth 重新啟動 db
C:\"Program Files"\MongoDB\Server\3.2\bin\mongod.exe --auth --dbpath "C:\Program Files\MongoDB\Server\3.2\data\db" --bind_ip 0.0.0.0
之後連線都需要有 root_username 及 root_password
mongodb://root_username:root_password@127.0.0.1/
2016年12月13日 星期二
vmware workstation player 12 guest 3D 設定
假設建立了一個 guest 叫做 windows_7 必須在
該 windows_7 vm 目錄下 windows_7.vmx 加入以下兩行:
mks.enableD3DRenderer = TRUE
mks.enableDX11Renderer = FALSE
2016年12月10日 星期六
使用 complete 呼叫的 jquery ajax polling
(function poll(){
$.ajax({
url: "/poll",
success: function (data) {
do_something_with(data);
},
dataType: "json",
complete: poll,
timeout: 30000
});
})();
2016年12月7日 星期三
進階 jquery polling 即使 request 失敗也不會停止
function doPoll(){
$.post('ajax/test.html').done(function(data) {
/* process */
}).always(function() {
setTimeout(doPoll, 5000);
});
}
$.post('ajax/test.html').done(function(data) {
/* process */
}).always(function() {
setTimeout(doPoll, 5000);
});
}
jquery 簡單 polling 範例
function doPoll()
{
$.post('ajax/test.html', function(data) {
alert(data); // process results here
setTimeout(doPoll,5000);
});
}
2016年12月3日 星期六
如何禁止Vmware同步時間
參考:http://trufflepenne.blogspot.tw/2014/02/vmware.html
如果你有時必須調整VM裡的時間,但重開VM後又恢復現在時間,請利用以下步驟來修改(以windows 2008 R2為例):
停止VMWare tools, Windows Time服務
進入你的VM作業系統,選服務,把VMWare tools, Windows Time停止,並改為手動
取消Internet同步時間
進入你的控制台找到日期時間,進入後將以下取消打勾
修改VMWare configuration檔 (vmx, 跟VM檔案同一目錄)
a. shutdown VM
b. 切到你的VM目錄
c. 打開xxx.vmx
d. 找到tools.syncTime = "XXX" 標籤,刪掉之
e. 在最結尾填入以下
tools.syncTime = "FALSE"
time.synchronize.continue = FALSE
time.synchronize.restore = FALSE
time.synchronize.resume.disk = FALSE
time.synchronize.shrink = FALSE
time.synchronize.tools.startup = FALSE
time.synchronize.resume.host = FALSE
重新進入VM,修改時間,重開機就會看到新設定時間沒被同步
如果要讓VM重開時,都定在固定時間
在xxx.vmx最後加上
rtc.startTime = 1335862800
後面數字是timesatmp, 上面是2012 09:00:00 ,可用以下計算
http://www.onlineconversion.com/unix_time.htm
2016年10月17日 星期一
mysql 與 django 設定 index 欄位注意事項
範例:
strOriginUrl = models.CharField(db_index=True, max_length=255, null=False)
注意:
1. 欄位需使用 CharField 型態
2. 最大長度為 255
3. 指定 db_index=True
2016年10月13日 星期四
[selenium] 讓 chrome 啟動時最大化視窗
chromeDriverExeFilePath = "path-to-executable/chromedriver.exe"
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path=chromeDriverExeFilePath, chrome_options=options)
options = webdriver.ChromeOptions()
options.add_argument("--start-maximized")
driver = webdriver.Chrome(executable_path=chromeDriverExeFilePath, chrome_options=options)
2016年10月3日 星期一
windows 系統下的網頁另存新檔 html 與 _files 的連動關閉
在 windows 下,預設 xxx.html 與 xxx_files 是連動的,
刪除或移動 xxx.html xxx_files 也會跟著刪除或移動
如果需要關閉這一層連動關係,必需修改註冊表:
HKEY_CURRENT_USER - Software - Microsoft - Windows - CurrentVersion - Explorer
建一個新的 DWORD 值
名稱:NoFileFolderConnection
數值: 1
完成後,不需重新開機等動作即可生效。
2016年8月31日 星期三
python3 selenium 移動滑鼠到 element 範例 (hover)
使用 selenium 時,有點要被 click 的元件,可能要先將滑鼠 hover 到某個元件上才會顯示出來
from selenium.webdriver.common.action_chains import ActionChains
可以這樣寫:
假設我們要切換幣別為 USD 但該網站設計,必需先移動到 #currency 元件上 #usd 選項才會出現。
from selenium.webdriver.common.action_chains import ActionChains
eleCurrency = self.driver.find_element_by_css_selector("#currency")
eleUsd = self.driver.find_element_by_css_selector("#usd")
actHoverThenClick = ActionChains(seleniumDriverInstance)
actHoverThenClick.move_to_element(eleCurrency).move_to_element(eleUsd).click().perform()
eleUsd = self.driver.find_element_by_css_selector("#usd")
actHoverThenClick = ActionChains(seleniumDriverInstance)
actHoverThenClick.move_to_element(eleCurrency).move_to_element(eleUsd).click().perform()
2016年8月13日 星期六
python3 在 ubuntu 下安裝 lxml 模組
sudo apt-get install libxml2-dev
sudo apt-get install libxslt1-dev
python3 -m pip install lxml
python3 -m pip install lxml
python3 zipfile 解壓縮 zip 檔
from zipfile import ZipFile
with ZipFile("z.zip", "r") as zipFile:
zipFile.extract("file_path_in_zip.xml", "folder_path_to_extract")
with ZipFile("z.zip", "r") as zipFile:
zipFile.extract("file_path_in_zip.xml", "folder_path_to_extract")
2016年7月26日 星期二
python 使用 smtplib 透過 gmail 寄信
首先要將 google 帳號切換為
低安全性應用程式,設定網址:
https://www.google.com/settings/security/lesssecureapps
低安全性應用程式,設定網址:
https://www.google.com/settings/security/lesssecureapps
選擇開啟
DEFAULT_ACCOUNT = "xxxxx@gmail.com"
程式碼範例:
import smtplib
from email.mime.text import MIMEText
DEFAULT_SMTP = "smtp.gmail.com:587"
from email.mime.text import MIMEText
DEFAULT_SMTP = "smtp.gmail.com:587"
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';
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'>
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'>
訂閱:
文章 (Atom)

