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

Anil Kumar
  • 93
  • 1
  • 17
  • 2
    Do you really mean MSDOS, the OS that run in less than 1MB ? – Panagiotis Kanavos May 14 '19 at 12:14
  • Here, MSDOS means Windows Command Prompt. – Anil Kumar May 14 '19 at 12:15
  • 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 – Panagiotis Kanavos May 14 '19 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. – Panagiotis Kanavos May 14 '19 at 12:18
  • Ohk, got your point. Editing the question. tnx – Anil Kumar May 14 '19 at 12:18
  • Windows 10 has [a Linux subsystem](https://learn.microsoft.com/en-us/windows/wsl/install-win10) that you can use to test code without moving to another machine or starting a VM. – Panagiotis Kanavos May 14 '19 at 12:19
  • hmm, with due respect, I think you are not reading my question. I have given a link of another thread that calls RabbitMQ Api and result is being passed to this function. – Anil Kumar May 14 '19 at 12:21
  • 1
    Post your question's code *here*. Don't force people to run previous questions just to reproduce the problem – Panagiotis Kanavos May 14 '19 at 12:23
  • 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 [mcve] guidelines, and the precise text of the related close reason. – Charles Duffy May 14 '19 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. – Charles Duffy May 14 '19 at 12:25
  • Ok, I was thinking that copying the same thing will violate guidelines. Editing the question again... thanks for guiding. – Anil Kumar May 14 '19 at 12:26
  • What is the value of res.read()? – Frontear May 14 '19 at 12:47
  • It is the message containing all properties of RabbitMQ message. Like:- [{'payload_bytes': 1, 'redelivered': True, 'exchange': '', 'routing_key': 'myQueueName', 'message_count': 1, 'properties': {'delivery_mode': 2, 'headers': {'apInformation': 'Details available in file:abcFileName.xml', 'attachmentFiles': 'abcEncoded_attachment_0.pdf',........ }] – Anil Kumar May 14 '19 at 12:52
  • @Yossi your mentioned link is totally different than mine problem. It's not duplicate one. tnx – Anil Kumar May 14 '19 at 13:07
  • @Yossi how you consider it as duplicate? Requesting you to not be so cruel on the basis of assumptions. – Anil Kumar May 14 '19 at 14:17
  • What's the specific Python version in the environment where it works? What's the specific Python version in the runtime where it fails? – Charles Duffy May 14 '19 at 16:54
  • @AnilKumar, frankly, that *does* look like a duplicate to me, because the str-vs-bytes distinction and the mechanism to convert between those types is the same no matter what the context -- it isn't specific to JSON, or to RabbitMQ, or otherwise. It *does* depend on your Python version, but because you aren't providing that information in the question, adverse inferences can reasonably be made. – Charles Duffy May 14 '19 at 16:56
  • @CharlesDuffy I checked the python version in both environment and they are differ. You are right, the same function is not complaining in python3 but in python2. You are super! Thank you very much. – Anil Kumar May 15 '19 at 08:16
  • @CharlesDuffy I have provided every details needed. Agree with you that this problem has nothing to do with RabbitMQ and same is mentioned in question also. My question says I get error in a one environment and that is too in post processing i.e. print_out. – Anil Kumar May 15 '19 at 08:18

1 Answers1

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

Anil Kumar
  • 93
  • 1
  • 17