When I run the python script from Windows command prompt it works perfectly but when it is run from Ubuntu throws errors which says, "the JSON object must be str, not 'bytes'".
It is quite puzzling why the same input (result from the RabbitMQ API call) is being treated differently while calling function "print_out".
Below is the code snippet of python script: -
import urllib.request, urllib.error, urllib.parse, requests
import json, optparse
class http_worker:
def authentication(self, url, user, pw):
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, url, user, pw)
self.auth = urllib2.HTTPBasicAuthHandler(password_manager)
opener = urllib2.build_opener(self.auth)
urllib2.install_opener(opener)
def call_url(self, url, body_raw):
body = json.dumps(body_raw)
#
# urllib2 post since there is body
#
req = urllib2.Request(url, body, {'Content-Type': 'application/json'})
return urllib2.urlopen(req)
# THIS FUNCTION CALL IS THROWING ERROR
def print_out(my_json):
for item in my_json:
out = []
for _, val in sorted(item.get("properties").get("headers").items()):
out.append(str(val))
print(", ".join(out))
user = "guest"
pwd = "guest"
rabbit_host = "http://localhost:15672"
host_suffix = "/api/queues/%%2F/%s/get" %(rabbit_queue_name)
url = rabbit_host + host_suffix
body_raw = {"count":5000,"ackmode":"ack_requeue_false", "encoding":"auto","truncate":50000}
worker = http_worker()
worker.authentication(url, user, pwd)
res = worker.call_url(url, body_raw)
#result = json.loads(res.read())
print_out(json.loads(res.read()))