-1

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()))

19
  • 2
    Do you really mean MSDOS, the OS that run in less than 1MB ? Commented May 14, 2019 at 12:14
  • 1
    Don't use that name then. The command prompt is just the command prompt. It doesn't host applications nor does it provide any kind of runtime environment. What you mean is that you executed a Python script on Windows Commented May 14, 2019 at 12:17
  • 1
    How did you run the script and how did you pass the JSON string to that method? The error complains that the input is a byte buffer instead of a string. Commented May 14, 2019 at 12:18
  • 3
    Questions are required to be self-contained. Informational links are allowed, but if a question can't be answered (which often implies: "If a problem can't be reproduced") using only information given directly in it without following any links, it's eligible to be closed as incomplete. See minimal reproducible example guidelines, and the precise text of the related close reason. Commented May 14, 2019 at 12:23
  • 2
    Frankly, the str-vs-bytes distinction isn't JSON-specific at all. It's just as likely to be that you have two different Python versions (one python2, the other python3 f/e) in your two environments. Commented May 14, 2019 at 12:25

1 Answer 1

1

So, it was python version specific error and has nothing to do with Environment. For executing my script I am using python.exe (which takes me to python3) instead of only python in Bash on Ubuntu on Windows. Credit goes to Charles Duffy as he pointed: -

Frankly, the str-vs-bytes distinction isn't JSON-specific at all. It's just as likely to be that you have two different Python versions (one python2, the other python3 f/e) in your two environments. – Charles Duffy

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.