pcap parsing in python2.7 - python-2.7

To carry on from this question.https://stackoverflow.com/questions/9330686/parsing-pcap-in-python-2-6
I'm now trying to perform print summary but still not sure what to include in my final argument before print summary. Please see the the code below:
def run_example():
global total_packet_count, total_bytes_count, average_pkt_size
try:
sys.argv[1]
dmp_file = sys.argv[1]
fp_dmp_file = open(dmp_file)
except Exception as e:
print 'Error: please supply pcap filename!\n'
return
f = open('test1.pcap')
try:
sys.argv[1]
dmp_file = sys.argv[1]
file = open(dmp_file)
except Exception as e:
print 'Error: please supply pcap filename!\n'
return
pcap = dpkt.pcap.Reader(file)
for ts, buf in pcap:
eth = dpkt.ethernet.Ethernet(buf)
ip = eth.data
tcp = ip.data
src_ip = socket.inet_ntoa(ip.src)
src_port = str(ip.data.sport)
dst_ip = socket.inet_ntoa(ip.dst)
dst_port = str(ip.data.dport)
if type(ip.data) == dpkt.tcp.TCP:
protocol = 'tcp'
elif type(ip.data) == dpkt.udp.UDP:
protocol = 'udp'
print_packet_info (ts, src_ip, src_port, dst_ip, dst_port, protocol, ip.len, ip.ttl)
print_summary(len (total_packet_count), len (total_bytes_count), len (average_pkt_size))
##fp_dmp_file.close()
if name == 'main':
run_example()
I managed to print packet data but still unable to print summary. I guess I need to do count values from global to be able to print summary.
Any help is much appreciated

So firstly, we need to identify global variables again on top of our file coming after added libraries in order to have it called outside "def run_example()".
Then, after "dst_port" we can call our summary variables with the fist one will increment packets in file. The second one will check the length of packets size in this case (bytes) This can be found in dkpt manual. Lastly, "print summary" variables I did wasn't wright. Instead we call our defined variables as for the average we will divide "total bytes"/"total packets" witch will give us the average size of packets.

Related

Error while closing the python serial port

" i am trying to read data from the serial connection and doing some stuff if it matches my string but its giving me errors when i close the serial connection port"
" for some reason i do not see this error if i use the serial.readline() method "
import time
import serial
from Queue import Queue
from threading import Thread
class NonBlocking:
def __init__(self, serial_connection, radio_serial_connection):
self._s = serial_connection
self._q = Queue()
self.buf = bytearray()
def _populateQueue(serial_connection, queue):
if type(serial_connection) == str:
return
self.s = serial_connection
while True:
i = self.buf.find(b"\n")
if i >= 0:
r = self.buf[:i + 1]
self.buf = self.buf[i + 1:]
queue.put(r)
while True:
i = max(1, min(2048, self.s.in_waiting))
data = self.s.read(i)
i = data.find(b"\n")
if i >= 0:
r = self.buf + data[:i + 1]
self.buf[0:] = data[i + 1:]
a = r.split('\r\n')
for item in a:
if item:
queue.put(item)
else:
self.buf.extend(data)
self._t = Thread(target=_populateQueue, args=(self._s, self._q))
self._t.daemon = True
self._t.start()
def read_all(self, timeout=None):
data = list()
if self._q.empty():
pass
while not self._q.empty():
data.append(self._q.get(block=timeout is not None, timeout=timeout))
return data
class SerialCommands:
def __init__(self, port, baudrate):
self.serial_connection = serial.Serial(port, baudrate)
self.queue_data = NonBlocking(self.serial_connection, '')
def read_data(self):
returned_info = self.queue_data.read_all()
return returned_info
def close_q(self):
self.serial_connection.close()
class qLibrary:
def __init__(self):
self.q = None
self.port = None
def close_q_connection(self):
self.q.close_q()
def establish_connection_to_q(self, port, baudrate=115200, delay=2):
self.delay = int(delay)
self.port = port
try:
if not self.q:
self.q = SerialCommands(self.port, int(baudrate))
except IOError:
raise AssertionError('Unable to open {0}'.format(port))
def verify_event(self, data, timeout=5):
timeout = int(timeout)
data = str(data)
# print data
while timeout:
try:
to_analyze = self.q.read_data()
for item in to_analyze:
print "item: ", item
if str(item).find(str(data)) > -1:
print "Found data: '{0}' in string: '{1}'".format(data, item)
except:
pass
time.sleep(1)
timeout -= 1
if __name__ == '__main__':
q1 = qLibrary()
q1.establish_connection_to_q('COM5')
q1.verify_event("ATE")
q1.close_q_connection()
" i expect the code to close the serial connection without any exceptions or errors "
the output is
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python27\Lib\threading.py", line 801, in __bootstrap_inner
self.run()
File "C:\Python27\Lib\threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "C:/Program Files (x86)/serialtest1.py", >line 27, in _populateQueue
data = self.s.read(i)
File "C:\Program Files (x86)\venv\lib\site->packages\serial\serialwin32.py", line 283, in read
ctypes.byref(self._overlapped_read))
TypeError: byref() argument must be a ctypes instance, not 'NoneType'
If you define your serial port with no timeout it will get the default setting timeout=None which means when you call serial.read(x) the code will block until you read x bytes.
If you never get those x bytes your code will get stuck in there waiting forever, or at least until you receive more data on the buffer to get the total number of bytes received equal to x.
If you mix that up with threading, I'm afraid you are quite likely closing the port while you are trying to read.
You can probably fix this issue just defining a sensible read timeout on your port or changing the way you read. The general advice is to set a timeout that works for your application and read at least the maximum number of bytes you expect. Reading your code, that seems to be what you wanted to do. If so, you forgot to set the timeout.
If you have a reason not to set a timeout or you want to keep your reading routine as it is, you can make your code work if you cancel reading before closing. You can do that with serial.cancel_read()

