Remove new line character from lines in Python - python-2.7

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

Related

pcap parsing in python2.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.

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

Replace a character with another in python

I am using the following code in python to receive data from a device.
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.strip('\r\n')
return
for line in readlines(sock):
print line
And I am getting the output in following format:
MSG,2,0,0,8963AB,0,2015/02/06,15:03:27.380,2015/02/06,15:03:27.380,,0,7.5,343.0,10.152763,76.390593,,,,,,-1 MSG,2,0,0,8963AB,0,2015/02/06,15:03:28.630,2015/02/06,15:03:28.630,,0,7.5,348.0,10.152809,76.390593,,,,,,-1
I should get the output in following format:
'MSG','2','0','0','8963AB','0','2015/02/06','15:03:27.380','2015/02/06','15:03:27.380','','0','7.5','343.0','10.152763','76.390593','','','','','','-1'
Split the string with , as delimiter. Then append the elements to a list
appended_list = [x for x in original_list.split(',')]
Now the appended list will contain the strings like you wish

Opening files with an If in Python

Profesor1= "Profesor-Materia.txt"
Profesor2= "Profesor-Año.txt"
input ("Seleccione un profesor: ")
if(input=="Profesor1"):
file = open(Profesor1)
data1= file.readlines(1)
print(data1)
else:
file = open(Profesor2)
data = file.readlines(1)
print(data)
So this is my code, I want to open The file: "Profesor-Año" whenever I input anything else than "Profesor1" but it just keeps opening the file "Profesor-Materia" even when I input something like: sadsadsad
Can you help me with this problem?
Ps: I've already tried using if(input==Profesor1)
Your code has quite a few problems. Here is a proper version:
Profesor1 = "Profesor-Materia.txt"
Profesor2 = "Profesor-Año.txt"
in_text = str(input("Seleccione un profesor: "))
if(in_text != 'Profesor1'):
file = open(Profesor2, 'r')
data = file.readlines()
else:
file = open(Profesor1, 'r')
data = file.readlines()
print(data)
Your if statement is just checking that the variable Profesor1 has something in it (see the Python docs on truth value testing), which it does. The user input is being ignored.
You need to change it to something like this:
prof = input ("Seleccione un profesor: ")
if(prof == Profesor1):
# do stuff

Read (os.read) FIFO non-blockingly without fixed buffer size

In this question, the asker has addressed the problem of reading from a named pipe in a non-blocking manner, but he uses a fixed buffer size. Is there a way to do this without a fixed buffer size and just waiting for the other end to terminate their own buffer with a newline character?
Assuming that your delimiter is a you can read multiple variable length strings in a non-blocking manner, as shown in this program which counts while receiving output from a named pipe.
import os
import time
import errno
import sys
io = os.open(expanduser("~/named_pipes/cob_input"), os.O_RDONLY | os.O_NONBLOCK)
# For implementing non-blocking IO
def read_pipe_non_blocking(input_pipe, size):
try:
in_buffer = os.read(input_pipe, size)
except OSError as err:
if err.errno == errno.EAGAIN or err.errno == errno.EWOULDBLOCK:
in_buffer = None
else:
raise # something else has happened -- better reraise
return in_buffer
def get_azimuth(input_pipe):
in_buffer = read_pipe_non_blocking(input_pipe, 1)
print(in_buffer)
if(in_buffer is None):
sys.stderr.write("n")
return ""
else:
tmp_buffer = None
while(tmp_buffer != "a"):
sys.stderr.write("m")
time.sleep(0.1)
tmp_buffer = read_pipe_non_blocking(input_pipe, 1)
if(tmp_buffer != None and tmp_buffer != "a"):
in_buffer += tmp_buffer
read_pipe_non_blocking(input_pipe, 1) #Read in the newline character and the toss it
sys.stderr.write("\nReturning \{%s\}" %in_buffer)
return in_buffer
i = 0
while 1:
print i
time.sleep(1)
i += 1
get_azimuth(io)
This code has been directly copy pasted from my code and isn't really that clear. If anyone needs clarification, leave a comment.