自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Google Go語言實(shí)現(xiàn)http共享(帶trace)

開發(fā) 開發(fā)工具
我們今天要講到用Go語言實(shí)現(xiàn)http文件共享,這個(gè)版本的程序比python的實(shí)現(xiàn)快了點(diǎn),默認(rèn)情況下支持的客戶端多了些。

 我之前有篇文章(http://www.cnblogs.com/MikeZhang/archive/2012/03/13/httpShareGolang20120312.html)中提到過用Go語言實(shí)現(xiàn)http文件共享,這個(gè)版本的程序比python的實(shí)現(xiàn)快了點(diǎn),默認(rèn)情況下支持的客戶端多了些,但是沒有客戶訪問的trace,程序運(yùn)行過程中,感覺像是死掉了。我想改進(jìn)下,讓它有trace。

代碼如下:

  1. /* 
  2. File      : httpShareWithTrace.go 
  3. Author    : Mike 
  4. E-Mail    : Mike_Zhang@live.com 
  5. */ 
  6. package main 
  7. import( 
  8.     "fmt" 
  9.     "net/http" 
  10.     "io/ioutil" 
  11.     "log" 
  12.     "time" 
  13.     "os" 
  14.     "strings" 
  15.  
  16. func getFilelist(path string) string { 
  17.         m_files,err  :=  ioutil.ReadDir(path) 
  18.         if err !=nil{ 
  19.         //     println( "Get filelist error !" ) 
  20.                 return "" 
  21.         } 
  22.         var strRet string 
  23.         for _,f :range m_files  { 
  24.                 //    println(f.Name(),f.IsDir()) 
  25.                 if path == "./" { 
  26.                         strRet += "<p><a href=\""+path+""+f.Name() +" \">" + f.Name() + "</a></p>
  27.                 }else{ 
  28.                         strRet += "<p><a href=\""+path[1:]+"/"+f.Name() +" \">" + f.Name() + "</a></p>
  29.                 } 
  30.         } 
  31.         return strRet 
  32.  
  33. func Handler( w http.ResponseWriter,r *http.Request ){ 
  34.         println("Request ",r.URL.Path," from ",r.RemoteAddr) 
  35.         //   path :r.URL.Path[1:] 
  36.         path :"." + r.URL.Path 
  37.         if path == "./favicon.ico" {http.NotFound(w,r);return} 
  38.         if path == "./" ||  getFilelist(path) != "" {fmt.Fprintf( w,"%s",getFilelist(path));return} 
  39.         fin,err :os.Open(path) 
  40.         defer fin.Close() 
  41.         if err != nil {fmt.Fprintf( w,"404 : Not found" );return} 
  42.         readLen :1024 * 1024 
  43.         buf :make([]byte,readLen) 
  44.         startPos :0 
  45.         println("Transfer file ",path," ... ") 
  46.         for { 
  47.                 n,err :fin.ReadAt(buf,int64(startPos)) 
  48.                 fmt.Fprintf(w,"%s",buf[:n]) 
  49.                 if 0 == n || err != nil {break} 
  50.                 startPos += readLen 
  51.         } 
  52. func main(){ 
  53.         port :"8080"  //Default port  
  54.         if len(os.Args)>1 { port = strings.Join(os.Args[1:2],"")} 
  55.         http.HandleFunc( "/",Handler) 
  56.         s := &http.Server{ 
  57.                 Addr:           ":"+port, 
  58.                 ReadTimeout:    1 * time.Hour,  
  59.                 WriteTimeout:   1 * time.Hour, 
  60.                 MaxHeaderBytes: (1 << 31) - 1 , //Max file size is 2048M 
  61.         } 
  62.         println("Listening on port ",port,"...") 
  63.         log.Fatal(s.ListenAndServe()) 

運(yùn)行效果如下:

1、啟動(dòng)http文件共享

2、web訪問

3、后臺(tái)trace

說明:最大支持2G文件的下載,限時(shí)為1個(gè)小時(shí),這里沒有用充分使用http協(xié)議,直接用文件io做的。時(shí)間有限,這里暫時(shí)達(dá)到了預(yù)期功能,夠局域網(wǎng)使用,這個(gè)等以后有時(shí)間了做進(jìn)一步的優(yōu)化。

原文鏈接:http://www.cnblogs.com/MikeZhang/archive/2012/08/06/httpShareGolang20120805.html

【編輯推薦】

  1. Google Go語言發(fā)布兩周年 不斷改進(jìn)中
  2. Google Go:新興語言的代表
  3. 1月編程榜發(fā)布:Google Go意外奪得年度編程語言
  4. Google Go有啥用?以及何謂好的系統(tǒng)編程語言
  5. Google Go語言的快樂編程因素

責(zé)任編輯:彭凡 來源: 博客園
相關(guān)推薦

2012-03-13 10:40:58

Google Go

2020-08-12 08:56:30

代碼凱撒密碼函數(shù)

2022-11-01 18:29:25

Go語言排序算法

2023-05-08 07:55:05

快速排序Go 語言

2024-08-29 13:23:04

WindowsGo語言

2022-05-19 14:14:26

go語言限流算法

2021-07-12 15:50:55

Go 語言netstat命令

2024-06-06 09:47:56

2022-04-18 10:01:07

Go 語言漢諾塔游戲

2023-03-27 00:20:48

2023-07-31 08:01:13

二叉搜索測(cè)試

2021-07-26 09:47:38

Go語言C++

2012-11-08 09:36:10

Google Go

2015-12-21 14:56:12

Go語言Http網(wǎng)絡(luò)協(xié)議

2021-03-01 18:35:18

Go語言虛擬機(jī)

2021-03-01 21:59:25

編程語言GoCX

2014-12-26 09:52:08

Go

2022-07-20 09:52:44

Go語言短信驗(yàn)證碼

2024-08-26 14:32:43

2024-04-26 09:04:13

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)