2016年4月3日 星期日

NAT 與 穿越防火牆技術


參考來源:http://www.cs.nccu.edu.tw/~lien/Writing/NGN/firewall.htm

關於Network Address Translation (NAT):
  • Why NAT? 解決IPv4地址短缺的方案
  • What is NAT? IP封包通過路由器或防火牆時重寫源IP地址或目的IP地址的技術。
  • How many NAT types? 
    • Full cone NAT
    • Address-Restricted cone NAT
    • Port-Restrict cone NAT
    • Symmetric NAT
Full cone NAT,亦即著名的一對一(one-to-one)NAT
  • 一旦一個內部地址(iAddr:port1)映射到外部地址(eAddr:port2),所有發自iAddr:port1的包都經由eAddr:port2向外發送。任意外部主機都能通過給eAddr:port2發包到達iAddr:port1
Full Cone NAT.svg
Address-Restricted cone NAT
  • 一旦一個內部地址(iAddr:port1)映射到外部地址(eAddr:port2),所有發自iAddr:port1的包都經由eAddr:port2向外發送。任意外部主機(hostAddr:any)都能通過給eAddr:port2發包到達iAddr:port1的前提是:iAddr:port1之前發送過包到hostAddr:any. "any"也就是說埠不受限制
Restricted Cone NAT.svg
Port-Restricted cone NAT
類似受限制錐形NAT(Restricted cone NAT),但是還有埠限制。
  • 一旦一個內部地址(iAddr:port1)映射到外部地址(eAddr:port2),所有發自iAddr:port1的包都經由eAddr:port2向外發送。一個外部主機(hostAddr:port3)能夠發包到達iAddr:port1的前提是:iAddr:port1之前發送過包到hostAddr:port3.
Port Restricted Cone NAT.svg
Symmetric NAT(對稱NAT)
  • 每一個來自相同內部IP與埠,到一個特定目的地地址和埠的請求,都映射到一個獨特的外部IP位址和埠。
    同一內部IP與埠發到不同的目的地和埠的信息包,都使用不同的映射
  • 只有曾經收到過內部主機封包的外部主機,才能夠把封包發回
Symmetric NAT.svg

常見穿越防火牆/NAT的相關技術:
  • UPnP(Universal Plug and Play)
  • STUN(Simple Traversal of UDP Through Network Address Translators)-RFC 3489
  • TURN(Traversal Using Relay NAT)
  • ALG(Application Layer Gateway)
  • ICE(Interactive Connectivity Establish)
UPnP缺點 :NAT必須支援UPnP協定
STUN缺點:Symmetric NAT無法穿透
TURN缺點 : TURN server需要承受連線頻寬
ALG缺點:基於網路安全,網管人員將不會接受用戶的應用程式控制他們的NAT


相關開源工具:
pystun, 提供查詢外部IP位置及NAT型別:https://github.com/jtriley/pystun
pjnath, Open Source ICE, STUN, and TURN Library: http://www.pjsip.org/pjnath/docs/html/

2016年3月20日 星期日

研究如何突破Koding VM always on限制

Koding是什麼?
提供開發者雲端開發環境的服務商,提供註冊用戶免費使用一個虛擬機並提供基本設定檔案與環境。如果有需要使用免費的 VM練習使用網頁,可以前往https://koding.com/註冊帳號,註冊完畢後就可以擁有免費的虛擬機器提供用戶練習程式。但美中不足的部分是免費版本的帳號每30分鐘會關閉虛擬機器,付費版本才會提供Keep VM always on的功能。

略過註冊的部分,此處提供登入Koding後畫面:


點擊觀看左側koding-vm-0相關設定,General標籤裡面出現Keep VM always on,但免費用戶無法使用:


官方建議免費版本用戶透過登入koding網站讓VM機器保持開啟狀態,於是開始思考如何突破限制。本次研究嘗試網頁自動化工具selenium,撰寫程式讓電腦每30分鐘內自動化登入,藉由此法成功保持VM持續開機!!本次採用的方案使用selenium進行網頁自動化登入koding帳號之後啟用VM

python範例程式碼:
https://github.com/stayhigh/koding-vm-active-selenium/blob/master/koding_login_active_vm.py

使用selenium網頁自動化的功能提到兩種重要的等待功能(wait)之間的差異:
explicit wait (wait for certain conditions, less than the specified time seconds)
implicit wait (DOM polling, waiting for elements)
官方網站也特別說明請勿混用,會造成等待時間增加。
http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp


本次範例程式碼當中採用的自動化工具是selenium,但還有其他自動化工具可以參考:

