Google Go语言实现http共享(带trace)

开发 开发工具
我们今天要讲到用Go语言实现http文件共享,这个版本的程序比python的实现快了点,默认情况下支持的客户端多了些。

 我之前有篇文章(http://www.cnblogs.com/MikeZhang/archive/2012/03/13/httpShareGolang20120312.html)中提到过用Go语言实现http文件共享,这个版本的程序比python的实现快了点,默认情况下支持的客户端多了些,但是没有客户访问的trace,程序运行过程中,感觉像是死掉了。我想改进下,让它有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()) 

运行效果如下:

1、启动http文件共享

2、web访问

3、后台trace

说明:最大支持2G文件的下载,限时为1个小时,这里没有用充分使用http协议,直接用文件io做的。时间有限,这里暂时达到了预期功能,够局域网使用,这个等以后有时间了做进一步的优化。

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

【编辑推荐】

  1. Google Go语言发布两周年 不断改进中
  2. Google Go:新兴语言的代表
  3. 1月编程榜发布:Google Go意外夺得年度编程语言
  4. Google Go有啥用?以及何谓好的系统编程语言
  5. Google Go语言的快乐编程因素

责任编辑:彭凡 来源: 博客园
相关推荐

2012-03-13 10:40:58

Google Go

2020-08-12 08:56:30

代码凯撒密码函数

2022-11-01 18:29:25

Go语言排序算法

2023-05-08 07:55:05

快速排序Go 语言

2022-05-19 14:14:26

go语言限流算法

2021-07-12 15:50:55

Go 语言netstat命令

2023-03-27 00:20:48

2022-04-18 10:01:07

Go 语言汉诺塔游戏

2023-07-31 08:01:13

二叉搜索测试

2021-07-26 09:47:38

Go语言C++

2015-12-21 14:56:12

Go语言Http网络协议

2021-03-01 18:35:18

Go语言虚拟机

2021-03-01 21:59:25

编程语言GoCX

2012-11-08 09:36:10

Google Go

2014-12-26 09:52:08

Go

2022-07-20 09:52:44

Go语言短信验证码

2011-01-05 10:58:05

Google Go

2023-01-30 08:16:39

Go语言Map

2022-09-05 08:07:25

goreplay监控工具

2022-02-21 18:16:38

Go语言枚举
点赞
收藏

51CTO技术栈公众号