分类 后端 下的文章

golang beego-httplib请求时302重定向获取cookie


国外服务器 抓取360搜索信息时有个302重定向的一个动作 是用来设置cookie的
不设置cookie的话 会出现stopped after 10 redirects

req := httplib.Get("https://www.so.com/s?ie=utf-8&fr=so.com&src=home_so.com&q=site:" + host)
req.Header("Accept-Language", "zh-CN,zh;q=0.9")
req.Header("Host", "www.so.com")
req.Header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36")
req.SetCheckRedirect(func(req *http.Request, via []*http.Request) error {
    return http.ErrUseLastResponse
})
response, err := req.Response()
if err != nil {
    fmt.Println(err)
    return
}
cookie := response.Header.Get("Set-Cookie")

golang为图片添加水印


//SetSY 一个图片添加水印
func SetSY(logo, tp string) {
    //原始图片是sam.jpg
    imgb, err := os.Open(tp)
    if err != nil {
        fmt.Println("打开图片失败")
        return
    }
    img, format1, err := image.Decode(imgb)
    if err != nil {
        fmt.Println("解码图片失败-", format1, err.Error())
        return
    }
    defer imgb.Close()

    wmb, err := os.Open(logo)
    if err != nil {
        fmt.Println("打开Logo失败")
        return
    }
    watermark, format2, err := image.Decode(wmb)
    if err != nil {
        fmt.Println("解码Logo失败-", format2, err.Error())
        return
    }

    defer wmb.Close()

    //把水印写到右下角,并向0坐标各偏移10个像素
    offset := image.Pt(img.Bounds().Dx()-watermark.Bounds().Dx()-10, img.Bounds().Dy()-watermark.Bounds().Dy()-10)
    b := img.Bounds()
    m := image.NewNRGBA(b)

    draw.Draw(m, b, img, image.ZP, draw.Src)
    draw.Draw(m, watermark.Bounds().Add(offset), watermark, image.ZP, draw.Over)

    //生成新图片new.jpg,并设置图片质量..
    imgw, _ := os.Create(tp)
    jpeg.Encode(imgw, m, &jpeg.Options{100})

    defer imgw.Close()

    fmt.Println("添加水印成功...")
}

golang协程限制数量



type GoLimit struct {
    ch chan int
}

func NewGoLimit(max int) *GoLimit {
    return &GoLimit{ch: make(chan int, max)}
}

func (g *GoLimit) Add() {
    g.ch <- 1
}

func (g *GoLimit) Done() {
    <-g.ch
}



