-
Notifications
You must be signed in to change notification settings - Fork 2
/
httping.go
69 lines (58 loc) · 1.4 KB
/
httping.go
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
68
69
package main
import (
"fmt"
"time"
"os"
"net/http"
"log"
"io/ioutil"
"strconv"
)
func check (url,port string) (urltime float64, urlsize int) {
t0 := time.Now()
client := &http.Client{}
// default case: HTTP request
domain := "http://"+url+":"+port
switch port {
case "80" : domain = "http://"+url;
case "443": domain = "https://"+url;
}
req, err := http.NewRequest("GET", domain, nil)
if err != nil {
// handle error
log.Fatalf ("|----- I couldn't connect to %s", url)
} else {
req.Proto = "HTTP/1.1"
req.ProtoMinor = 0
req.Header.Set("User-Agent", "GoLang httping v0.1")
resp, err := client.Do(req)
if err != nil {
// handle error
log.Fatalf ("|----- I couldn't connect to %s\n", url)
} else {
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
url_size := len(body)
msec := time.Since(t0)
url_time := msec.Seconds() * float64(time.Second/time.Millisecond)
return url_time, url_size
}
}
return
}
func main () {
fmt.Printf ("GoLang httping v0.1 - PINGING %s\n", os.Args[1])
port := "80"
// Do we have port defined ?
if os.Args[2] != "" {
port = os.Args[2]
}
seq := 0
// infinite loop
for {
seq = seq +1
t, s := check(os.Args[1], port)
fmt.Printf ("connected to %s:%s, seq=%d time=%s bytes=%d\n", os.Args[1], port, seq, strconv.FormatFloat(t, 'f', 3, 64), s)
time.Sleep(1 * time.Second)
}
}