2013-03-03 39 views
1

我想使用的xmlrpclib蟒蛇的lib做XMP-RPC,但我越來越XML-RPC客戶端蟒提高xml.parsers.expat.ExpatError例外

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字符串

,我已經TR如果沒有運氣,可以編碼或解碼爲UTF-8。 我也嘗試谷歌它,但沒有sucsess。有人看到了嗎?

哇,這裏的客戶端代碼:

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

感謝提前的幫助。

正如我評論,ExpatError是在響應對象。由於

+0

我注意到在回溯中,問題出在響應中,而不是在請求中。我會調查迴應,看看發生了什麼。謝謝@danodonovan – bslima 2013-03-03 22:53:10

回答

1

你似乎在傳遞一個dict對象的東西期待XML - 這條線

server.salvar_imovel(dict(item)) 

如果你真的必須傳中dict,你可以嘗試序列化dict第一。有一些示例代碼here

+0

我注意到在回溯中,問題出在響應中,而不是在請求中。我會調查迴應,看看發生了什麼。謝謝@danodonovan – bslima 2013-03-05 02:23:25

4

[已解決]響應是在開頭用空格發送的。 我不得不從xmlrpc擴展Transport類,並簡單地刪除響應。

class CustomTransport(Transport): 
    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() 
+1

小備註:要調用HTTPS服務器上的方法,我必須通過SafeTransport而不是Transport,否則調用的是http:// URL,而不是https://。要使用它,不要忘記從xmlrpclib導入Transport(或SafeTransport)和GzipDecodedResponse。在我的情況下,這允許我在Drupal XML-RPC響應開始時去除額外的「\ n」。 – FGM 2014-08-29 10:15:51