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

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

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

代码如下:

/* 
File      : httpShareWithTrace.go 
Author    : Mike 
E-Mail    : Mike_Zhang@live.com 
*/ 
package main 
import( 
    "fmt" 
    "net/http" 
    "io/ioutil" 
    "log" 
    "time" 
    "os" 
    "strings" 

 
func getFilelist(path string) string { 
        m_files,err  :=  ioutil.ReadDir(path) 
        if err !=nil{ 
        //     println( "Get filelist error !" ) 
                return "" 
        } 
        var strRet string 
        for _,f :range m_files  { 
                //    println(f.Name(),f.IsDir()) 
                if path == "./" { 
                        strRet += "<p><a href=\""+path+""+f.Name() +" \">" + f.Name() + "</a></p>
                }else{ 
                        strRet += "<p><a href=\""+path[1:]+"/"+f.Name() +" \">" + f.Name() + "</a></p>
                } 
        } 
        return strRet 

 
func Handler( w http.ResponseWriter,r *http.Request ){ 
        println("Request ",r.URL.Path," from ",r.RemoteAddr) 
        //   path :r.URL.Path[1:] 
        path :"." + r.URL.Path 
        if path == "./favicon.ico" {http.NotFound(w,r);return} 
        if path == "./" ||  getFilelist(path) != "" {fmt.Fprintf( w,"%s",getFilelist(path));return} 
        fin,err :os.Open(path) 
        defer fin.Close() 
        if err != nil {fmt.Fprintf( w,"404 : Not found" );return} 
        readLen :1024 * 1024 
        buf :make([]byte,readLen) 
        startPos :0 
        println("Transfer file ",path," ... ") 
        for { 
                n,err :fin.ReadAt(buf,int64(startPos)) 
                fmt.Fprintf(w,"%s",buf[:n]) 
                if 0 == n || err != nil {break} 
                startPos += readLen 
        } 

func main(){ 
        port :"8080"  //Default port  
        if len(os.Args)>1 { port = strings.Join(os.Args[1:2],"")} 
        http.HandleFunc( "/",Handler) 
        s := &http.Server{ 
                Addr:           ":"+port, 
                ReadTimeout:    1 * time.Hour,  
                WriteTimeout:   1 * time.Hour, 
                MaxHeaderBytes: (1 << 31) - 1 , //Max file size is 2048M 
        } 
        println("Listening on port ",port,"...") 
        log.Fatal(s.ListenAndServe()) 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.

运行效果如下:

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

2022-11-01 18:29:25

Go语言排序算法

2020-08-12 08:56:30

代码凯撒密码函数

2024-08-29 13:23:04

WindowsGo语言

2023-05-08 07:55:05

快速排序Go 语言

2022-05-19 14:14:26

go语言限流算法

2021-07-12 15:50:55

Go 语言netstat命令

2024-06-06 09:47:56

2023-07-31 08:01:13

二叉搜索测试

2021-07-26 09:47:38

Go语言C++

2022-04-18 10:01:07

Go 语言汉诺塔游戏

2023-03-27 00:20:48

2015-12-21 14:56:12

Go语言Http网络协议

2012-11-08 09:36:10

Google Go

2014-12-26 09:52:08

Go

2022-07-20 09:52:44

Go语言短信验证码

2024-08-26 14:32:43

2021-03-01 18:35:18

Go语言虚拟机

2021-03-01 21:59:25

编程语言GoCX

2011-01-05 10:58:05

Google Go
点赞
收藏

51CTO技术栈公众号