簡體   English   中英

Python - urllib2中不可用的類型錯誤

[英]Python - unhashable type error in urllib2

>> url = 'https://test.authorize.net/gateway/transact.dll'
>> data = {'x_login': 'abc123', 'x_type': 'AUTH_CAPTURE', 'x_card_num': '4444333322221103', 'x_amount': '50.75', 'x_tran_key
': 'abc123', 'x_version': '3.1', 'x_delim_char': '|', 'x_exp_date': '022012', 'x_delim_data': 'TRUE'}
>> 
>> urllib2.urlopen(url, data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "gateways\base.py", line 81, in dispatch
    return gw_method(self, *args, **kwargs)
  File "gateways\decorators.py", line 17, in wrapper
    method(*args, **kwargs)
  File "gateways\authorize_net.py", line 39, in auth_capture
    return self.post_data(data)
  File "gateways\authorize_net.py", line 43, in post_data
    raw_response = urllib2.urlopen(self.get_endpoint(), data)
  File "C:\Python26\lib\urllib2.py", line 124, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python26\lib\urllib2.py", line 389, in open
    response = self._open(req, data)
  File "C:\Python26\lib\urllib2.py", line 407, in _open
    '_open', req)
  File "C:\Python26\lib\urllib2.py", line 367, in _call_chain
    result = func(*args)
  File "C:\Python26\lib\urllib2.py", line 1154, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "C:\Python26\lib\urllib2.py", line 1118, in do_open
    h.request(req.get_method(), req.get_selector(), req.data, headers)
  File "C:\Python26\lib\httplib.py", line 898, in request
    self._send_request(method, url, body, headers)
  File "C:\Python26\lib\httplib.py", line 938, in _send_request
    self.send(body)
  File "C:\Python26\lib\httplib.py", line 743, in send
    self.sock.sendall(str)
  File "C:\Python26\lib\ssl.py", line 203, in sendall
    v = self.send(data[count:])
TypeError: unhashable type

我無法弄清楚導致此錯誤的原因。

data被假定為“標准應用程序/ x-www-form-urlencoded格式的緩沖區”,而不是dict。

在do data = urllib.urlencode(data)傳遞數據dict之前,您可以從dict獲得正確的格式。

如果您使用的是json,也可以使用以下內容。

json.dumps(data)

請記住:urlencode可以編碼dict,但不能編碼字符串。 json.dumps的輸出是一個字符串。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM