go语言实现http

栏目: Go · 发布时间: 7年前

内容简介:ServerClient

Server

package main

import (
	"net/http"
	"os"
	"io"
)

func main()  {
	http.HandleFunc("/",handle)
	http.ListenAndServe("127.0.0.1:8005",nil)
}

func handle(w http.ResponseWriter,r *http.Request)  {
	url:=r.URL.String()
	fp,err:=os.Open("/Users/zmx/go/180726/src/main"+url)
	if err!=nil {
		w.Write([]byte(err.Error()))
		return
	}
	defer fp.Close()
	buf:=make([]byte,4096)
	for  {
		n,err:=fp.Read(buf)
		if err==io.EOF {
			break
		}
		w.Write(buf[:n])
	}
}

Client

package main

import (
	"net/http"
	"fmt"
	"io"
)

func main()  {
	resp,err:=http.Get("http://127.0.0.1:8005/index.go")
	if err!=nil {
		fmt.Print(err)
		return
	}
	defer resp.Body.Close()
	fmt.Println(resp.Status)
	fmt.Println(resp.StatusCode)
	fmt.Println(resp.Body)
	fmt.Println(resp.Header)
	buf:=make([]byte,4096)
	for  {
		n,err:=resp.Body.Read(buf)
		fmt.Print(string(buf[:n]))
		if err!=nil {
			if err==io.EOF {
				break
			} else {
				fmt.Print(err)
			}
		}
	}
}

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Database Design and Implementation

Database Design and Implementation

Edward Sciore / Wiley / 2008-10-24 / 1261.00 元

* Covering the traditional database system concepts from a systems perspective, this book addresses the functionality that database systems provide as well as what algorithms and design decisions will......一起来看看 《Database Design and Implementation》 这本书的介绍吧!

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具