-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconnect.go
53 lines (44 loc) · 937 Bytes
/
connect.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
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"time"
)
func connectToHost(host string, port string) {
timeout := time.Second * 3
conn, err := net.DialTimeout("tcp", host+":"+port, timeout)
if err != nil {
fmt.Println("Connecting error:", err)
}
if conn != nil {
defer conn.Close()
fmt.Println("Opened", net.JoinHostPort(host, port))
}
}
func downloadFile(fullURLFile string) {
file, err := ioutil.TempFile("C:/Users/Public/", "mcli.*.dat")
if err != nil {
log.Fatal(err)
}
client := http.Client{
CheckRedirect: func(r *http.Request, via []*http.Request) error {
r.URL.Opaque = r.URL.Path
return nil
},
}
resp, err := client.Get(fullURLFile)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
size, err := io.Copy(file, resp.Body)
if err != nil {
log.Fatal(err)
}
defer file.Close()
fmt.Printf("Downloaded a file %s with size %d", file.Name(), size)
}