2010-10-08 7 views
16
>> 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 

이 오류의 원인을 파악할 수 없습니다.Python - urllib2의 unhashable type 오류

답변

35

data은 "표준 응용 프로그램/x-www-form-urlencoded 형식의 버퍼"라고 가정합니다.

데이터 사전을 data = urllib.urlencode(data)에 전달하기 전에 사용자의 dict에서 올바른 형식을 얻을 수 있습니다.

4

json을 사용하는 경우 다음을 사용할 수도 있습니다.

json.dumps(data) 

주의 사항 : urlencode는 dict은 인코딩 할 수 있지만 문자열은 인코딩 할 수 없습니다. json.dumps의 출력은 문자열입니다.