【问题标题】:Use pycurl to get response from server使用 pycurl 从服务器获取响应
【发布时间】:2015-06-17 11:35:25
【问题描述】:
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) . . . .

这只是错误的快照。

我该如何解决?

【问题讨论】:

  • 我有点困惑;看起来您已经粘贴了 Python 代码,而是一个 java stacktrace。你能提供更多的上下文吗?更新:将该 URL 发布到浏览器(我意识到这将执行 GET 请求)后,我看到了类似的输出。输出不是来自 your 程序的堆栈跟踪,而是来自服务器的输出。所以请求中的某些内容需要有所不同。
  • 异常的开头说什么?这是重要的部分,而不是结束。
  • 网络服务器好像有bug。

标签: python curl dbpedia pycurl spotlight-dbpedia


【解决方案1】:

你可以试试这段代码..

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多