1

我正在尝试使用 pycurl 与服务器交互,但出现 500 错误。我希望能够看到从服务器返回的错误文本,但 pycurl 似乎没有给我一种方法来做到这一点。

例如,当我将“curl”指向服务器时,我会得到如下信息:

$ curl -H "Content-Type: text/xml; charset=utf-8" -k -d @subcase.xml -X POST https://192.168.1.110/vision.asmx
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:Body>
    <soap:Fault>
      <faultcode>soap:Server</faultcode>
      <detail>
        <ExceptionMessage>Invalid Case Objid -1 or idNumber 76029744 or Case is closed.</ExceptionMessage>
      </detail>
    </soap:Fault>
  </soap:Body>
</soap:Envelope>

但是当我尝试构建一些做同样事情的 pycurl 时,从服务器返回的 HTML 似乎被吞没了:

class Storage:
    '''An object used to hold the output of a pyCurl command'''
    def __init__(self):
        self.contents = []
    def store(self, buf):
        self.contents.append(buf)
    def __str__(self):
        return '\n'.join(self.contents)

def go_get_it(url):
    curl_obj = pycurl.Curl()
    curl_obj.setopt(pycurl.URL, str(url))

    output = Storage()
    header = Storage()
    curl_obj.setopt(pycurl.WRITEFUNCTION, output.store)
    curl_obj.setopt(pycurl.HEADERFUNCTION, header.store)

    response = Response(header, output)
    if self.debug_level:
        curl_obj.setopt(pycurl.VERBOSE, 1)
        curl_obj.setopt(pycurl.DEBUGFUNCTION, self.curl_debug)
    try:
        curl_obj.perform()
        status_code = curl_obj.getinfo(pycurl.HTTP_CODE)
        response.set_status_code(status_code)
        return response
    except pycurl.error, curl_err:
        print curl_obj.errstr()

如果服务器没有抛出 500 错误,则该output对象包含来自服务器的 HTML 响应。但是,当我们得到异常时,输出对象是空的。从服务器返回的 HTML 也不可用curl_err(据我所知)。

有谁知道在这种情况下是否有办法恢复从服务器返回的 HTML?

4

0 回答 0