美文网首页
go编译dll给python调用

go编译dll给python调用

作者: ArtioL | 来源:发表于2022-02-07 21:34 被阅读0次

    go

    package main
    
    //go build -buildmode=c-shared -o exportgo.dll exportgo.go
    
    import (
        "fmt"
        "github.com/Danny-Dasilva/CycleTLS/cycletls"
        "log"
        "C"
    )
    
    //export test_send
    func test_send(Input *C.char ) *C.char{
    
        client := cycletls.Init()
        cookies := []cycletls.Cookie{
            {
                Name:  "x-s3-s4e",
                Value: "zadjb0rbZt1AmPFV2F4v3osOhIBLcCyjvawLXmKaGFVoYAv9W9bb0oXOgJAmteo7J0x0m6pvLhYhqYIeGkfRjldZ%2FJF%2FBXv5pZDGVegG6pQYeu94ShxC39X8a%2B1iZehNo%2FMpy5P0AvzyrE%2FWxvU4VIackP3o0M3LEH7rapxPp809sPWwo7pws4fURkIGAZ%2Bq2WAjyTFXQy3gN1jm5GySwzqxiYgG4mTfIS4%2B%2BaNxmvGk7vm2E%2FzeeYctV2uzk%2BQHrTJBzcEbGRJ2DMIK19THtqRBN7rNpotYr25P%2BHCwSrgu67e3NCU8Y1Q2zqMvZiez3BzkFaf1Cejw0dk2kybgHUufAVsxDOsEPFIT2lTLtuOBhQlwO1RI5Ddv2iqbuxhjD4gVaFIVeePB51lQTn%2FBxAVLUTDFqiQgGl%2FnPLceVbgaprpWdSIMh3UkmukmOcyHVaRbtSYvOHBoF5g3ooGkpAMV2NWJDoT9klLL5WAQurRZ3zeAvHUPoLOkKKkNVO66lYgcca5BOaTXYzwNA1%2Fj1CmoLjY%2Bk3FWcULL%2FUEba92VRakT2F59OcoNtSzSgKs4GGbuo4eMI8u935AxSCVEQ9gElg3ZHsNnJh%2FbAqPkTVRZ4WodW15T2iVJoM0D%2F8tx7xlOkGJHGQ756bCuJ3SkavDxO4NQONMktHD%2F6TV%2BO5zr9QnkJXSnKS1hTO%2FBfONddnykstXwJPvwURxAYSR4QzVlXrs3MDFp0r6faHfiBDQmPTx95nO48Jg1De%2FVsAKXYkjtunp1ch3RZJTF9ql7K30B11RSRcWNDNwyaOzilog%3D3sSs733068ccea8b64a4c806a027a82348d13a8021f7:b6:a1a50f24-6e16-11ec-9740-005056b92763:06f0422470",
            },
            {
                Name:  "x-s3-sid",
                Value: "S1wdffw1byRMh14413oc1qg3a",
            },
        }
        url := C.GoString(Input)
        fmt.Println(url)
        response, err := client.Do(url, cycletls.Options{
            Ja3:       "771,47802-4865-4866-4867-49195-49199-49196-49200-52393-52392-49171-49172-156-157-47-53,10794-0-23-65281-10-11-35-16-5-13-18-51-45-43-27-17513-6682-21,39578-29-23-24,0",
            UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36",
            Headers: map[string]string{
                "Connection":      "keep-alive",
                "Pragma":          "no-cache",
                "Accept":          "application/json, text/plain, */*",
                "Cache-Control":   "must-revalidate",
                "Accept-Language": "zh_CN",
                "User-Agent":      "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36",
                "Expires":         "0",
                "Referer":         "http://www.9air.com/zh-CN/book/booking",
            },
            Cookies:         cookies,
            Method:          "GET",
            Timeout:         3,
            DisableRedirect: false,
        }, "GET")
        if err != nil {
            log.Print("Request Failed: " + err.Error())
            return  C.CString("")
        }
        //log.Println("123")
        //log.Println(response.Response.Body)
        //log.Println(response.Response.Status)
        return C.CString(response.Response.Body)
    }
    
    
    func main(){
    
    }
    

    编译成dll动态库

    go build -buildmode=c-shared -o exportgo.dll exportgo.go
    

    将生成的dll拷贝到python目录下

    python调用代码

    from ctypes import CDLL
    import ctypes
    
    
    pDll = CDLL("./exportgo.dll")
    
    
    # 指定参数类型
    pDll.test_send.argtypes = [ctypes.c_char_p]
    # 指定返回值类型
    pDll.test_send.restype = ctypes.c_char_p
    
    #调用动态链接库函数
    res = pDll.test_send(b"http://www.9air.com/shop/api/shopping/b2c/searchflight?language=zh_CN&currency=CNY&flightCondition=index:0;depCode:CAN;arrCode:SHA;depDate:2022-01-20;depCodeType:CITY;arrCodeType:CITY;&channelNo=B2C&tripType=OW&groupIndicator=I&adultCount=1&childCount=0&infantCount=0&airlineCode=&directType=&cabinClass=&taxFee=&taxCurrency=&promotionCode=")
    #打印返回结果
    print(res)
    

    相关文章

      网友评论

          本文标题:go编译dll给python调用

          本文链接:https://www.haomeiwen.com/subject/lmykcrtx.html