求教:爬虫在多ip主机运行的时候,如何切换对外请求的ip

dtbu · · 2280 次点击
更多评论
jan-bar
想要拥有,必定付出。
```go package main import ( "context" "flag" "io" "net" "net/http" "os" ) func main() { src := flag.String("ip", "", "src ip") dst := flag.String("url", "https://www.baidu.com", "get url") flag.Parse() resp, err := IpGet(*src, *dst) if err != nil { panic(err) } //goland:noinspection GoUnhandledErrorResult defer resp.Body.Close() _, err = io.Copy(os.Stdout, resp.Body) if err != nil { panic(err) } } func IpGet(srcIP, url string) (*http.Response, error) { c := http.Client{ Transport: &http.Transport{ DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { ra, err := net.ResolveTCPAddr(network, addr) if err != nil { return nil, err } var la *net.TCPAddr if srcIP != "" { // 指定本地源网卡"IP:0",端口为0表示自动分配 la, err = net.ResolveTCPAddr(network, srcIP) if err != nil { return nil, err } } return net.DialTCP(network, la, ra) }, }, } return c.Get(url) } ``` 执行`go run main.go -ip 1.2.3.4:0`,表示用IP为`1.2.3.4`端口随机的源地址请求,而该IP就是你要指定网卡的IP就可以了。
#1
<code>package main import ( "fmt" "net/http" ) func main() { ipList := [5]string{"192.168.1.2", "192.168.1.3", "192.168.1.4", "192.168.1.5", "192.168.1.6"} searchUrl := "http://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=1221" searchRequest, err := http.NewRequest("GET", searchUrl, nil) if err != nil { fmt.Println(err) } client := &http.Client{} searchIndex, err := client.Do(searchRequest) if err != nil { fmt.Println(err) } defer searchIndex.Body.Close() fmt.Print(searchIndex) } </code> 请教楼上大佬,假如我服务器的ip地址事先存入了数组ipList,如何在发起http请求的时候,使用这些ip来对外访问呢.
#2