Use pycurl to get response from server

5.9k views Asked by At
def curlDBPedia(DB_url):
    data = json.dumps({"text":"President Obama called Wednesday on Congress to extend a tax break for students included in last year's economic stimulus package, arguing that the policy provides more generous assistance.",
    "confidence": "0.2", "support": "20"
    })
    c = pycurl.Curl()
    c.setopt(pycurl.URL, DB_url)
    c.setopt(pycurl.POST, 1)
    c.setopt(pycurl.POSTFIELDS, data)
    c.perform()
curlDBPedia("http://spotlight.dbpedia.org/rest/annotate")

The program is given as above, but I can not get correct response from the server. The error is: )

com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59) com.sun.grizzly.ContextTask.run(ContextTask.java:71) com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532) com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513) java.lang.Thread.run(Thread.java:701) . . . .

This is only a snapshot of the error.

How can I fix it?

1

There are 1 answers

0
byteC0de On

You could try this code ..

def curlDBPedia(DB_url):
        data = json.dumps({"text":"President Obama called Wednesday on Congress to extend a tax break for students included in last year's economic stimulus package, arguing that the policy provides more generous assistance.",
        "confidence": "0.2", "support": "20"
        })
        buffer = StringIO()
        c = pycurl.Curl()
        c.setopt(pycurl.URL, DB_url)
        c.setopt(pycurl.POST, 1)
        c.setopt(pycurl.POSTFIELDS, data)
        c.setopt(c.WRITEFUNCTION, buffer.write)
        c.perform()
        c.close()
        body = buffer.getvalue()#here we got the response data
    curlDBPedia("http://spotlight.dbpedia.org/rest/annotate")