func main() { ch1 := curl.EasyInit() ch2 := curl.EasyInit() ch1.Setopt(curl.OPT_URL, "http://www.163.com") ch1.Setopt(curl.OPT_HEADER, 0) ch1.Setopt(curl.OPT_VERBOSE, true) ch2.Setopt(curl.OPT_URL, "http://www.baidu.com") ch2.Setopt(curl.OPT_HEADER, 0) ch2.Setopt(curl.OPT_VERBOSE, true) mh := curl.MultiInit() mh.AddHandle(ch1) mh.AddHandle(ch2) for { nRunning, _ := mh.Perform() // println("n =", nRunning) if nRunning == 0 { println("ok") break } time.Sleep(1000) } }
func main() { var ( rset, wset, eset syscall.FdSet still_running, curl_timeout int = 0, 0 err error ) ch1 := curl.EasyInit() ch2 := curl.EasyInit() ch1.Setopt(curl.OPT_URL, "http://www.163.com") ch1.Setopt(curl.OPT_HEADER, 0) ch1.Setopt(curl.OPT_VERBOSE, true) ch2.Setopt(curl.OPT_URL, "http://www.baidu.com") ch2.Setopt(curl.OPT_HEADER, 0) ch2.Setopt(curl.OPT_VERBOSE, true) mh := curl.MultiInit() mh.AddHandle(ch1) mh.AddHandle(ch2) for { FD_ZERO(&rset) FD_ZERO(&wset) FD_ZERO(&eset) timeout := syscall.Timeval{Sec: 1, Usec: 0} curl_timeout, err = mh.Timeout() if err != nil { fmt.Printf("Error multi_timeout: %s\n", err) } if curl_timeout >= 0 { timeout.Sec = int64(curl_timeout / 1000) if timeout.Sec > 1 { timeout.Sec = 1 } else { timeout.Usec = int64((curl_timeout % 1000)) * 1000 } } max_fd, err := mh.Fdset(&rset, &wset, &eset) if err != nil { fmt.Printf("Error FDSET: %s\n", err) } _, err = syscall.Select(int(max_fd+1), &rset, &wset, &eset, &timeout) if err != nil { fmt.Printf("Error select: %s\n", err) } else { still_running, err = mh.Perform() if still_running > 0 { fmt.Printf("Still running: %d\n", still_running) } else { break } } } }
func getAllImageUrl(url string, ep string) { //init eas easy := curl.EasyInit() defer easy.Cleanup() //header easy = generateHeader(easy) //loop page for i := 0; i < 100; i++ { fmt.Println("page " + strconv.Itoa(i)) url := url + strconv.Itoa(i) ur := getData(easy, url, ep) go downloadFromUrl(ur, ep, strconv.Itoa(i+1)) ts := strings.Split(ur, "/") extn := strings.Split(ts[len(ts)-1], ".") if len(extn) == 1 { i = 100 } else { if extn[1] == "png" || extn[1] == "jpg" { //lnjut } else { i = 100 } } } }
func main() { // init the curl session easy := curl.EasyInit() defer easy.Cleanup() easy.Setopt(curl.OPT_URL, "http://www.renren.com") easy.Setopt(curl.OPT_PORT, 80) easy.Setopt(curl.OPT_VERBOSE, true) easy.Setopt(curl.OPT_CONNECT_ONLY, true) easy.Setopt(curl.OPT_WRITEFUNCTION, nil) if err := easy.Perform(); err != nil { println("ERROR: ", err.Error()) } easy.Send([]byte("HEAD / HTTP/1.0\r\nHost: www.renren.com\r\n\r\n")) buf := make([]byte, 1000) time.Sleep(1000000000) // wait gorotine num, err := easy.Recv(buf) if err != nil { println("ERROR:", err.Error()) } println("recv num = ", num) // NOTE: must use buf[:num] println(string(buf[:num])) fmt.Printf("got:\n%#v\n", string(buf[:num])) }
func main() { curl.GlobalInit(curl.GLOBAL_ALL) // init the curl session easy := curl.EasyInit() defer easy.Cleanup() // set URL to get easy.Setopt(curl.OPT_URL, "http://cn.bing.com/") // no progress meter easy.Setopt(curl.OPT_NOPROGRESS, true) easy.Setopt(curl.OPT_WRITEFUNCTION, write_data) // write file fp, _ := os.OpenFile(bodyfilename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777) defer fp.Close() easy.Setopt(curl.OPT_WRITEDATA, fp) // easy.Setopt(curl.OPT_WRITEHEADER, 0) if err := easy.Perform(); err != nil { println("ERROR: ", err.Error()) } }
func getCep(id string) string { cached := getFromCache(id) if cached != "" { return cached } easy := curl.EasyInit() defer easy.Cleanup() easy.Setopt(curl.OPT_URL, "http://viacep.com.br/ws/"+id+"/json/") result := " " // make a callback function fooTest := func(buf []byte, userdata interface{}) bool { result = string(buf) return true } easy.Setopt(curl.OPT_WRITEFUNCTION, fooTest) if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } return saveOnCache(id, result) }
func main() { curl.GlobalInit(curl.GLOBAL_ALL) // init the curl session easy := curl.EasyInit() defer easy.Cleanup() easy.Setopt(curl.OPT_URL, "http://cn.bing.com/") easy.Setopt(curl.OPT_WRITEFUNCTION, write_data) // make a chan ch := make(chan string, 100) go func(ch chan string) { for { data := <-ch println("Got data size=", len(data)) } }(ch) easy.Setopt(curl.OPT_WRITEDATA, ch) if err := easy.Perform(); err != nil { println("ERROR: ", err.Error()) } time.Sleep(10000) // wait gorotine }
func test_login() { easy := curl.EasyInit() defer easy.Cleanup() fmt.Println(*url) easy.Setopt(curl.OPT_URL, *url) easy.Setopt(curl.OPT_WRITEFUNCTION, callback) var mylogin sLogin mylogin.Cmd = 1 mylogin.User_name = "test1" mylogin.Passwd = "passwd" mylogin.Ver = 1 body, err := json.Marshal(mylogin) if err != nil { panic(err.Error()) } easy.Setopt(curl.OPT_POSTFIELDS, string(body)) if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func main() { curl.GlobalInit(curl.GLOBAL_DEFAULT) defer curl.GlobalCleanup() easy := curl.EasyInit() defer easy.Cleanup() easy.Setopt(curl.OPT_URL, "ftp://ftp.gnu.org/README") // define our callback use lambda function easy.Setopt(curl.OPT_WRITEFUNCTION, func(ptr []byte, userdata interface{}) bool { file := userdata.(*os.File) if _, err := file.Write(ptr); err != nil { return false } return true }) fp, _ := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0777) defer fp.Close() // defer close easy.Setopt(curl.OPT_WRITEDATA, fp) easy.Setopt(curl.OPT_VERBOSE, true) if err := easy.Perform(); err != nil { println("ERROR", err.Error()) } }
func test_get_realm() { //curl -d '{"cmd":3, "machine":1000, "ver":1, "acc_id":1001}' "http://127.0.0.1:9001" easy := curl.EasyInit() defer easy.Cleanup() fmt.Println(*url) easy.Setopt(curl.OPT_URL, *url) easy.Setopt(curl.OPT_WRITEFUNCTION, callback) var getRealm sGet_realm getRealm.Cmd = 3 getRealm.Machine = 1000 getRealm.Ver = 1 getRealm.Acc_id = 1001 body, err := json.Marshal(getRealm) if err != nil { panic(err.Error()) } easy.Setopt(curl.OPT_POSTFIELDS, string(body)) if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func main() { curl.GlobalInit(curl.GLOBAL_ALL) // init the curl session easy := curl.EasyInit() defer easy.Cleanup() easy.Setopt(curl.OPT_URL, "http://curl.haxx.se/download/curl-7.22.0.tar.gz") easy.Setopt(curl.OPT_WRITEFUNCTION, write_data) easy.Setopt(curl.OPT_NOPROGRESS, false) started := int64(0) easy.Setopt(curl.OPT_PROGRESSFUNCTION, func(dltotal, dlnow, ultotal, ulnow float64, userdata interface{}) bool { // canceled when 50% finished if dlnow/dltotal > 0.5 { println("") // abort downloading return false } if started == 0 { started = time.Now().Unix() } fmt.Printf("Downloaded: %3.2f%%, Speed: %.1fKiB/s \r", dlnow/dltotal*100, dlnow/1000/float64((time.Now().Unix()-started))) return true }) if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func main() { easy := curl.EasyInit() defer easy.Cleanup() if easy != nil { easy.Setopt(curl.OPT_URL, "http://www.google.com/") easy.Perform() } }
func test_login() { easy := curl.EasyInit() defer easy.Cleanup() fmt.Println(*url) easy.Setopt(curl.OPT_URL, *url) easy.Setopt(curl.OPT_WRITEFUNCTION, callback) easy.Setopt(curl.OPT_POSTFIELDS, "{\"cmd\":1, \"acc_id\":\"[email protected]\", \"passwd\": \"mypasswd\"}") if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func test_get_user_id() { easy := curl.EasyInit() defer easy.Cleanup() fmt.Println(*url) easy.Setopt(curl.OPT_URL, *url) easy.Setopt(curl.OPT_WRITEFUNCTION, callback) easy.Setopt(curl.OPT_POSTFIELDS, "{\"cmd\": 5, \"realm_id\":0, \"acc_id\": 1}") if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func test_get_realm() { easy := curl.EasyInit() defer easy.Cleanup() fmt.Println(*url) easy.Setopt(curl.OPT_URL, *url) easy.Setopt(curl.OPT_WRITEFUNCTION, callback) easy.Setopt(curl.OPT_POSTFIELDS, "{\"cmd\":3, \"machine\": 1000, \"ver\": 1}") if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func test_login() { easy := curl.EasyInit() defer easy.Cleanup() fmt.Println(*url) easy.Setopt(curl.OPT_URL, *url) easy.Setopt(curl.OPT_WRITEFUNCTION, callback) easy.Setopt(curl.OPT_POSTFIELDS, "{\"cmd\":1, \"user_name\":\"test1\", \"passwd\": \"passwd\", \"ver\":1}") if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func main() { url := fmt.Sprintf("http://dev.markitondemand.com/MODApis/Api/v2/Quote/jsonp?symbol=%v&callback=my", os.Args[1]) easy := curl.EasyInit() defer easy.Cleanup() easy.Setopt(curl.OPT_URL, url) handleData := func(buf []byte, userdata interface{}) bool { s := string(buf) print(s) return true } easy.Setopt(curl.OPT_WRITEFUNCTION, handleData) easy.Perform() }
func test_get_realm() { //curl -d '{"cmd":3, "machine":1000, "ver":1, "acc_id":1001}' "http://127.0.0.1:9001" easy := curl.EasyInit() defer easy.Cleanup() fmt.Println(*url) easy.Setopt(curl.OPT_URL, *url) easy.Setopt(curl.OPT_WRITEFUNCTION, callback) easy.Setopt(curl.OPT_POSTFIELDS, "{\"cmd\":3, \"machine\": 1000, \"ver\": 1, \"acc_id\": 1001}") if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func main() { easy := curl.EasyInit() defer easy.Cleanup() easy.Setopt(curl.OPT_URL, "http://www.baidu.com/") // make a callback function fooTest := func(buf []byte, userdata interface{}) bool { println("DEBUG: size=>", len(buf)) println("DEBUG: content=>", string(buf)) return true } easy.Setopt(curl.OPT_WRITEFUNCTION, fooTest) if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func main() { fmt.Println("Start proxy") http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { //urlhash := r.URL.Path[1:] q := r.URL.Query() urlhash := q.Get("u") url, err := base64.StdEncoding.DecodeString(urlhash) //url = []byte("https://www.google.com/search?q=neighborcity+agent+Karen+Frost&client=firefox-aurora&hs=CtL&rls=org.mozilla:en-US:unofficial&channel=sb&source=lnms&tbm=isch") fmt.Println(string(url)) //return if err != nil { fmt.Println(w, "Error.") return } //aHR0cHM6Ly9heGNvdG8uY29t -> https://axcoto.com //fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:]) //aHR0cHM6Ly9pZmNvbmZpZy5tZQ== -> ifconfig.me easy := curl.EasyInit() defer easy.Cleanup() easy.Setopt(curl.OPT_URL, string(url)) //What to do when finish CURL request. //We write back to original response requestFinish := func(buf []byte, userdata interface{}) bool { //println("DEBUG: size=>", len(buf)) //println("DEBUG: content=>", string(buf)) fmt.Fprintf(w, "%s", string(buf)) return true } easy.Setopt(curl.OPT_WRITEFUNCTION, fooTest) //easy.Setopt(curl.OPT_HTTPHEADER, []string{"User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0"}) easy.Setopt(curl.OPT_HTTPHEADER, []string{r.UserAgent()}) if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }) http.ListenAndServe(":23478", nil) }
func main() { // init the curl session easy := curl.EasyInit() defer easy.Cleanup() posturl := getUploadUrl() easy.Setopt(curl.OPT_URL, posturl) easy.Setopt(curl.OPT_PORT, 80) easy.Setopt(curl.OPT_VERBOSE, true) // save cookie and load cookie easy.Setopt(curl.OPT_COOKIEFILE, "./cookie.jar") easy.Setopt(curl.OPT_COOKIEJAR, "./cookie.jar") // disable HTTP/1.1 Expect: 100-continue easy.Setopt(curl.OPT_HTTPHEADER, []string{"Expect:"}) form := curl.NewForm() form.Add("albumid", "452618633") // your album id form.AddFile("theFile", "./test.jpg") form.Add("description", "我就尝试下这段代码靠谱不。。截图下看看") form.Add("post", "上传照片") easy.Setopt(curl.OPT_HTTPPOST, form) // print upload progress easy.Setopt(curl.OPT_NOPROGRESS, false) easy.Setopt(curl.OPT_PROGRESSFUNCTION, func(dltotal, dlnow, ultotal, ulnow float64, _ interface{}) bool { fmt.Printf("Download %3.2f%%, Uploading %3.2f%%\r", dlnow/dltotal*100, ulnow/ultotal*100) return true }) if err := easy.Perform(); err != nil { println("ERROR: ", err.Error()) } time.Sleep(1000000000) // wait gorotine }
func test_get_init_mech() { easy := curl.EasyInit() defer easy.Cleanup() fmt.Println(*url) easy.Setopt(curl.OPT_URL, *url) easy.Setopt(curl.OPT_WRITEFUNCTION, callback) var InitMech sGet_init_mech InitMech.Cmd = 9 InitMech.Acc_id = 1001 body, err := json.Marshal(InitMech) if err != nil { panic(err.Error()) } easy.Setopt(curl.OPT_POSTFIELDS, string(body)) if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func test_get_user_id() { easy := curl.EasyInit() defer easy.Cleanup() fmt.Println(*url) easy.Setopt(curl.OPT_URL, *url) easy.Setopt(curl.OPT_WRITEFUNCTION, callback) var Get_user_id sGet_user_id Get_user_id.Cmd = 5 Get_user_id.Realm_id = 0 Get_user_id.Acc_id = 1 body, err := json.Marshal(Get_user_id) if err != nil { panic(err.Error()) } easy.Setopt(curl.OPT_POSTFIELDS, string(body)) if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func test_apply_account() { easy := curl.EasyInit() defer easy.Cleanup() fmt.Println(*url) easy.Setopt(curl.OPT_URL, *url) easy.Setopt(curl.OPT_WRITEFUNCTION, callback) var Apply_account sApply_account Apply_account.Cmd = 7 Apply_account.Acc_name = "*****@*****.**" Apply_account.Passwd = "passwd" body, err := json.Marshal(Apply_account) if err != nil { panic(err.Error()) } easy.Setopt(curl.OPT_POSTFIELDS, string(body)) if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func main() { //for i:=0; i<10; i++{ easy := curl.EasyInit() defer easy.Cleanup() easy.Setopt(curl.OPT_URL, "http://127.0.0.1:9001") // make a callback function fooTest := func(buf []byte, userdata interface{}) bool { println("DEBUG: size=>", len(buf)) println("DEBUG: content=>", string(buf)) return true } easy.Setopt(curl.OPT_WRITEFUNCTION, fooTest) easy.Setopt(curl.OPT_POSTFIELDS, "{\"user_id\":134}") if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } //} }
func test_get_player_info() { easy := curl.EasyInit() defer easy.Cleanup() fmt.Println(*url) easy.Setopt(curl.OPT_URL, *url) easy.Setopt(curl.OPT_WRITEFUNCTION, callback) var player sQryPlayer player.Cmd = 15 player.Player_id = *player_id body, err := json.Marshal(player) if err != nil { panic(err.Error()) } easy.Setopt(curl.OPT_POSTFIELDS, string(body)) if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func test_sync_cust_user() { easy := curl.EasyInit() defer easy.Cleanup() fmt.Println(*url) easy.Setopt(curl.OPT_URL, *url) easy.Setopt(curl.OPT_WRITEFUNCTION, callback) var player sSyncCustUser player.Cmd = 13 player.Acc_id = 1 player.User_id = 1000 player.Realm_id = 0 body, err := json.Marshal(player) if err != nil { panic(err.Error()) } easy.Setopt(curl.OPT_POSTFIELDS, string(body)) if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func test_apply_player() { easy := curl.EasyInit() defer easy.Cleanup() fmt.Println(*url) easy.Setopt(curl.OPT_URL, *url) easy.Setopt(curl.OPT_WRITEFUNCTION, callback) var player sApply_player player.Cmd = 11 player.Acc_id = 1001 player.Mech_id = 11000 player.Player_name = "无敌" player.Realm_id = 0 body, err := json.Marshal(player) if err != nil { panic(err.Error()) } easy.Setopt(curl.OPT_POSTFIELDS, string(body)) if err := easy.Perform(); err != nil { fmt.Printf("ERROR: %v\n", err) } }
func getUploadUrl() string { page := "" easy := curl.EasyInit() defer easy.Cleanup() u := "http://3g.renren.com/album/wuploadphoto.do" easy.Setopt(curl.OPT_URL, u) easy.Setopt(curl.OPT_COOKIEFILE, "./cookie.jar") easy.Setopt(curl.OPT_COOKIEJAR, "./cookie.jar") easy.Setopt(curl.OPT_VERBOSE, true) easy.Setopt(curl.OPT_WRITEFUNCTION, func(ptr []byte, _ interface{}) bool { page += string(ptr) return true }) easy.Perform() // extract url from // <form enctype="multipart/form-data" action="http://3g.renren.com/album/wuploadphoto.do?type=3&sid=zv3tiXTZr6Cu1rj5dhgX_X" pattern, _ := regexp.Compile(`action="(.*?)"`) if matches := pattern.FindStringSubmatch(page); len(matches) == 2 { return matches[1] } return "" }
func main() { println("DEBUG chdir=>", os.Chdir("/sadf")) ret := curl.EasyInit() defer ret.Cleanup() print("init =>", ret, " ", reflect.TypeOf(ret).String(), endl) ret = ret.Duphandle() defer ret.Cleanup() print("dup =>", ret, " ", reflect.TypeOf(ret).String(), endl) print("global init =>", curl.GlobalInit(curl.GLOBAL_ALL), endl) print("version =>", curl.Version(), endl) // debug //print("set verbose =>", ret.Setopt(curl.OPT_VERBOSE, true), endl) //print("set header =>", ret.Setopt(curl.OPT_HEADER, true), endl) // auto calculate port // print("set port =>", ret.EasySetopt(curl.OPT_PORT, 6060), endl) fmt.Printf("XXXX debug setopt %#v \n", ret.Setopt(30000, 19).Error()) print("set timeout =>", ret.Setopt(curl.OPT_TIMEOUT, 20), endl) //print("set post size =>", ret.Setopt(curl.OPT_POSTFIELDSIZE, 10), endl) if ret.Setopt(curl.OPT_URL, "http://www.baidu.com:8000/") != nil { println("set url ok!") } //print("set url =>", ret.Setopt(curl.OPT_URL, "http://commondatastorage.googleapis.com/chromium-browser-continuous/Linux_x64/104547/chrome-linux.zip"), endl) print("set user_agent =>", ret.Setopt(curl.OPT_USERAGENT, "go-curl v0.0.1") == nil, endl) // add to DNS cache print("set resolve =>", ret.Setopt(curl.OPT_RESOLVE, []string{"www.baidu.com:8000:127.0.0.1"}) == nil, endl) // ret.EasyReset() clean seted // currently not finished! // fooTest := func(buf []byte, userdata interface{}) bool { // buf := ptr.([]byte) println("size=>", len(buf)) println("DEBUG(in callback)", buf, userdata) println("data = >", string(buf)) return true } ret.Setopt(curl.OPT_WRITEFUNCTION, fooTest) // curl.CallbackWriteFunction(fooTest)) println("set opt!") // for test only code := ret.Perform() // dump.Dump(code) fmt.Printf("code -> %v\n", code) println("================================") print("pause =>", ret.Pause(curl.PAUSE_ALL), endl) print("escape =>", ret.Escape("http://baidu.com/"), endl) print("unescape =>", ret.Unescape("http://baidu.com/-%00-%5c"), endl) print("unescape lenght =>", len(ret.Unescape("http://baidu.com/-%00-%5c")), endl) // print("version info data =>", curl.VersionInfo(1), endl) ver := curl.VersionInfo(curl.VERSION_NOW) fmt.Printf("VersionInfo: Age: %d, Version:%s, Host:%s, Features:%d, SslVer: %s, LibzV: %s, ssh: %s\n", ver.Age, ver.Version, ver.Host, ver.Features, ver.SslVersion, ver.LibzVersion, ver.LibsshVersion) print("Protocols:") for _, p := range ver.Protocols { print(p, ", ") } print(endl) println(curl.Getdate("20111002 15:05:58 +0800").String()) ret.Getinfo(curl.INFO_EFFECTIVE_URL) ret.Getinfo(curl.INFO_RESPONSE_CODE) ret.Getinfo(curl.INFO_FILETIME) ret.Getinfo(curl.INFO_SSL_ENGINES) ret.Getinfo(curl.INFO_TOTAL_TIME) println("================================") // ret.Getinfo(curl.INFO_SSL_ENGINES) /* mret := curl.MultiInit() mret.AddHandle(ret) // works defer mret.Cleanup() if ok, handles := mret.Perform(); ok == curl.OK { fmt.Printf("ok=%s, handles=%d\n", ok, handles) } else { fmt.Printf("error calling multi\n") } */ println("================================") //println(curl.GlobalInit(curl.GLOBAL_SSL)) }