跳轉至內容

newLISP 簡介 / 網際網路

來自 Wikibooks,開放世界中的開放書籍

網際網路

[編輯 | 編輯原始碼]

HTTP 和網路

[編輯 | 編輯原始碼]

大多數網路任務都可以使用 newLISP 的網路功能完成

  • base64-dec 將字串從 BASE64 格式解碼
  • base64-enc 將字串編碼為 BASE64 格式
  • delete-url 刪除 URL
  • get-url 從網路讀取檔案或頁面
  • net-accept 接受新的傳入連線
  • net-close 關閉套接字連線
  • net-connect 連線到遠端主機
  • net-error 返回最後一次錯誤
  • net-eval 在多個遠端 newLISP 伺服器上評估表示式
  • net-interface 定義預設網路介面
  • net-listen 監聽對本地套接字的連線
  • net-local 連線的本地 IP 和埠號
  • net-lookup IP 編號的名稱
  • net-peek 準備好讀取的字元數
  • net-peer net-connect 的遠端 IP 和埠
  • net-ping 向一個或多個地址傳送 ping 包(ICMP 回聲請求)
  • net-receive 在套接字連線上讀取資料
  • net-receive-from 在開啟的連線上讀取 UDP 資料報
  • net-receive-udp 在開啟的連線上讀取 UDP 資料報並關閉連線
  • net-select 檢查套接字或套接字列表的狀態
  • net-send 在套接字連線上傳送資料
  • net-send-to 在開啟的連線上傳送 UDP 資料報
  • net-send-udp 傳送 UDP 資料報並關閉連線
  • net-service 將服務名稱轉換為埠號
  • net-sessions 返回當前開啟的連線列表
  • post-url 將資訊釋出到 URL 地址
  • put-url 將頁面上傳到 URL 地址。
  • xml-error 返回最後一次 XML 解析錯誤
  • xml-parse 解析 XML 文件
  • xml-type-tags 顯示或修改 XML 型別標籤

使用這些網路功能,您可以構建各種網路應用程式。使用諸如 net-eval 之類的功能,您可以將 newLISP 作為守護程序啟動在遠端計算機上,然後在本地計算機上使用它將 newLISP 程式碼傳送到網路進行評估。

訪問網頁

[編輯 | 編輯原始碼]

這是一個使用 get-url 的非常簡單的示例。給定網頁的 URL,獲取原始碼,然後使用 replace 及其列表構建功能來生成該頁面上所有 JPEG 影像的列表

(set 'the-source (get-url "http://www.apple.com"))
(replace {src="(http\S*?jpg)"} the-source (push $1 images-list -1) 0)
(println images-list)
("http://images.apple.com/home/2006/images/ipodhifititle20060228.jpg"
"http://images.apple.com/home/2006/images/ipodhifitag20060228.jpg"
"http://images.apple.com/home/2006/images/macminiwings20060228.jpg" 
"http://images.apple.com/home/2006/images/macminicallouts20060228.jpg" 
"http://images.apple.com/home/2006/images/ipodhifititle20060228.jpg" 
"http://images.apple.com/home/2006/images/ipodhifitag20060228.jpg")

一個簡單的 HTML 表單

[編輯 | 編輯原始碼]

最簡單的搜尋表單可能類似於以下內容。

(load "cgi.lsp")
(println (string "Content-type: text/html\r\n\r\n"
    [text]<!doctype html>
    <html>
     <head>
     <title>Title</title>
     </head>
    <body>
 [/text]))

(set 'search-string (CGI:get "userinput"))

(println (format [text]
        <form name="form" class="dialog" method="GET">
             <fieldset>
                 <input type="text" value="search" name="userinput" >
                 <input type="submit" style="display:none"/>
             </fieldset>
        </form>[/text]))

(unless (nil? search-string)
    (println " I couldn't be bothered to search for \"" search-string "\""))

(println [text]
      </body>
    </html>
 [/text])

一個簡單的 IRC 客戶端

[編輯 | 編輯原始碼]

以下程式碼實現了一個簡單的 IRC(Internet Relay Chat)客戶端,它展示瞭如何使用基本網路功能。指令碼使用給定的使用者名稱登入到伺服器,並加入 #newlisp 頻道。然後,指令碼分為兩個執行緒:第一個執行緒在連續迴圈中顯示任何頻道活動,而第二個執行緒在控制檯中等待輸入。兩個執行緒之間唯一的通訊是透過共享的 connected 標誌進行的。

(set 'server (net-connect "irc.freenode.net" 6667)) 
(net-send server "USER newlispnewb 0 * :XXXXXXX\r\n") 
(net-send server "NICK newlispnewb \r\n") 
(net-send server "JOIN #newlisp\r\n") 

(until (find "366" buffer)
  (net-receive server buffer 8192 "\n")
  (print buffer))

(set 'connected (share))
(share connected true)

(fork
    (while (share connected)
      (cond
        ((net-select server "read" 1000) ; read the latest
            (net-receive server buffer 8192 "\n")
            ; ANSI colouring: output in yellow then switch back
            (print "\n\027[0;33m" buffer "\027[0;0m"))
        ((regex {^PING :(.*)\r\n} buffer) ; play ping-pong
            (net-send server (append "PONG :" (string $1 ) "\r\n"))
            (sleep 5000))
        ((net-error) ; error
            (println "\n\027[0;34m" "UH-OH: " (net-error) "\027[0;0m")
            (share connected nil)))
     (sleep 1000)))
 
(while (share connected) 
   (sleep 1000)
   (set 'message (read-line))
   (cond
     ((starts-with message "/")  ; a command?
          (net-send server (append (rest message) "\r\n"))
          (if 
            (net-select server "read" 1000)
            (begin
                (net-receive server buffer 8192 "\n") 
                (print "\n\027[0;35m" buffer "\027[0;0m"))))
     ((starts-with message "quit") ; quit
            (share connected nil))
     (true  ; send input as message
          (net-send server (append "PRIVMSG #newlisp :" message "\r\n")))))

(println "finished; closing server")
(close server)
(exit)
華夏公益教科書