xdotool: 應用shell script相當方便,可用於模擬鍵盤與滑鼠行為
關於python的鍵盤與滑鼠自動化的模組羅列如下:

 - pyautogui: python的模組,主要提供控制鍵盤與滑鼠的GUI自動化功能,
pyautogui安裝方法:
On Windows, there are no other modules to install.
On OS X run sudo pip3 install pyobjc-framework-Quartz, sudo pip3 install pyobjc-core, and then sudo pip3 install pyobjc.
On Linux, run sudo pip3 install python3-xlib, sudo apt-get install scrot, sudo apt-get install python3-tk, and sudo apt-get install python3-dev. (Scrot is a screenshot program that PyAutoGUI uses.)
pyautogui詳情參考:https://automatetheboringstuff.com/chapter18/

關於其他python自動化工具請參考:http://schurpf.com/python-automation/
- SendKeysCtypes
- PYHK
- win32gui
- pywinauto
- mouse

HTML的script 標籤三個屬性說明! 請愛用 async

參考來源:http://peter.sh/experiments/asynchronous-and-deferred-javascript-execution-explained/

Asynchronous and deferred JavaScript execution explained


The HTML <script> element allows you to define when the JavaScript code in your page should start executing. The “async” and “defer” attributes were added to WebKit early September. Firefox has been supporting them quite a while already. Does your browser support the attributes?

- Normal execution <script>
- This is the default behavior of the <script> element. Parsing of the HTML code pauses while the script is executing. For slow servers and heavy scripts this means that displaying the webpage will be delayed.

- Deferred execution <script defer>
- Simply put: delaying script execution until the HTML parser has finished. A positive effect of this attribute is that the DOM will be available for your script. However, since not every browser supports defer yet, don’t rely on it!

- Asynchronous execution <script async>
- Don’t care when the script will be available? Asynchronous is the best of both worlds: HTML parsing may continue and the script will be executed as soon as it’s ready. I’d recommend this for scripts such as Google Analytics.


根據上面英文原文重點整理,重述script標籤的三種屬性:
- <script>
- <script defer>
- <script async>

上圖當中出現三個名詞:parser|net|execution
- net 代表下載該javascript檔案的時段
- execution 代表執行該javascript檔案的時段
- parser代表http client 解析的過程的時段

可以看到async的屬性相當在設計網頁場景當中相當實用,parser解析時同時將下載該javascript檔案。請愛用async!

使用selenium自動化登入github網頁

主要使用到以下工具完成自動化登入github網頁:
selenium:網頁自動化工具,請參考 http://www.seleniumhq.org/
python 的getpass模組:提供使用者輸入密碼功能

範例程式碼已放置於stayhigh的github空間:
https://github.com/stayhigh/github-login-selenium/blob/master/github_login.py

程式碼用途:
場景為redhero0702@gmail.com作為用戶名稱登入github網站,如有需要可將
github_account = "redhero0702@gmail.com"
改成您的github用戶名即可,假設你的用戶名稱為your_github_account@gmail.com
github_account = "your_github_account@gmail.com"


2016年3月14日 星期一

Python Scrapy 快速攻略與教學


# 安裝google chrome plugin,SelectorGadget快速取得selector與xpath等相關資訊,網頁爬蟲特別實用
# 安裝firefox plugin,sqlite manager觀看資料庫內容:http://www.minwt.com/website/server/4964.html

#開啟apple新的scrapy專案
stayhigh@stayhighnet:/Users/stayhigh/projects/apple  $  scrapy startproject apple

#執行apple的scrapy專案
stayhigh@stayhighnet:/Users/stayhigh/projects/apple  $  scrapy crawl apple

#執行apple的scrap專案,並且將輸出a.json的json格式檔案
stayhigh@stayhighnet:/Users/stayhigh/projects/apple  $  scrapy crawl apple -o a.json -t json

#執行分段爬蟲任務並放置相關資料於job1目錄
stayhigh@stayhighnet:/Users/stayhigh/projects/apple  $  scrapy crawl apple -s JOBDIR=job1

#觀看apple專案目錄結構
- crawler.py為使用者自行定義的爬蟲程式,藉由繼承scrapy.Spider類別進行網頁抓取
- items.py 用於定義資料欄位
- pipeline.py 用於定義爬蟲程式的控制流程
- settings.py 設定檔,用於設定啟用的功能,如常見的pipeline功能,並切記設定時指定pipeline.py當中的apple.pipelines.ApplePipeline

ITEM_PIPELINES {
    'apple.pipelines.ApplePipeline'300,
}

