繁体   English   中英

使用pycurl从服务器获取响应

[英]Use pycurl to get response from server

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")

该程序如上所述,但我无法从服务器得到正确的响应。 错误是:)

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)。

这只是错误的快照。

我该如何解决?

你可以尝试这个代码..

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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM