pycurl - Serbipunk/notes GitHub Wiki

install

ubuntu

https://stackoverflow.com/a/69074383

sudo apt install libcurl4-openssl-dev libssl-dev
pip install pycurl

windows

https://stackoverflow.com/a/47134766

https://www.lfd.uci.edu/~gohlke/pythonlibs/#pycurl

ts

[curl: (60) SSL certificate problem]

Use cURL with -k option which allows curl to make insecure connections, that is cURL does not verify the certificate.

https://stackoverflow.com/a/24618403

examples

file upload

def upload():
    c = pycurl.Curl()

    c.setopt(pycurl.VERBOSE, False)

    c.setopt(pycurl.HTTPHEADER, ['Accept-Language: en'])
    c.setopt(c.POST, 1)

    c.setopt(c.HTTPPOST, [
        # ("pic", (c.FORM_FILE, "data/cloth.json")),
        ("pic", (c.FORM_FILE, r"E:\local_s\COLLATION_PHOTOS\251\DFT_CLOTH_20220810_007\56+1\deformation_config\cloth.json")),
        ("path", "/data/images/srv/ceph_fs/hmy_local_data/COLLATION_PHOTOS/251/DFT_CLOTH_20220810_007/56+1/deformation_config/cloth_upload_py.json"),
        ("user_id", "240"),
        ("key", "98148e13b05af5c2fda54023f28764ffc969fc40"),
    ])

    c.setopt(pycurl.URL, "https://pmapi.xiangyiai.cn/collocation_fitting_room/collocationSkuStyle/upload_skuStyle_pic/")

    c.setopt(pycurl.SSL_VERIFYPEER, 0)  # python2需要加上这段类似varify=False的参数
    c.setopt(pycurl.SSL_VERIFYHOST, 0)

    c.setopt(pycurl.CONNECTTIMEOUT, 5)
    http_code = c.getinfo_raw(pycurl.HTTP_CODE)
    c.perform()
    c.close()

    print("http_code: {http_code}".format(http_code=http_code))

basic file download

def download(arg):
    url, path = arg
    if os.path.isfile(path):  # if download success
        return
    http_code = 0
    try:
        with open(path, "wb") as f:
            curl = pycurl.Curl()
            curl.setopt(pycurl.URL, url)
            curl.setopt(pycurl.WRITEDATA, f)
            curl.setopt(pycurl.CONNECTTIMEOUT, 5)
            curl.setopt(pycurl.TIMEOUT, 8)
            curl.setopt(pycurl.VERBOSE, False)
            curl.perform()
            http_code = curl.getinfo_raw(pycurl.HTTP_CODE)
            curl.close()
    except Exception as e:
        logger.error(e)
        logger.error(traceback.format_exc())
        return
    if http_code // 100 != 2:
        logger.error(f"http_code: {http_code}")
        return
    return

https://web.archive.org/web/20181230101555/https://www.angryobjects.com/2011/10/15/http-with-python-pycurl-by-example/

http://pycurl.io/docs/latest/quickstart.html