running shell commands crystal language and capturing the output

I am used to using open3 to run commands in Ruby. Since there doesn't seem to be an equivalent lib in crystal-lang, I kludged up this:
def run_cmd(cmd, args)
stdout_str = IO::Memory.new
stderr_str = IO::Memory.new
result = [] of Int32 | String
status = Process.run(cmd, args: args, output: stdout_str, error: stderr_str)
if status.success?
result = [status.exit_code, "#{stdout_str}"]
else
result = [status.exit_code, "#{stderr_str}"]
end
stdout_str.close
stderr_str.close
result
end
cmd = "ping"
hostname = "my_host"
args = ["-c 2", "#{hostname}"]
result = run_cmd(cmd, args)
puts "ping: #{hostname}: Name or service not known" if result[0] != 0
Is there a better way to do this? Says the retired network specialist who is not a software developer exploring crystal-lang.
Thanks in advance for all advice.
Probably this:
def run_cmd(cmd, args)
stdout = IO::Memory.new
stderr = IO::Memory.new
status = Process.run(cmd, args: args, output: stdout, error: stderr)
if status.success?
{status.exit_code, stdout.to_s}
else
{status.exit_code, stderr.to_s}
end
end
We don't need to close an IO::Memory because it doesn't represent a handle to any OS resources, just a block of memory, and we use tuples instead of arrays for the return. This means the callers know we're returning exactly two items and the first is a number and the second is a string. With an array return the caller only knows we're returning any number of items, any of which could be either an int32 or a string.
You can then use it like this:
cmd = "ping"
hostname = "my_host"
args = ["-c 2", hostname]
status, output = run_cmd(cmd, args)
puts "ping: #{hostname}: Name or service not known" unless status == 0

Getting error when using value returned by a function as an input for another function

I am a starter in using python. I am showing a section of the code I was working on. I was trying to communicate python to an arduino using pyserial and blinking a LED. I want to use the value returned by the function send_data to be used as an input for function delay_for_goodant. But when I run the code, I get the error
global name data not defined.
Any suggestions to get rid of this?
def openthedoor(set_accepted_list):
if(((len(set_accepted_list)) >0) & (set_forbidden_list == set()):
print"yes,open the gate"
use_door(1)
else:
print"no,close the gate"
use_door(0)
set_for_comparison = set(set_accepted_list & set_list_ant_id)
list_for_comparison = list(set_for_comparison)
return set_for_comparison,list_for_comparison
def establishing_connection():
print ser.read();
ser.write('1')
last_action = -1
def use_door(activate):
global last_action
if(last_action != activate):
send_data(activate)
last_action = activate
def send_data(data):
if (data ==0):
print "manga"
return True
else:
print "muringa"
return False
def delay_for_goodant(data):
print "thenga"
global ser
try:
if (ser == None):
ser = serial.Serial("COM1",9600,timeout = 0)
print "reconnect"
if send_data(data) is True:
ser.write('0')
time.sleep(0)
incoming_data = ser.readline()
print "python is telling arduino to keep the LED dim"
else:
ser.write('1')
time.sleep(0.7)
incoming_data2 = ser.readline()
print "python is telling the arduino to keep the LED bright"
except IOError:
ser = None
I call these functions in later parts of the code as. Am i doing a mistake here? What i am trying to do here is if data ==0, i want to ser.write('1').
establishing_connection()
set_for_comparison,list_for_comparison = openthedoor(set_accepted_list)
activate = use_door()
data = send_data(activate)
arabica = delay_for_goodant(data)
Also the value inside the function delay_for_goodant is not printed.
This is what i get, when i run the code:
True
muringa
Traceback (most recent call last):
File "door_code_final_calib_dig_2.py", line 352, in <module>
arabica = delay_for_goodant(data)
NameError: name 'data' is not defined

Re-using Queued Data in Python

I have a data stream that adds price information to a queue where it is consumed while processing an order. Could someone advise on how I could save the last price streamed and only send to the queue IF the price were (for instance) higher than the last, or vice versa?
Here is my code, thanks in advance for any help!
def stream_to_queue(self):
response = self.connect_to_stream()
if response.status_code != 200:
return
for line in response.iter_lines(1):
if line:
try:
msg = json.loads(line)
except Exception as e:
print "Caught exception when converting message into json\n" + str(e)
return
if msg.has_key("instrument") or msg.has_key("tick"):
print msg["tick"]
instrument = msg["tick"]["instrument"]
time = msg["tick"]["time"]
bid = msg["tick"]["bid"]
ask = msg["tick"]["ask"]
stop = msg["tick"]["ask"]
tev = TickEvent(instrument, time, bid, ask)
self.events_queue.put(tev)

Remove new line character from lines in Python

I finally wrote a program to recieve data from a socket:
from socket import *
HOST = 'localhost'
PORT = 30003 #our port from before
ADDR = (HOST,PORT)
BUFSIZE = 4096
sock = socket( AF_INET,SOCK_STREAM)
sock.connect((ADDR))
def readlines(sock, recv_buffer=4096, delim='\n'):
buffer = ''
data = True
while data:
data = sock.recv(recv_buffer)
buffer += data
while buffer.find(delim) != -1:
line, buffer = buffer.split('\n', 1)
yield line
return
for line in readlines(sock):
print line
I am recieving the required data line by line but there is a new line character at the end of each line which is not required for me.
Please tell me how to remove the character at the end of each line. I want to save
these data to a database,line by line, in CSV format.
Regards
Manoj
change
yield line
to
yield line.strip('\n')
if you are running on windows, you might need to do:
yield line.strip('\r\n')