stayhigh@stayhighnet:/Users/stayhigh/projects/apple  $ tree
.
├── a.json
├── apple
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── items.py
│   ├── items.pyc
│   ├── pipelines.py
│   ├── settings.py
│   ├── settings.pyc
│   └── spiders
│       ├── __init__.py
│       ├── __init__.pyc
│       ├── crawler.py
│       └── crawler.pyc
└── scrapy.cfg

#如何實現多網頁爬取功能
from scrapy.spiders import CrawlSpider

# crawler.py內的爬蟲類別繼承CrawlSpider
class AppleCrawler(CrawlSpider):



2014年2月22日 星期六

寫C語言需要注意的型別問題 portablility


參考來源:http://blog.urdada.net/2008/04/18/85/

不同位元(32 bits, 64 bits)的作業系統下面的型別大小不一定相同


1.《Bypass the 2GB file size limit on 32-bit Linux => 64-bit 的系統下,long 的長度也是各自表述的!
2.int 的大小即使到了 64-bit 的機器上,大部分的系統仍然使用 4 bytes 的大小而已,這主要是為了避免程式從 32-bit 系統轉換到 64-bit 系統需要修改太多地方
3.參考 Wikipedia: 64-bit data models 的說明
絕大多數的 UNIX 系統在 64-bit 下面採用 LP64 這種 data model,此時 long 就不再是固定為 4 bytes 大小,而是變成 8 bytes 的大小了!
然而,Win64 卻不是使用 LP64,而是採用 LLP64 這個 data model,這時候 long 的大小仍然還是 4 bytes

Many 64-bit compilers today use the LP64 model (including Solaris, AIX, HP, Linux, Mac OS X, and IBM z/OS native compilers). Microsoft's VC++ compiler uses the LLP64 model.
兩種 data model 的最大差異點就是 long 這個資料型態的大小,LP64 是 64-bit,而 LLP64 則是 32-bit
LLP64 data model 基本上可以說跟 32-bit 的系統一樣,唯一差別只有位址(pointer)改成了 64-bit 而已。資料物件(class, structure) 等如果沒有包含 pointer 的成員的話,整個物件的大小是與 32-bit 系統一樣的!
而 LP64 則是除了位址(pointer)改成 64-bit 之外,long 的大小也變成了 64-bit 大小。所以在 UNIX 下面,要把 32-bit 程式 porting 到 64-bit 可能要比 Windows 多花費多一點功夫。
兩個型別問題影響著程式的相容性:
  1. 在 UNIX 下面,long 的大小在 32-bit 與 64-bit 的系統下是不一樣的
  2. 同樣是 64-bit 系統,UNIX 與 Windows 對於 long 的大小看法是不一致的
為了使程式在 32-bit 與 64-bit 之間以及 UNIX 與 Windows 之間的相容性提昇,改用固定長度的資料型態是寫程式的一個好習慣
在 UNIX 下面,我們可以改用 stdint.h 這個 header file 中對於資料型態的定義:
int8_t     8-bit signed interger
int16_t    16-bit signed interger
int32_t    32-bit signed interger
int64_t    64-bit signed interger
uint8_t    8-bit unsigned interger
uint16_t   16-bit unsigned interger
uint32_t   32-bit unsigned interger
uint64_t   64-bit unsigned interger
在 Windows 下面,則改用下面這些整數固定大小的資料型態
INT8       8-bit signed integer
INT16      16-bit signed integer
INT32      32-bit signed integer
INT64      64-bit signed integer
UINT8      8-bit unsigned integer
UINT16     16-bit unsigned integer
UINT32     32-bit unsigned integer
UINT64     64-bit unsigned integer
絕對不要再使用 int 和 long !請愛用stdint.h內的型別
尤其是寫網路程式時,作業系統與作業系統的位元數所造成的型別相容性問題
參考資料:
  1. Wikipedia: 64-bit data models
  2. 64-Bit Programming Models: Why LP64?
  3. Introduction to Win32/Win64
  4. Porting 32-bit Applications to the Itanium® Architecture
  5. Preparing Code for the IA-64 Architecture (PDF)

2013年7月27日 星期六

python自然語言教學 nltk課程

參考資料:http://ccl.pku.edu.cn/alcourse/nlp/

