Go Pentester - HTTP Servers(1)
HTTP Server Basics
Use net/http package and useful third-party packages by building simple servers.
Building a Simple Server
- package main
- import (
- "fmt"
- "net/http"
- )
- func hello(w http.ResponseWriter, r *http.Request) {
- fmt.Fprintf(w, "Hello %s\n", r.URL.Query().Get("name"))
- }
- func main() {
- http.HandleFunc("/hello", hello)
- http.ListenAndServe(":8000",nil)
- }
Run the above program and test it.
- curl -i http://localhost:8000/hello?name=eric
You can also use http.ListenAndServeTLS(), which will start a server using HTTPS and TLS.
Build a Simple Router
- package main
- import (
- "fmt"
- "net/http"
- )
- type router struct {
- }
- func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
- switch req.URL.Path {
- case "/a":
- fmt.Fprintf(w, "Executing /a\n")
- case "/b":
- fmt.Fprintf(w, "Executing /b\n")
- case "/c":
- fmt.Fprintf(w, "Executing /c\n")
- default:
- http.Error(w, "404 Not Found", 404)
- }
- }
- func main() {
- var r router
- http.ListenAndServe(":8000", &r)
- }
Test the above program by the following commands.
- curl http://localhost:8000/a
- curl http://localhost:8000/d
Building simple Middleware
A simple middleware, which is a sort of wrapper that will execute on all incoming requests regardless of the destination function.
- package main
- import (
- "fmt"
- "log"
- "net/http"
- "time"
- )
- type logger struct {
- Inner http.Handler
- }
- func (l *logger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- log.Printf("start %s\n", time.Now().String())
- l.Inner.ServeHTTP(w,r)
- log.Printf("finish %s",time.Now().String())
- }
- func hello(w http.ResponseWriter, r *http.Request) {
- fmt.Fprint(w, "Hello\n")
- }
- func main() {
- f := http.HandlerFunc(hello)
- l := logger{Inner: f}
- http.ListenAndServe(":8000", &l)
- }
Run the program and issue a request.
- curl http://localhost:8000
Go Pentester - HTTP Servers(1)的更多相关文章
- Go Pentester - HTTP Servers(2)
Routing with the gorilla/mux Package A powerful HTTP router and URL matcher for building Go web serv ...
- Go Pentester - HTTP Servers(3)
Building Middleware with Negroni Reasons use middleware, including logging requests, authenticating ...
- Coping with the TCP TIME-WAIT state on busy Linux servers
Coping with the TCP TIME-WAIT state on busy Linux servers 文章源自于:https://vincent.bernat.im/en/blog/20 ...
- How To Restart timer service on all servers in farm
[array]$servers= Get-SPServer | ? {$_.Role -eq "Application"} $farm = Get-SPFarm foreach ( ...
- eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Sertomcat
eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Serto ...
- coderforces #387 Servers(模拟)
Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- Servers
Servers¶ Server interface. class novaclient.v1_1.servers.Server(manager, info, loaded=False) Bases: ...
- 使用servers 启动项目时 ,一直处于启动中, 最后出现无法的问题。
使用eclipse 中的servers 配置了一个server 来启动项目, 发现无法启动 排除法: 去掉项目配置,单独启动该server ,发现可以启动, 说明是项目出现问题 但是项目并没有报错, ...
- servers中添加server时,看不到运行环境的选择。
servers中添加server时,看不到运行环境的选择. 主要原因是tomcat目录中的配置文件格式不对.
随机推荐
- 简单梳理JavaScript垃圾回收机制
JavaScript具有自动垃圾回收机制,即执行环境会负责管理代码执行过程中使用地内存. 这种垃圾回收机制的原理很简单:找出那些不再继续使用的变量,然后释放其占用的内存.为此,垃圾收集器会按照固定的时 ...
- MFC的Spin Control基础控件的使用
1.向GUI界面添加一个MFC 提供的Spin数值调节控件 2.设置其"伙伴“,并设置数值调节的范围 3.如何让数值显示在文本框中?你可以有多种选择:可以让文本框控件绑定一个数值类型的变量: ...
- 08.DRF-反序列化
三.反序列化使用 3.1 验证 使用序列化器进行反序列化时,需要对数据进行验证后,才能获取验证成功的数据或保存成模型类对象. 在获取反序列化的数据前,必须调用is_valid()方法进行验证,验证成功 ...
- java中的excel操作
导入jxl.jar包: 下载个jxl.jar包,然后这个包放在什么位置都行,在你的项目中导入这个包就可以. 具体做法: 项目上右键,点击“属性”, 类别那里选择”库“,点击"添加jar文 ...
- 深入理解JVM(③)ZGC收集器
前言 ZGC是一款在JDK11中新加入的具有实验性质的低延迟垃圾收集器,目前仅支持Linux/x86-64.ZGC收集器是一款基于Region内存布局的,(暂时)不设分代的,使用了读屏障.染色指针和内 ...
- Redis->主从复制->哨兵模式(高可用)
一:安装redis $ yum -y install gcc $ yum -y install gcc-c++ $ wget http://download.redis.io/releases/red ...
- 主机Redis服务迁移到现有Docker Overlay环境
记录最后一次对中型2C企业级项目的容器化改造 hello, 好久不见,之前文章记录了一个实战的2C分布式项目的改造过程,结果如下: 其中Redis并未完成容器化改造(目前是主机单点),本文记录将Red ...
- 入门大数据---Hbase容灾与备份
一.前言 本文主要介绍 Hbase 常用的三种简单的容灾备份方案,即CopyTable.Export/Import.Snapshot.分别介绍如下: 二.CopyTable 2.1 简介 CopyTa ...
- Dll的多字节和Unicode
Dll的多字节和Unicode 分类: MFC2013-10-17 13:00 28人阅读 评论(0) 收藏 举报 dll字符集字符集多字节Unicode 我们定义dll的时候会区分: 字符集:使用多 ...
- Oracle数据库中,误删除或者修改数据恢复方法
在我们实际工作中,误删除或者修改Oracle数据库中的数据,怎么办呢?这里给大家分享一种解决办法.假如你误操作的时间不超过30分钟(数据库默认的回滚保持段里的数据时间,可以在pl/sql执行窗口按ct ...