首页
📷在线壁纸
🎬娱乐天地
🔖友情链接
更多
📝留言板
Search
1
【javascript】JS-向当前url追加参数
2,343 阅读
2
【PHP】生成随机昵称
2,200 阅读
3
【PHP】判断一个字符串是否属于序列化后的数据
2,022 阅读
4
【css】html+css给文章页,做阅读全文
1,973 阅读
5
【PHP】 设计模式(23种)
1,907 阅读
📂默认分类
💓typecho
🏳️🌈代码改变世界
🍇 mysql
🍈 Winform
🍓 golang
🍉 设计模式
🥝 PHP
🍎 python
🍊 nginx
🍋 网络安全
🍍 javascript
🫑 database
🍡 AI
🍭文艺范
🍏mac
登录
Search
标签搜索
php
typecho
代码注释
redis
mysql
go
golang
nginx
thinkphp
gin
linux
curl
html
mamp
docker
算法
短网址
构造函数
webhook
代码片段
依然范儿特西
累计撰写
142
篇文章
累计收到
1
条评论
首页
栏目
📂默认分类
💓typecho
🏳️🌈代码改变世界
🍇 mysql
🍈 Winform
🍓 golang
🍉 设计模式
🥝 PHP
🍎 python
🍊 nginx
🍋 网络安全
🍍 javascript
🫑 database
🍡 AI
🍭文艺范
🍏mac
页面
📷在线壁纸
🎬娱乐天地
🔖友情链接
📝留言板
搜索到
142
篇与
的结果
2021-10-18
Golang 语言极简 HTTP 客户端 GoRequest
1 介绍GoRequest 是一个极简的 HTTP 客户端,作者灵感来源于 Node.js 库 SuperAgent。相比 Golang 标准库 net/http,GoRequest 使用起来更加简单。GoRequest 官方的口号是 “像机枪一样发送请求”。GoRequest 包含以下功能:支持 HTTP 请求方式:Get/Post/Put/Head/Delete/Patch/Options支持设置 header 请求头支持使用 JSON 字符串作为请求参数支持将多路请求的方式发送数据和文件支持通过代理发送请求支持为请求设置超时支持 TLS 客户端设置支持设置重定向策略支持为请求设置 cookieCookieJar - automatic in-memory cookiejar支持请求头设置基本身份认证安装方式:go get github.com/parnurzeal/gorequest2 HTTP请求方式Golang 发送一个简单的 Get 请求,使用 net/http 标准库和使用 GoRequst 库,两种发送 Get 请求的方式都比较简单。示例代码如下:标准库方式:resp, err := http.Get("http://example.com/")GoRequest 库方式:request := gorequest.New() resp, body, errs := request.Get("http://example.com/").End()或(该 GoRequest 方式无法复用对象)resp, body, errs := gorequest.New().Get("http://example.com/").End()阅读上面这两段代码,我们可以发现,使用标准库的方式发送 Get 请求,甚至比使用 GoRequest 库的方式发送 Get 请求更加简单。但是,当我们需求稍作修改,比如我们需要为 Get 请求,设置 header 头和设置重定向策略。我们再来看一下分别使用标准库和 GoRequest 库两种实现方式。标准库方式:client := &http.Client{ CheckRedirect: redirectPolicyFunc, } req, err := http.NewRequest("GET", "http://example.com", nil) req.Header.Add("If-None-Match", `W/"wyzzy"`) resp, err := client.Do(req)GoRequest 库方式(其它 HTTP 请求方式与 Get 使用方式相同):request := gorequest.New() resp, body, errs := request.Get("http://example.com"). RedirectPolicy(redirectPolicyFunc). Set("If-None-Match", `W/"wyzzy"`). End()阅读上面两段代码,很容易发现使用 GoRequest 方式使实现更加简单。使用标准库方式,首先需要创建一个 Client,然后使用不同的命令设置 header 头等操作,这仅仅是为了实现一个 HTTP 请求。而使用 GoRequest 方式,仅需链式调用两个方法即可轻松实现。3 JSON 格式请求参数在 Golang 语言中,如果使用标准库 net/http 发送请求参数为 JSON 格式的 POST 请求,首先需要先将 map 或 struct 类型的数据,使用标准库 encoding/json 的 Marshal 方法,将数据转换为 JSON 格式的数据,并且设置 header 头参数 Content-Type 的值为 application/json,然后创建一个 Client,最终你的代码变得越来越长,越来越难维护。标准库方式:m := map[string]interface{}{ "name": "backy", "species": "dog", } mJson, _ := json.Marshal(m) contentReader := bytes.NewReader(mJson) req, _ := http.NewRequest("POST", "http://example.com", contentReader) req.Header.Set("Content-Type", "application/json") req.Header.Set("Notes","GoRequest is coming!") client := &http.Client{} resp, _ := client.Do(req)如果使用 GoRequest 库发送请求参数为 JSON 格式的 POST 请求,因为它默认支持 JSON 格式的请求参数,所以它只需要一行代码就可以实现。GoRequest 库方式:request := gorequest.New() resp, body, errs := request.Post("http://example.com"). Set("Notes","gorequst is coming!"). Send(`{"name":"backy", "species":"dog"}`). End()4支持回调函数 CallbackGoRequest 库还支持回调函数,你可以根据自己的项目需求灵活使用它,回调函数示例代码如下:func printStatus(resp gorequest.Response, body string, errs []error){ fmt.Println(resp.Status) } gorequest.New().Get("http://example.com").End(printStatus)5 请求控制在 Golang 项目开发中,有时我们可能需要对请求做一些额外控制,比如超时处理,重试请求 N 次,重定向处理等。GoRequest 库都可以为我们提供简单的实现方式。超时处理:request := gorequest.New().Timeout(2*time.Millisecond) resp, body, errs:= request.Get("http://example.com").End()需要注意的是,Timeout 是将 Dial 连接和 IO 读写的耗时总和,与时间参数作比较。重试请求:request := gorequest.New() resp, body, errs := request.Get("http://example.com/"). Retry(3, 5 * time.Second, http.StatusBadRequest, http.StatusInternalServerError). End()阅读上面这段代码,它的含义是当服务器返回结果是 http.StatusBadRequest 或 http.StatusInternalServerError 时,会每隔 5 秒重试请求一次,共重试 3 次。重定向处理:request := gorequest.New() resp, body, errs := request.Get("http://example.com/"). RedirectPolicy(func(req Request, via []*Request) error { if req.URL.Scheme != "https" { return http.ErrUseLastResponse } }). End() 阅读上面这段代码,它的含义是将 http 请求重定向为 https 请求。6 返回结果处理方式朋友们可能已经发现,以上示例代码都是以 End 结束,End 的含义是返回结果是字符串类型,如果我们希望返回结果是其他类型,比如字节类型和结构体类型,可以将 End 分别替换为 EndBytes 和 EndStruct。EndBytes 格式:resp, bodyBytes, errs := gorequest.New().Get("http://example.com/").EndBytes()EndStruct 格式:heyYou struct { Hey string `json:"hey"` } var heyYou heyYou resp, _, errs := gorequest.New().Get("http://example.com/").EndStruct(&heyYou)7 总结本文我们介绍 Golang 语言的极简 HTTP 客户端 GoRequest 以及它的使用方法。它比标准库 net/http 使用方式简单,当我们项目开发中需要使用 HTTP 方式调用接口时,强烈推荐使用 GoRequest 库。GoRequest 底层在大多数用例中是基于 http.Client 实现的,所以通过一次调用 gorequest.New() 得到的对象,应尽可能多次使用。GoRequest 除了上面介绍的 JSON 参数,它还支持 Struct 和 File,感兴趣的读者可以查阅官方文档了解相关内容。https://github.com/parnurzeal/gorequest
2021年10月18日
261 阅读
0 评论
1 点赞
2021-10-09
nginx-rewrite规则
作用场景:URL访问跳转,支持开发设计,如页面跳转,兼容性支持,展示效果等SEO优化维护:后台维护、流量转发等安全调整用户浏览的URL,看起来规范为了让搜索引擎收录网站内容,让用户体验更好网站更换新域名后根据特殊的变量、目录、客户端信息进行跳转注:nginx官方文档:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html1、rewrite语法:指令语法:rewrite regex replacement[flag];默认值:none应用位置:server、location、ifrewrite是实现URL重定向的重要指令,他根据regex(正则表达式)来匹配内容跳转到replacement,结尾是flag标记简单的小例子:# 匹配成功后跳转到百度,执行永久301跳转 rewrite ^/(.*) http://www.baidu.com/ permanent;常用正则表达式:字符描述\将后面接着的字符标记为一个特殊字符或者一个原义字符或一个向后引用^匹配输入字符串的起始位置$匹配输入字符串的结束位置*匹配前面的字符零次或者多次+匹配前面字符串一次或者多次?匹配前面字符串的零次或者一次.匹配除“\n”之外的所有单个字符(pattern)匹配括号内的patternrewrite 最后一项flag参数:标记符号说明last停止rewrite检测【如果没有匹配到,会继续向下匹配】break停止rewrite检测【如果没有匹配到,则不再向下匹配,直接返回结果404redirect返回302临时重定向,地址栏会显示跳转后的地址permanent返回301永久重定向,地址栏会显示跳转后的地址
2021年10月09日
130 阅读
0 评论
4 点赞
2021-09-30
Golang之log(如何将日志写到指定文件里面)
对于Go语言的日志来说,如何将log写到指定的文件里面,下面是一个例子。如何将log 写入到指定的文件中?方法一:package main import ( "log" "os" "time" ) func init() { file := "./" +"log"+ ".txt" logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766) if err != nil { panic(err) } log.SetOutput(logFile) // 将文件设置为log输出的文件 log.SetPrefix("[logTool]") log.SetFlags(log.LstdFlags | log.Lshortfile | log.LUTC) return } func main() { log.Println("Hello laofan!") // log 还是可以作为输出的前缀 return }output:// message.txt里面 显示 [logTool]2021/09/30 15:30:05 log.go:24: Hello laofan! 方法二:package main import ( "log" "os" "time" ) var loger *log.Logger func init() { file := "./" + time.Now().Format("20210930") + ".txt" logFile, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0766) if err != nil { panic(err) } loger = log.New(logFile, "[logTool]",log.LstdFlags | log.Lshortfile | log.LUTC) // 将文件设置为loger作为输出 return } func main() { // 使用的时候,需要采用loger作为输出的前缀 loger.Println("Hello:laofan!") return } output:// message.txt里面 显示 [logTool]2021/09/30 15:35:20 log.go:24: Hello laofan! `` 灰子作于二零二一年九月三十日。
2021年09月30日
827 阅读
0 评论
0 点赞
2021-09-21
tree命令输出目录树层结构
你也想要生成这个目录树层结构吗?,按下面操作即可:. ├── etc │ └── greet-api.yaml ├── go.mod ├── go.sum ├── greet.api ├── greet.go └── internal ├── config │ └── config.go ├── handler │ ├── greethandler.go │ └── routes.go ├── logic │ └── greetlogic.go ├── svc │ └── servicecontext.go └── types └── types.go 7 directories, 11 files 1 、安装 tree# mac 下使用 brew包管理工具 brew install tree # linux 下使用yum安装 yum install tree2、 安装成功后,直接在终端使用,使用 --help 查看帮助信息tree --help看到如下功能usage: tree [-acdfghilnpqrstuvxACDFJQNSUX] [-H baseHREF] [-T title ] [-L level [-R]] [-P pattern] [-I pattern] [-o filename] [--version] [--help] [--inodes] [--device] [--noreport] [--nolinks] [--dirsfirst] [--charset charset] [--filelimit[=]#] [--si] [--timefmt[=]<f>] [--sort[=]<name>] [--matchdirs] [--ignore-case] [--] [<directory list>] ------- Listing options ------- -a All files are listed. -d List directories only. -l Follow symbolic links like directories. -f Print the full path prefix for each file. -x Stay on current filesystem only. -L level Descend only level directories deep. -R Rerun tree when max dir level reached. -P pattern List only those files that match the pattern given. -I pattern Do not list files that match the given pattern. --ignore-case Ignore case when pattern matching. --matchdirs Include directory names in -P pattern matching. --noreport Turn off file/directory count at end of tree listing. --charset X Use charset X for terminal/HTML and indentation line output. --filelimit # Do not descend dirs with more than # files in them. --timefmt <f> Print and format time according to the format <f>. -o filename Output to file instead of stdout. -------- File options --------- -q Print non-printable characters as '?'. -N Print non-printable characters as is. -Q Quote filenames with double quotes. -p Print the protections for each file. -u Displays file owner or UID number. -g Displays file group owner or GID number. -s Print the size in bytes of each file. -h Print the size in a more human readable way. --si Like -h, but use in SI units (powers of 1000). -D Print the date of last modification or (-c) status change. -F Appends '/', '=', '*', '@', '|' or '>' as per ls -F. --inodes Print inode number of each file. --device Print device ID number to which each file belongs. ------- Sorting options ------- -v Sort files alphanumerically by version. -t Sort files by last modification time. -c Sort files by last status change time. -U Leave files unsorted. -r Reverse the order of the sort. --dirsfirst List directories before files (-U disables). --sort X Select sort: name,version,size,mtime,ctime. ------- Graphics options ------ -i Don't print indentation lines. -A Print ANSI lines graphic indentation lines. -S Print with CP437 (console) graphics indentation lines. -n Turn colorization off always (-C overrides). -C Turn colorization on always. ------- XML/HTML/JSON options ------- -X Prints out an XML representation of the tree. -J Prints out an JSON representation of the tree. -H baseHREF Prints out HTML format with baseHREF as top directory. -T string Replace the default HTML title and H1 header with string. --nolinks Turn off hyperlinks in HTML output. ---- Miscellaneous options ---- --version Print version and exit. --help Print usage and this help message and exit. -- Options processing terminator. 3、 输出你的树层目录结构cd 目标文件夹路径然后 tree 一下,会将该层级下所有文件都遍历了输出,不管层级多深4、 常用技巧我们可以在目录遍历时使用 -L 参数指定遍历层级tree -L 2如果你想把一个目录的结构树导出到文件 Readme.md ,可以这样操作tree -L 2 >README.md //然后我们看下当前目录下的 README.md 文件只显示文件夹tree -d 显示项目的层级,n表示层级数。例:显示项目三层结构,tree -l 3;tree -L n tree -I pattern 用于过滤不想要显示的文件或者文件夹。比如要过滤项目中的node_modules文件夹;tree -I “node_modules”
2021年09月21日
124 阅读
0 评论
2 点赞
2021-09-18
markdown折叠内容语法
如题,语法是:<details> <summary>Title</summary> content!!! </details> 举例 <details> <summary>CLICK ME</summary> **<summary>标签与正文间一定要空一行!!!** </details>
2021年09月18日
160 阅读
0 评论
0 点赞
2021-09-14
清除谷歌浏览器的dns缓存
清除谷歌浏览器的dns缓存
2021年09月14日
160 阅读
0 评论
2 点赞
2021-09-13
mac下为MAMP添加php扩展msgpack
查看最新的扩展版本https://pecl.php.net/package/msgpackPHP7.2.22 的文件夹地址/Applications/MAMP/bin/php/php7.2.22wget -c https://pecl.php.net/get/msgpack-0.0.1.tgz tar zxvf msgpack.tgz cd msgpack /Applications/MAMP/bin/php/php7.2.22/bin/phpize ./configure --with-php-config=/Applications/MAMP/bin/php/php7.2.22/bin/php-config make && make install vi /Applications/MAMP/bin/php/php7.2.22/etc/php.ini #加上一行 extension=msgpack.so # 重启下php # php -m 验证
2021年09月13日
132 阅读
0 评论
2 点赞
2021-09-13
Mac终端采用mamp的PHP版本运行
查看环境变量,PHP运行的文件位置:which php终端输入,可能,bash_profile 文件并不存在,就创建新文件vi ~/.bash_profile然后把环境变量代码添加到bash_profile脚本里export PATH="/Applications/MAMP/bin/php/php7.2.22/bin:$PATH"执行文件,使起生效source ~/.bash_profile
2021年09月13日
130 阅读
0 评论
1 点赞
2021-09-03
linux 实现每秒执行&间隔 90分钟执行
前言linux crontab 命令,最小的执行时间是一分钟, 如果要在小于一分钟执行。就要换个方法来实现crontab 的延时:原理:通过延时方法 sleep N 来实现每N秒执行。crontab -e 输入以下语句,然后 :wq 保存退出。* * * * * /usr/bin/curl http://www.test.com * * * * * sleep 5; /usr/bin/curl http://www.test.com * * * * * sleep 10; /usr/bin/curl http://www.test.com * * * * * sleep 15; /usr/bin/curl http://www.test.com * * * * * sleep 20; /usr/bin/curl http://www.test.com * * * * * sleep 25; /usr/bin/curl http://www.test.com * * * * * sleep 30; /usr/bin/curl http://www.test.com * * * * * sleep 35; /usr/bin/curl http://www.test.com * * * * * sleep 40; /usr/bin/curl http://www.test.com * * * * * sleep 45; /usr/bin/curl http://www.test.com * * * * * sleep 50; /usr/bin/curl http://www.test.com * * * * * sleep 55; /usr/bin/curl http://www.test.com注意:60必须能整除间隔的秒数(没有余数),例如间隔的秒数是2,4,6,10,12等。如果间隔的秒数太少,例如2秒执行一次,这样就需要在crontab 加入60/2=30条语句。不建议使用此方法,可以使用下面介绍的第二种方法。2 shell 脚本实现原理:在sh使用for语句实现循环指定秒数执行。crontab.sh#!/bin/bash step=2 #间隔的秒数,不能大于60 for (( i = 0; i < 60; i=(i+step) )); do $(php '/home/fdipzone/php/crontab/tolog.php') sleep $step done exit 0 crontab -e 输入以下语句,然后:wq 保存退出。# m h dom mon dow command * * * * * /home/fdipzone/php/crontab/crontab.sh 使用以下命令查看结果tail -f run.log 注意:如果60不能整除间隔的秒数,则需要调整执行的时间。例如需要每7秒执行一次,就需要找到7与60的最小公倍数,7与60的最小公倍数是420(即7分钟)。则 crontab.sh step的值为7,循环结束条件i<420, crontab -e可以输入以下语句来实现# m h dom mon dow command */7 * * * * /home/fdipzone/php/crontab/crontab.sh 举一反三q : 如果需要间隔 90 分钟 执行一次,如何处理?a :把他扩大成 24小时 = 24 * 60 分钟执行次数 = 24*60 / 90 = 16也就是最大可执行 16次每次 sleep 90*60 秒即可实现 每间隔90分钟 执行一次代码实现:#!/bin/bash step=5400 #间隔的秒数 for (( i = 0; i < 16; i=(i+step) )); do $(php '/home/fdipzone/php/crontab/tolog.php') sleep $step done exit 0
2021年09月03日
736 阅读
2 评论
0 点赞
2021-09-01
Golang 获取https证书信息、过期信息
package main import ( "crypto/tls" "fmt" "net/http" ) func main() { tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, } client := &http.Client{Transport: tr} seedUrl := "https://richerdyoung.com" resp, err := client.Get(seedUrl) defer resp.Body.Close() if err != nil { fmt.Errorf(seedUrl," 请求失败") panic(err) } //fmt.Println(resp.TLS.PeerCertificates[0]) certInfo:=resp.TLS.PeerCertificates[0] fmt.Println("过期时间:",certInfo.NotAfter) fmt.Println("组织信息:",certInfo.Subject) } 运行结果过期时间: 2021-09-02 07:27:20 +0000 UTC 组织信息: CN=www.richerdyoung.com
2021年09月01日
324 阅读
0 评论
1 点赞
1
...
6
7
8
...
15