課程名稱:自然語言處理導論課程討論區 ( 最新貼:2010-6-16 9:21:04 )
任課教師:詹衛東 *  劉揚王厚峰常寶寶*** 北京大學中文系** 北京大學信息科學技術學院
電子郵件:zwd@pku.edu.cn (詹卫东)liuyang@pku.edu.cn(劉揚)
辦公電話:6276581062765835-205(分機)
有關本課程的任何問題和建議,都歡迎與我們聯繫
2011-2012學年第二學期上課時間:2012年2月13日~6月8日( 5~6節地點:教206考試時間
教學參考資料
史蒂芬鳥,伊万·克萊因和愛德華·洛珀。2009年與Python自然語言處理。O'Reilly Media出版。
克里斯托弗D.萬寧和辛里奇SCHUTZE的的。1999年統計自然語言處理的基礎。麻省理工學院出版社。
丹尼爾Jurafsky和詹姆斯·馬丁。2000年語音和語言處理。培生教育。
課程進度安排                 

 
序號 內容提要 講義參考資料
第1週
2012年2月13日
課程概述:課程安排, 參考文獻說明, 等等.
緒論:什麼是自然語言處理?
課程安排 问答系统:ElizaIBM Watson,……
機器翻譯系統:GoogleWorldLingo,……
自然語言處理的支撐科學是什麼?(Author:Shuly Wintner)
漫話人工智能 (顧森)
 
第2週
2012年2月20日
理論基礎:
中文文本的自動分詞
第02章漢語自動分詞研究述評
 
第3週
2012.2.27
理論基礎:
詞性標註方法
Chapter_03 
 
楊孝華二卷
 
第4週
2012年3月5日
理論基礎:
漢語的句法結構分析(上)
Chapter_04(I)
簡單句法分析方法示例
(自底向上,自頂向下,左角分析法)
 
歐萊的分析算法
第5週
2012.3.12
理論基礎:
漢語的句法結構分析(下)
chapter_04(II)
 
句法結構歧義的程度
第6週
2012.3.19
理論基礎:
語義分析
 
Chapter_05 
第7週2012.3.26
 
理論基礎:
語篇分析(王厚峰)
Chapter_06
 
第8週
2012.4.2
討論課(第一次大作業)
作業要求:
 
 
  • 根據選課人數情況,採用分組報告形式在課堂上進行交流。
  • 所有選課同學均需提交書面報告。
  • 報告文件名 ​​請採用統一格式:
     學號_姓名_文章名.doc/pdf
  • 可以合作完成,但人數不得超過3人。合作完成的報告,要詳細註明各人的分工情況。
  • 作業電子版(word或pdf文件)發至 zwd@pku.edu.cn
  • 請在2012.4.16(含)之前提交作業。如果需要延期提交,請給出理由。但不應晚於4.23日提交。晚於4.23日提交的作業將罰分。
第9週
2012年4月9日
工程實踐:
Python及NLTK包的應用—— 訪問語言資源
教材下載:NLP與Python
Chapter_07
要求:熟悉教材第1章第1、2、3、4節;熟悉教材第2章第2、3節,了解第1、4節。
蟒蛇-2.5.4
第10週
2012年4月16日
工程實踐:
Python及NLTK包的應用—— 文本處理基礎
Chapter_08
要求:熟悉教材第3章第1、2、8、9節,了解第3、4、5、6、7節。
第11週
2012年4月23日
工程實踐:
Python及NLTK包的應用—— 程序設計進階
Chapter_09
要求:熟悉教材第4章1、2、3、4節,了解第5、6節。
第12週
2012.4.30
工程實踐:
Python及NLTK包的應用—— 分詞和詞性標註
Chapter_10
要求:熟悉教材第5章第1、2、3節,了解第4、5節。
第13週
2012.5.7
工程實踐:
Python及NLTK包的應用—— 句法分析實現
Chapter_11
要求:了解教材第8章第1、2、3、4節。[特別說明,期末考試第8章第1、2、3、4節不作要求]
第14週
2012年5月14日
工程實踐:
Python及NLTK包的應用—— 信息抽取
Chapter_12
要求:熟悉教材第7章第1、2、3、4、5、6節。
第15週
2012.5.21
工程實踐:
Python及NLTK包的應用—— 文本分類
Chapter_13
要求:熟悉教材第6章第1、2、3節,了解第4、5、6節。[特別說明,期末考試第6章第4、5、6節不作要求]
第16週
2012年5月28日
機器翻譯(常寶寶)
  
第17週
2012年6月4日
 
討論課 (第二次大作業)
漢語自動分詞與詞性標註
 
 
  • 根據選課人數情況,採用分組報告形式在課堂上進行交流。
  • 所有選課同學均需提交書面報告。
  • 可以合作完成,但人數不得超過3人。合作完成的報告,要詳細註明各人的分工情況。
  • 在6月11日(個別情況需要延期,須給出理由,但不遲於6月25日)前,請將所有程序源碼、數據文件及實驗報告(限pdf格式)打包壓縮為“學號_姓名.rar”,將其作為附件發送至liuyang@pku.edu.cn(我收到後會有回复,注意確認)。
第18週
2012.6.11 
考試