如何在使用urllib2在python中发送json数据时避免rabbit mq中的错误映射

2024-06-01 07:05:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用urllib2将json数据发送到RMQ

 request = urllib2.Request('https://%s:%s/api/exchanges/../publish' % (broker_service, broker_mgmt_port))
                base64string = base64.b64encode('usr:pwd')
                request.add_header("Authorization", "Basic %s" % base64string)
                x = {'A':'b'}
                result = urllib2.urlopen(request, data=json.dumps(str(x), ensure_ascii=False), context=ssl._create_unverified_context())

在客户端,我得到500英镑

Traceback (most recent call last):
  File "test.py", line 57, in <module>
    result = urllib2.urlopen(request, data=json.dumps(str(x), ensure_ascii=False), context=ssl._create_unverified_context())
  File "/usr/lib64/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib64/python2.7/urllib2.py", line 437, in open
    response = meth(req, response)
  File "/usr/lib64/python2.7/urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib64/python2.7/urllib2.py", line 475, in error
    return self._call_chain(*args)
  File "/usr/lib64/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.7/urllib2.py", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 500: Internal Server Error

在检查服务器日志时,我得到

2019-09-14 23:18:24.465 [error] <0.29773.18> CRASH REPORT Process <0.29773.18> with 0 neighbours crashed with reason: {{badmap,<<"{'A': 'b'}">>},[{maps,fold,[#Fun<rabbit_mgmt_util.23.39948334>,<<"{'A': 'b'}">>,<<"{'A': 'b'}">>],[{file,"maps.erl"},{line,221}]},{rabbit_mgmt_util,decode,2,[{file,"src/rabbit_mgmt_util.erl"},{line,739}]},{rabbit_mgmt_util,with_decode,5,[{file,"src/rabbit_mgmt_util.erl"},{line,727}]},{rabbit_mgmt_wm_exchange_publish,accept_content,2,[{file,"src/rabbit_mgmt_wm_exchange_publish.erl"},{line,51}]},{cowboy_rest,call,3,[{file,"src/cowboy_rest.erl"},{line,1576}]},{cowboy_rest,process_content_type,...},...]}
2019-09-14 23:18:24.466 [error] <0.29772.18> Ranch listener rabbit_web_dispatch_sup_9450, connection process <0.29772.18>, stream 1 had its request process <0.29773.18> exit with reason {badmap,<<"{'A': 'b'}">>} and stacktrace [{maps,fold,[#Fun<rabbit_mgmt_util.23.39948334>,<<"{'A': 'b'}">>,<<"{'A': 'b'}">>],[{file,"maps.erl"},{line,221}]},{rabbit_mgmt_util,decode,2,[{file,"src/rabbit_mgmt_util.erl"},{line,739}]},{rabbit_mgmt_util,with_decode,5,[{file,"src/rabbit_mgmt_util.erl"},{line,727}]},{rabbit_mgmt_wm_exchange_publish,accept_content,2,[{file,"src/rabbit_mgmt_wm_exchange_publish.erl"},{line,51}]},{cowboy_rest,call,3,[{file,"src/cowboy_rest.erl"},{line,1576}]},{cowboy_rest,process_content_type,3,[{file,"src/cowboy_rest.erl"},{line,1100}]},{cowboy_rest,upgrade,4,[{file,"src/cowboy_rest.erl"},{line,288}]},{cowboy_stream_h,execute,3,[{file,"src/cowboy_stream_h.erl"},{line,296}]}]

如果删除json.dumps中的str(),则会出现以下错误

Traceback (most recent call last):
  File "test.py", line 57, in <module>
    result = urllib2.urlopen(request, data=json.dumps(x, ensure_ascii=False), context=ssl._create_unverified_context())
  File "/usr/lib64/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib64/python2.7/urllib2.py", line 437, in open
    response = meth(req, response)
  File "/usr/lib64/python2.7/urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib64/python2.7/urllib2.py", line 475, in error
    return self._call_chain(*args)
  File "/usr/lib64/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.7/urllib2.py", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 400: Bad Request

我需要如何/以什么格式将json发送到RMQ以避免错误映射


Tags: inpysrcrestrequestusrlineurllib2