Help with cURL in Python

后端 未结 4 1514
-上瘾入骨i
-上瘾入骨i 2021-01-07 05:05

I have to POST a request to a server. In the API documentation of the website there is this example that uses cURL in PHP:

$ch = curl_init();
curl_setopt($ch         


        
4条回答
  •  北海茫月
    2021-01-07 05:43

    curl is for Python too: http://pycurl.sourceforge.net/

    The example could be translated into Python and pycurl like this:

    import pycurl
    c = pycurl.Curl()
    c.setopt(pycurl.URL, "http://api.website.com")
    c.setopt(pycurl.POST, 1)
    c.setopt(pycurl.POSTFIELDS, "request=%s" % wrapper)
    import StringIO
    b = StringIO.StringIO()
    c.setopt(pycurl.WRITEFUNCTION, b.write)
    c.perform()
    c.close()
    data = b.getvalue()
    

    Your Python code using urllib2 looks OK, it should be working. Probably there is an error in something other you did not mention in question; could you be please more specific?

提交回复
热议问题