1

我正在尝试使用 xmlrpclib python 的库来执行 XMP-RPC,但我得到了

xml.parsers.expat.ExpatError: XML or text declaration not at start of entity: line 1, column 1

这是带有参数的字典:

{'cliente_chave': 'hUi238sh328sjk37Hms8Kisjeg9',
     'cliente_codigo': 164,
     'imovel_bairro': u'Renascen\xe7a',
     'imovel_banheiros': u'5',
     'imovel_cidade': u'S\xe3o Lu\xeds - MA',
     'imovel_codigo_imobiliaria': u'757',
     'imovel_descricao': u'\n\t\t\t\n\t\t\tCasa em otima localiza\xe7\xe3o ideal tanto para empresas quanto para moradia tendo:\r\nTerra\xe7o\r\nSala Ampla para 03 ambientes\r\nLavabo\r\nHall\r\n03 su\xedtes\r\nEscritorio\r\nCozinha\r\nQuintaL\r\n\xc1rea de Servi\xe7o\r\nDCE\r\nGaragem coberta para 02 carros\r\nPort\xe3o eletronico\r\nCisterna\r\nNascente',
     'imovel_estado': 'MA',
     'imovel_negociacao': 'venda',
     'imovel_novo': False,
     'imovel_quartos': u'3',
     'imovel_subtipo': u'padr\xe3o',
     'imovel_suites': u'3',
     'imovel_tipo': u'casa',
     'imovel_vagas': u'2',
     'imovel_valor': u' 480.000,00',
     'url': 'http://www.estiloma.com.br/imoveis/para-venda/em-sao-luis/no-bairro-renascenca/casa-padrao/id-774.html'}

这是异常的回溯:

Traceback (most recent call last):
      File "/Library/Python/2.7/site-packages/Twisted-12.2.0-py2.7-macosx-10.8-intel.egg/twisted/internet/defer.py", line 551, in _runCallbacks
        current.result = callback(current.result, *args, **kw)
      File "/Users/bslima/Documents/Aptana Studio 3 Workspace/aqueleimovel/aqueleimovel/pipelines.py", line 31, in process_item
        result = server.salvar_imovel(dict(item))
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xmlrpclib.py", line 1224, in __call__
        return self.__send(self.__name, args)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xmlrpclib.py", line 1575, in __request
        verbose=self.__verbose
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xmlrpclib.py", line 1264, in request
        return self.single_request(host, handler, request_body, verbose)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xmlrpclib.py", line 1297, in single_request
        return self.parse_response(response)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xmlrpclib.py", line 1467, in parse_response
        p.feed(data)
      File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/xmlrpclib.py", line 557, in feed
        self._parser.Parse(data, 0)
    xml.parsers.expat.ExpatError: XML or text declaration not at start of entity: line 1, column 1

我只对字符串使用 UTF-8,如果没有运气,我会尝试编码或解码为 UTF-8。我也尝试用谷歌搜索,但没有成功。有人见过这个吗?

哦,这是客户端代码:

server = ServerProxy(uri=settings.RPC_SERVER, verbose=True)
server.salvar_imovel(item)

我在这里先向您的帮助表示感谢。

正如我评论的那样,ExpatError 在响应对象中。谢谢

4

2 回答 2

5

[已解决] 响应在开头发送了空格。我不得不从 xmlrpc 扩展 Transport 类并简单地剥离响应。

from xmlrpc.client import SafeTransport, Transport
from xmlrpc.client import GzipDecodedResponse

is_https = False
if is_https:
    BaseTransport = SafeTransport
else:
    BaseTransport = Transport

class CustomTransport(BaseTransport):
    def parse_response(self, response):
        # read response data from httpresponse, and parse it

        # Check for new http response object, else it is a file object
        if hasattr(response,'getheader'):
            if response.getheader("Content-Encoding", "") == "gzip":
                stream = GzipDecodedResponse(response)
            else:
                stream = response
        else:
            stream = response

        p, u = self.getparser()

        while 1:
            data = stream.read(1024)
            data = data.strip()
            if not data:
                break
            if self.verbose:
                print "body:", repr(data)
            p.feed(data)

        if stream is not response:
            stream.close()
        p.close()

        return u.close()
于 2013-03-05T02:24:43.457 回答
1

您似乎将对象传递dict给期望的对象XML-这条线

server.salvar_imovel(dict(item))

如果你真的必须传入dict,你可以尝试序列化第dict一个。这里有一些示例代码。

于 2013-03-03T22:45:59.567 回答