I am new to python, I am creating a websocket with pyhton as server and javascript as client, I got them to communicate but I cannot pass very extensive data because it throws me this error when the python function receives and tries to decode the bytes and the PAYLOAD_LENGTH.
This is my function that treats the received bytes
def parse_frame(self):
data = self.conn.recv(2)
FIN = (data[0] >> 7) & 1
RSV1 = (data[0] >> 6) & 1
RSV2 = (data[0] >> 5) & 1
RSV3 = (data[0] >> 4) & 1
OPCODE = data[0] & 0xf
MASK = (data[1] >> 7) & 1
if not MASK:
self.close_handshake(1002)
#print(data[1])
PAYLOAD_LENGTH = data[1] & 0x7f
#print(PAYLOAD_LENGTH)
if PAYLOAD_LENGTH == 126:
data = self.conn.recv(2)
PAYLOAD_LENGTH = struct.unpack('B', data)[0]
elif PAYLOAD_LENGTH == 127:
data = self.conn.recv(8)
print(data)
PAYLOAD_LENGTH += struct.unpack('B', data)[0]
MASKING_KEY = self.conn.recv(4)
PAYLOAD_DATA = self.conn.recv(PAYLOAD_LENGTH)
PAYLOAD_DATA = self.unmask(PAYLOAD_DATA, MASKING_KEY)
return OPCODE, PAYLOAD_DATA
Waiting connection at port 5000
1
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\digit\AppData\Local\Programs\Python\Python39\lib\threading.py", line 973, in _bootstrap_inner
self.run()
File "C:\xampp\htdocs\digitalife\token\server\wsprotocol\server.py", line 359, in run
self.data_transfer()
File "C:\xampp\htdocs\digitalife\token\server\wsprotocol\server.py", line 226, in data_transfer
OPCODE, PAYLOAD_DATA = self.parse_frame()
File "C:\xampp\htdocs\digitalife\token\server\wsprotocol\server.py", line 276, in parse_frame
PAYLOAD_LENGTH = struct.unpack('B', data)[0]
struct.error: unpack requires a buffer of 1 bytes
Can someone tell me how to solve this error?
Related
My Device Address is 1, I tried Read Holding Register, and Register Address is 0.
I tried pyserial to communicate Modbus Device but pyserial this is my code:
import serial,time
ser = serial.Serial(port='/dev/ttyUSB2')
while True:
val = ser.read(b'\x01\x03\x00')
print(val)
This is my error:
Traceback (most recent call last):
File "modbus.py", line 6, in <module>
val = ser.read(b'\x01\x03\x00') #Slave Address = 1, RTU Function number = 3 = Read Holding Registers, Register Address = 0
File "/home/pi/PythonDeneme/Venv/lib/python3.7/site-packages/serial/serialposix.py", line 481, in read
while len(read) < size:
TypeError: '<' not supported between instances of 'int' and 'bytes'
I also tried minimalmodbus to communicate:
import minimalmodbus
instrument = minimalmodbus.Instrument('/dev/ttyUSB2', 1) # port name, slave address (in decimal)
## Read temperature (PV = ProcessValue) ##
temperature = instrument.read_register(0, 1) # Registernumber, number of decimals
print(temperature)
And i also got this error:
Traceback (most recent call last):
File "modbus.py", line 15, in <module>
temperature = instrument.read_register(0, 1) # Registernumber, number of decimals
File "/home/pi/PythonDeneme/Venv/lib/python3.7/site-packages/minimalmodbus.py", line 447, in read_register
payloadformat=_PAYLOADFORMAT_REGISTER,
File "/home/pi/PythonDeneme/Venv/lib/python3.7/site-packages/minimalmodbus.py", line 1170, in _generic_command
payload_from_slave = self._perform_command(functioncode, payload_to_slave)
File "/home/pi/PythonDeneme/Venv/lib/python3.7/site-packages/minimalmodbus.py", line 1240, in _perform_command
response = self._communicate(request, number_of_bytes_to_read)
File "/home/pi/PythonDeneme/Venv/lib/python3.7/site-packages/minimalmodbus.py", line 1406, in _communicate
raise NoResponseError("No communication with the instrument (no answer)")
minimalmodbus.NoResponseError: No communication with the instrument (no answer)
The read() method takes an integer for the number of bytes to read.
Did you mean:
while True:
ser.write(b'\x01\x03\x00')
val = ser.read()
print(val)
I have pretty straightforward Python code which queries data from memcached:
import memcache
client = memcache.Client([('127.0.0.1', 11211)])
res = client.get("data_e")
data_e key exists, is not null and I am able to get results via telnet connection to memcached.
However when Python script is executed I keep receiving following error:
Traceback (most recent call last):
File "./position_expiration.py", line 5, in <module>
res = client.get("data_e")
File "/usr/lib/python2.7/site-packages/memcache.py", line 1129, in get
return self._get('get', key)
File "/usr/lib/python2.7/site-packages/memcache.py", line 1113, in _get
return _unsafe_get()
File "/usr/lib/python2.7/site-packages/memcache.py", line 1101, in _unsafe_get
value = self._recv_value(server, flags, rlen)
File "/usr/lib/python2.7/site-packages/memcache.py", line 1294, in _recv_value
raise ValueError('Unknown flags on get: %x' % flags)
ValueError: Unknown flags on get: 20
Data in memcached is stored by third party service, so I can't change a way how it is written there. What can I do from client side with Python to read it?
I have solved this problem.
This is the solution in Chinese: https://www.cnblogs.com/LanTianYou/p/9204431.html
The solution is to modify the final else-block content in the method "_recv_value" in the python file "memcache.py".
The _recv_value method should be like this:
def _recv_value(self, server, flags, rlen):
rlen += 2 # include \r\n
buf = server.recv(rlen)
if len(buf) != rlen:
raise _Error("received %d bytes when expecting %d"
% (len(buf), rlen))
if len(buf) == rlen:
buf = buf[:-2] # strip \r\n
if flags & Client._FLAG_COMPRESSED:
buf = self.decompressor(buf)
flags &= ~Client._FLAG_COMPRESSED
if flags == 0:
# Bare bytes
val = buf
elif flags & Client._FLAG_TEXT:
val = buf.decode('utf-8')
elif flags & Client._FLAG_INTEGER:
val = int(buf)
elif flags & Client._FLAG_LONG:
if six.PY3:
val = int(buf)
else:
val = long(buf) # noqa: F821
elif flags & Client._FLAG_PICKLE:
try:
file = BytesIO(buf)
unpickler = self.unpickler(file)
if self.persistent_load:
unpickler.persistent_load = self.persistent_load
val = unpickler.load()
except Exception as e:
self.debuglog('Pickle error: %s\n' % e)
return None
else:
self.debuglog("unknown flags on get: %x\n" % flags)
# 注释掉这行
# raise ValueError('Unknown flags on get: %x' % flags)
# 设定返回值
val = buf
return val
i'm trying to create a layer 2 protocole for pratice. There are my imports (in both files) :
from pystack.layers.ethernet import EthernetProtocol
from pystack.layers.arp import ARPProtocol
from pystack.layers.ethernet import Ether
from scapytrame import EtherData
from scapy.all import *
It works perfectly when i do those lines in a file (Sender.py)
ethData = EtherData(src="00:21:70:a2:b7:6d", dst="d4:3d:7e:6c:66:e9", data="Kikouuu!", seq_ack=9)
sendp(ethData)
print ethData.show()
So i receive the packet and i can see it in WireShark
But now i want to do it by using a function and this doesn't work. There is the second file :
def sender(frames, macSrc="00:21:70:a2:b7:6d", iface="eth0") :
i = 0
sentBack = False
while(i < len(frames)):
# Sending packet
frames[i].seq_ack = hex(i%2);
frames[i].show()
Scapy.sendp(frames[i])
# Acknowledge + Next packet
macDst = frames[i].dst
filtre = "ether src " + macDst
ack = sniff(iface=str(iface), filter=str(filtre), count=1, timeout=5)
if not ack:
sentBack = True
continue
ackData = EtherData(str(ack[0]))
if int(ackData.seq_ack) == 2:
sentBack = False
i = i + 1
else:
continue
def receiver(macSrc="00:21:70:a2:b7:6d", iface="eth0") :
nextFrame = 0
while(True):
ack = sniff(iface=str(iface), filter=str(filtre), count=1)
ackData = EtherData(str(ack[0]))
if int(ackData.seq_ack) == nextFrame%2:
ackData.show()
sender([EtherData(src="00:21:70:a2:b7:6d", dst=ackData.dst, seq_ack=2)])
nextFrame = nextFrame + 1
frames_test = [ EtherData(src="00:21:70:a2:b7:6d", dst="d4:3d:7e:6c:66:e9", data="Kikouuu!", seq_ack=9),
EtherData(src="00:21:70:a2:b7:6d", dst="d4:3d:7e:6c:66:e9", data="Kikouuu!", seq_ack=9),
EtherData(src="00:21:70:a2:b7:6d", dst="d4:3d:7e:6c:66:e9", data="Kikouuu!", seq_ack=9),
EtherData(src="00:21:70:a2:b7:6d", dst="d4:3d:7e:6c:66:e9", data="Kikouuu!", seq_ack=9),
EtherData(src="00:21:70:a2:b7:6d", dst="d4:3d:7e:6c:66:e9", data="Kikouuu!", seq_ack=9)]
sender(frames_test)
Then i get an error as :
Traceback (most recent call last):
File "ProtAckPosAndTrans.py", line 47, in <module>
sender(frames_test)
File "ProtAckPosAndTrans.py", line 15, in sender
sendp(frames[i])
File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 259, in sendp
__gen_send(conf.L2socket(iface=iface, *args, **kargs), x, inter=inter, loop=loop, count=count, verbose=verbose, realtime=realtime)
File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 234, in __gen_send
s.send(p)
File "/usr/lib/python2.7/dist-packages/scapy/supersocket.py", line 32, in send
sx = str(x)
File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 261, in __str__
return self.build()
File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 319, in build
p = self.do_build()
File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 308, in do_build
pkt = self.self_build()
File "/usr/lib/python2.7/dist-packages/scapy/packet.py", line 299, in self_build
p = f.addfield(self, p, val)
File "/usr/lib/python2.7/dist-packages/scapy/fields.py", line 70, in addfield
return s+struct.pack(self.fmt, self.i2m(pkt,val))
struct.error: cannot convert argument to integer
The frames[i].show() works perfectly and i also did a 'print frames[i].class' everything is find... So i can't get why it is not working.
I searched everywhere can't find an anwser. Thanks for answers :)
I am trying to run a cuda kernel in numbapro python, but I keep getting an out of resources error.
I then tried to execute the kernel into a loop and send smaller arrays, but that still gave me the same error.
Here is my error message:
Traceback (most recent call last):
File "./predict.py", line 418, in <module>
predict[griddim, blockdim, stream](callResult_d, catCount, numWords, counts_d, indptr_d, indices_d, probtcArray_d, priorC_d)
File "/home/mhagen/Developer/anaconda/lib/python2.7/site-packages/numba/cuda/compiler.py", line 228, in __call__
sharedmem=self.sharedmem)
File "/home/mhagen/Developer/anaconda/lib/python2.7/site-packages/numba/cuda/compiler.py", line 268, in _kernel_call
cu_func(*args)
File "/home/mhagen/Developer/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.py", line 1044, in __call__
self.sharedmem, streamhandle, args)
File "/home/mhagen/Developer/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.py", line 1088, in launch_kernel
None)
File "/home/mhagen/Developer/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.py", line 215, in safe_cuda_api_call
self._check_error(fname, retcode)
File "/home/mhagen/Developer/anaconda/lib/python2.7/site-packages/numba/cuda/cudadrv/driver.py", line 245, in _check_error
raise CudaAPIError(retcode, msg)
numba.cuda.cudadrv.driver.CudaAPIError: Call to cuLaunchKernel results in CUDA_ERROR_LAUNCH_OUT_OF_RESOURCES
Here is my source code:
from numbapro.cudalib import cusparse
from numba import *
from numbapro import cuda
#cuda.jit(argtypes=(double[:], int64, int64, double[:], int64[:], int64[:], double[:,:], double[:] ))
def predict( callResult, catCount, wordCount, counts, indptr, indices, probtcArray, priorC ):
i = cuda.threadIdx.x + cuda.blockIdx.x * cuda.blockDim.x
correct = 0
wrong = 0
lastDocIndex = -1
maxProb = -1e6
picked = -1
for cat in range(catCount):
probSum = 0.0
for j in range(indptr[i],indptr[i+1]):
wordIndex = indices[j]
probSum += (counts[j]*math.log(probtcArray[cat,wordIndex]))
probSum += math.log(priorC[cat])
if probSum > maxProb:
maxProb = probSum
picked = cat
callResult[i] = picked
predictions = []
counter = 1000
for i in range(int(math.ceil(numDocs/(counter*1.0)))):
docTestSliceList = docTestList[i*counter:(i+1)*counter]
numDocsSlice = len(docTestSliceList)
docTestArray = np.zeros((numDocsSlice,numWords))
for j,doc in enumerate(docTestSliceList):
for ind in doc:
docTestArray[j,ind['term']] = ind['count']
docTestArraySparse = cusparse.ss.csr_matrix(docTestArray)
start = time.time()
OPT_N = numDocsSlice
blockdim = 1024, 1
griddim = int(math.ceil(float(OPT_N)/blockdim[0])), 1
catCount = len(music_categories)
callResult = np.zeros(numDocsSlice)
stream = cuda.stream()
with stream.auto_synchronize():
probtcArray_d = cuda.to_device(numpy.asarray(probtcArray),stream)
priorC_d = cuda.to_device(numpy.asarray(priorC),stream)
callResult_d = cuda.to_device(callResult, stream)
counts_d = cuda.to_device(docTestArraySparse.data, stream)
indptr_d = cuda.to_device(docTestArraySparse.indptr, stream)
indices_d = cuda.to_device(docTestArraySparse.indices, stream)
predict[griddim, blockdim, stream](callResult_d, catCount, numWords, counts_d, indptr_d, indices_d, probtcArray_d, priorC_d)
callResult_d.to_host(stream)
#stream.synchronize()
predictions += list(callResult)
print "prediction %d: %f" % (i,time.time()-start)
I found out this was in the cuda procedure.
When you call predict the blockdim is set to 1024.
predict[griddim, blockdim, stream](callResult_d, catCount, numWords, counts_d, indptr_d, indices_d, probtcArray_d, priorC_d)
But the procedure is called iteratively with slice sizes of 1000 elements not 1024.
So, in the procedure it will attempt to write 24 elements that are out of bounds in the return array.
Sending a number of elements parameter (n_el) and placing an error checking call in the cuda procedure solves it.
#cuda.jit(argtypes=(double[:], int64, int64, int64, double[:], int64[:], int64[:], double[:,:], double[:] ))
def predict( callResult, n_el, catCount, wordCount, counts, indptr, indices, probtcArray, priorC ):
i = cuda.threadIdx.x + cuda.blockIdx.x * cuda.blockDim.x
if i < n_el:
....
I am trying to build a new layer/protocol in Scapy. I see this error when I am trying to send/show2 it.
I have put breakpoints to see what is happening but somehow internally the str is getting converted to a Tuple. I am not sure what I am missing. Can someone help me nail down the issue?
>>> p1=PCEPOPEN()
>>> p1.show2()
> /usr/local/lib64/python2.6/site-packages/scapy/fields.py(71)addfield()
-> return s+struct.pack(self.fmt, self.i2m(pkt,val))
(Pdb) val
1
(Pdb) p s
''
(Pdb) c
> /usr/local/lib64/python2.6/site-packages/scapy/fields.py(71)addfield()
-> return s+struct.pack(self.fmt, self.i2m(pkt,val))
(Pdb) p s
'\x01\x10'
(Pdb) val
4
(Pdb) p s
'\x01\x10'
(Pdb) c
> /usr/local/lib64/python2.6/site-packages/scapy/fields.py(71)addfield()
-> return s+struct.pack(self.fmt, self.i2m(pkt,val))
(Pdb) val
10
(Pdb) p s
('\x01\x10\x00\x04', 3, 1L)
(Pdb) c
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 831, in show2
self.__class__(str(self)).show()
File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 262, in __str__
return self.build()
File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 321, in build
p = self.do_build()
File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 309, in do_build
pkt = self.self_build()
File "/usr/local/lib64/python2.6/site-packages/scapy/packet.py", line 300, in self_build
p = f.addfield(self, p, val)
File "/usr/local/lib64/python2.6/site-packages/scapy/fields.py", line 71, in addfield
return s+struct.pack(self.fmt, self.i2m(pkt,val))
TypeError: can only concatenate tuple (not "str") to tuple
>>> p1.show()
###[ OPEN Object for Open message ]###
oclass= OPEN
oType= 1
resflags=
pflag=
iflag=
obLength= 4
ver= 1
kalive= 10
dead= 40
sid= 1
>>>
class PCEPOPEN(Packet):
"""OPEN message to establish a PCEP session"""
name="OPEN Object for Open message"
fields_desc = [ByteEnumField("oclass",1,_object_class),
BitField("oType",1,4),
BitField("resflags", 0, 2),
FlagsField("pflag", 0x0, 1, "P"),
FlagsField("iflag", 0x0, 1, "I"),
ShortField("obLength", 4),
BitField("ver",1,3),
ByteField("kalive",10),
ByteField("dead",40),
ByteField("sid",1)]
def post_build(self, pkt, pay):
if self.obLength is 4:
olen = len(pkt) + len(pay)
pkt = pkt[:2]+struct.pack("!h", olen)
return pkt+pay
Looks like I accidentally deleted a field which caused the problem. I had used BitField and added 3 bits but I was had deleted the line which contained BitField with 5 more bits. Since the packets are Byte aligned scapy was complaining. It was good to learn that :)