g := NewGoLimit(10) //max_num(最大允许并发数)设置为10
for _, book := range 10{
    g.Add()
    go func(g *GoLimit) {
    defer g.Done() //一个并发协程已经完成
}(g)

golang 正则表达式匹配


rm := regexp.MustCompile("52bd") //正则规则匹配

将匹配的结果,替换成新输入的结果

ReplaceAllString(src, repl string) string

查找匹配的字符串

FindString(s string) string

查找匹配字符串开始和结束位置的索引

FindStringIndex(s string) (loc []int)

获取完全匹配和局部匹配的字符串到的字符串数组

FindStringSubmatch(s string) []string

获取完全匹配和局部匹配的字符串的索引数组

FindStringSubmatchIndex(s string) []int

是否匹配到

MatchString(s string) bool

获取所有匹配到的字符串,正整数用来限制匹配次数

FindAllString(s string, n int)


golnag根据ip地址的子网掩码获取最大和最小的ip地址


//根据ip地址的子网掩码获取最大ip和最小ip
func getCidrIpRange(cidr string) (string, string) {
    ip := strings.Split(cidr, "/")[0]
    ipSegs := strings.Split(ip, ".")
    maskLen, _ := strconv.Atoi(strings.Split(cidr, "/")[1])
    seg3MinIp, seg3MaxIp := getIpSeg3Range(ipSegs, maskLen)
    seg4MinIp, seg4MaxIp := getIpSeg4Range(ipSegs, maskLen)
    ipPrefix := ipSegs[0] + "." + ipSegs[1] + "."

    return ipPrefix + strconv.Itoa(seg3MinIp) + "." + strconv.Itoa(seg4MinIp),
        ipPrefix + strconv.Itoa(seg3MaxIp) + "." + strconv.Itoa(seg4MaxIp)
}

//计算得到CIDR地址范围内可拥有的主机数量
func getCidrHostNum(maskLen int) uint {
    cidrIpNum := uint(0)
    var i uint = uint(32 - maskLen - 1)
    for ; i >= 1; i-- {
        cidrIpNum += 1 << i
    }
    return cidrIpNum
}

//获取Cidr的掩码
func getCidrIpMask(maskLen int) string {
    // ^uint32(0)二进制为32个比特1,通过向左位移,得到CIDR掩码的二进制
    cidrMask := ^uint32(0) << uint(32-maskLen)
    fmt.Println(fmt.Sprintf("%b \n", cidrMask))
    //计算CIDR掩码的四个片段,将想要得到的片段移动到内存最低8位后,将其强转为8位整型,从而得到
    cidrMaskSeg1 := uint8(cidrMask >> 24)
    cidrMaskSeg2 := uint8(cidrMask >> 16)
    cidrMaskSeg3 := uint8(cidrMask >> 8)
    cidrMaskSeg4 := uint8(cidrMask & uint32(255))

    return fmt.Sprint(cidrMaskSeg1) + "." + fmt.Sprint(cidrMaskSeg2) + "." + fmt.Sprint(cidrMaskSeg3) + "." + fmt.Sprint(cidrMaskSeg4)
}

//得到第三段IP的区间(第一片段.第二片段.第三片段.第四片段)
func getIpSeg3Range(ipSegs []string, maskLen int) (int, int) {
    if maskLen > 24 {
        segIp, _ := strconv.Atoi(ipSegs[2])
        return segIp, segIp
    }
    ipSeg, _ := strconv.Atoi(ipSegs[2])
    return getIpSegRange(uint8(ipSeg), uint8(24-maskLen))
}

//得到第四段IP的区间(第一片段.第二片段.第三片段.第四片段)
func getIpSeg4Range(ipSegs []string, maskLen int) (int, int) {
    ipSeg, _ := strconv.Atoi(ipSegs[3])
    segMinIp, segMaxIp := getIpSegRange(uint8(ipSeg), uint8(32-maskLen))
    return segMinIp + 1, segMaxIp
}

//根据用户输入的基础IP地址和CIDR掩码计算一个IP片段的区间
func getIpSegRange(userSegIp, offset uint8) (int, int) {
    var ipSegMax uint8 = 255
    netSegIp := ipSegMax << offset
    segMinIp := netSegIp & userSegIp
    segMaxIp := userSegIp&(255<<offset) | ^(255 << offset)
    return int(segMinIp), int(segMaxIp)
}

golang获取一个ip段所有地址


//getIpAll 获取ip段
//最小IP: 223.255.0.1  最大IP: 223.255.127.255
func getIpAll(minIp, maxIp string) []string {
    ipArr := make([]string, 0)
    minIpaddress := net.ParseIP(minIp)
    maxIpaddress := net.ParseIP(maxIp)
    if minIpaddress == nil || maxIpaddress == nil {
        fmt.Println("ip地址格式不正确")
    } else {
        minIpSplitArr := strings.Split(minIp, ".")
        maxIpSplitArr := strings.Split(maxIp, ".")

        minIP1, _ := strconv.Atoi(minIpSplitArr[0])
        minIP2, _ := strconv.Atoi(minIpSplitArr[1])
        minIP3, _ := strconv.Atoi(minIpSplitArr[2])
        minIP4, _ := strconv.Atoi(minIpSplitArr[3])

        maxIP1, _ := strconv.Atoi(maxIpSplitArr[0])
        maxIP2, _ := strconv.Atoi(maxIpSplitArr[1])
        maxIP3, _ := strconv.Atoi(maxIpSplitArr[2])
        maxIP4, _ := strconv.Atoi(maxIpSplitArr[3])

        if minIP1 <= maxIP1 {
            for i1 := minIP1; i1 <= maxIP1; i1++ {
                minIP1 = i1
                var i2 int
                var maxi2 int
                if minIP1 == maxIP1 { //如果第一个数相等
                    i2 = minIP2
                    maxi2 = maxIP2
                } else {
                    i2 = 0
                    maxi2 = 255
                }
                for ii2 := i2; ii2 <= maxi2; ii2++ {
                    minIP2 = ii2
                    var i3 int
                    var maxi3 int
                    if minIP1 == maxIP1 && minIP2 == maxIP2 { //如果第一个数相等 并且 第二个数相等
                        i3 = minIP3
                        maxi3 = maxIP3
                    } else {
                        i3 = 0
                        maxi3 = 255
                    }
                    for ii3 := i3; ii3 <= maxi3; ii3++ {
                        minIP3 = ii3
                        var i4 int
                        var maxi4 int
                        if minIP1 == maxIP1 && minIP2 == maxIP2 && minIP3 == maxIP3 { //如果第一个数相等 并且 第二个数相等 并且 第三个数相等
                            i4 = minIP4
                            maxi4 = maxIP4
                        } else {
                            i4 = minIP4
                            maxi4 = 255
                        }
                        for ii4 := i4; ii4 <= maxi4; ii4++ {
                            minIP4 = ii4
                            newIP := fmt.Sprintf("%v.%v.%v.%v", minIP1, minIP2, minIP3, minIP4)
                            ipArr = append(ipArr, newIP)

                        }
                        minIP4 = 0

                    }
                    minIP3 = 0

                }
                minIP2 = 0

            }
        }
    }
    return ipArr
}

golang复制文件夹和文件



// File copies a single file from src to dst
func File(src, dst string) error {
    var err error
    var srcfd *os.File
    var dstfd *os.File
    var srcinfo os.FileInfo

    if srcfd, err = os.Open(src); err != nil {
        return err
    }
    defer srcfd.Close()

    if dstfd, err = os.Create(dst); err != nil {
        return err
    }
    defer dstfd.Close()

    if _, err = io.Copy(dstfd, srcfd); err != nil {
        return err
    }
    if srcinfo, err = os.Stat(src); err != nil {
        return err
    }
    return os.Chmod(dst, srcinfo.Mode())
}

// CopyDir copies a whole directory recursively
func CopyDir(src string, dst string) error {
    var err error
    var fds []os.FileInfo
    var srcinfo os.FileInfo

    if srcinfo, err = os.Stat(src); err != nil {
        return err
    }

    if err = os.MkdirAll(dst, srcinfo.Mode()); err != nil {
        return err
    }

    if fds, err = ioutil.ReadDir(src); err != nil {
        return err
    }
    for _, fd := range fds {
        srcfp := path.Join(src, fd.Name())
        dstfp := path.Join(dst, fd.Name())

        if fd.IsDir() {
            if err = CopyDir(srcfp, dstfp); err != nil {
                fmt.Println(err)
            }
        } else {
            if err = File(srcfp, dstfp); err != nil {
                fmt.Println(err)
            }
        }
    }
    return nil
}


golang给图片添加图片水印



//一个图片添加水印
func SetSY(logo, tp string) {
    //原始图片是sam.jpg
    imgb, err := os.Open(tp)
    if err != nil {
        fmt.Println("打开图片失败")
        return
    }
    img, format1, err := image.Decode(imgb)
    if err != nil {
        fmt.Println("解码图片失败-", format1, err.Error())
        return
    }
    defer imgb.Close()

    wmb, err := os.Open(logo)
    if err != nil {
        fmt.Println("打开Logo失败")
        return
    }
    watermark, format2, err := image.Decode(wmb)
    if err != nil {
        fmt.Println("解码Logo失败-", format2, err.Error())
        return
    }

    defer wmb.Close()

    //把水印写到右下角,并向0坐标各偏移10个像素
    offset := image.Pt(img.Bounds().Dx()-watermark.Bounds().Dx()-10, img.Bounds().Dy()-watermark.Bounds().Dy()-10)
    b := img.Bounds()
    m := image.NewNRGBA(b)

    draw.Draw(m, b, img, image.ZP, draw.Src)
    draw.Draw(m, watermark.Bounds().Add(offset), watermark, image.ZP, draw.Over)

    //生成新图片new.jpg,并设置图片质量..
    imgw, _ := os.Create(tp)
    jpeg.Encode(imgw, m, &jpeg.Options{100})

    defer imgw.Close()

    fmt.Println("添加水印成功...")
}

beego on range loop re-entry: {{range}} branches end in different contexts:


在html模板js中 ``字符串模板中渲染出现的这个错误

template Execute err: html/template:xxxx/xxxx.html:34:36: on range loop re-entry: {{range}} branches end in different contexts: {stateJSRegexp delimNone urlPartNone jsCtxRegexp attrNone elementScript <nil>}, {stateJS delimNone urlPartNone jsCtxRegexp attrNone elementScript <nil>}

最后改成这样写 就没问题了

QQ图片20200912145604.png