Replace a character with another in python - python-2.7

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

Related

How do I get a TextIO.instream from a POSIX file descriptor?

If I have a POSIX file descriptor of type Posix.FileSys.file_desc, how to I convert it into a TextIO.instream? I want to do the reverse of How do I get the file descriptor of a file opened using TextIO.openIn?
This'll do the trick. It requires a "name" for the instream, which the basis library claims is used for error messages shown to the user. So I would recommend using the name or path of the underlying file, if there is one.
fun fdToInstream (name: string, fd: Posix.FileSys.file_desc) : TextIO.instream =
let
val (flags, _) = Posix.IO.getfl fd
val isNonBlockMode = Posix.IO.O.anySet (Posix.IO.O.nonblock, flags)
val reader: TextIO.StreamIO.reader =
Posix.IO.mkTextReader
{ fd = fd
, name = name
, initBlkMode = not isNonBlockMode
}
val stream_ins: TextIO.StreamIO.instream =
TextIO.StreamIO.mkInstream (reader, "")
in
TextIO.mkInstream stream_ins
end

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.

Unicode and UTF-8 in python to generate csv file

I'm trying to write some variableas in a csv file.
Documentation (https://docs.python.org/2/library/csv.html) say the all input must be UTF-8 or ASCII, but I don't know how to set the encode ( I already tried .decode('utf-8'))
part of the code:
def get_events():
global SEVERITY, SEVERITY
get_host()
for HID,HNAME in zip(HOSTID,HOSTNAME):
EVENT = zapi.event.get(time_from=DATE_FROM,
time_till=DATE_TILL,
output='extend',
source='0',
hostids=HID,
select_acknowledges='extend',
)
for t in EVENT:
TRIGGERID = t['objectid']
TRIGGER = zapi.trigger.get(output='extend',
triggerids=TRIGGERID,
expandDescription='true',
)
for T in TRIGGER:
TRIGGER_D = T['description'].encode('utf-8')
SEVERITY = T['priority']
if int(SEVERITY) < 3 or TRIGGER_D.decode('utf-8') == 'Zabbix agent on %s is unreachable for 8 minutes' % HNAME or TRIGGER_D.decode('utf-8') == '%s jmx is not reachable' % HNAME:
continue
NS = t['ns']
HOUR = t['clock']
if t.get('value') == "0":
STATUS = "OK"
elif t.get('value')== "1":
STATUS = "PROBLEM"
else:
STATUS = "UNKNOWN"
if t['acknowledged'] == "1":
LISTA_DIC = t['acknowledges'][0]
USER = LISTA_DIC['alias']
MESSAGE = LISTA_DIC["message"]
else:
MESSAGE = ""
USER = ""
with open(FILE_OUT, 'wb') as FILE:
FILE = csv.writer(FILE,delimiter=';')
FILE.writerow((HNAME,STATUS,HOUR,NS,TRIGGER_D, SEVERITY,MESSAGE,USER)
I'm get the error
“UnicodeDecodeError: 'ascii' codec can't decode byte”
Another thing: How add a newline in
FILE.writerow((HNAME,STATUS,HOUR,NS,TRIGGER_D, SEVERITY,MESSAGE,USER))
..MESSAGE,USER "\n")) doesn't work
Can solve the problem change the encode to latin-1
FILE.writerow((HNAME,STATUS,HOUR,NS,TRIGGER_D.encode('latin-1'), SEVERITY,MESSAGE.encode('latin-1'),USER)

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

JSON.parse error on simplejson return in Django

I have a view page that currently has two columns of data shown, soon to be expanded to four. Each column contains the result of a QuerySet for that particular model.
Here's what I have in my views.py method:
if request.REQUEST["type"] == "text":
client = Client.objects.get(client_name = request.REQUEST["search"])
peerList = ClientPeers.objects.prefetch_related().filter(client = client.client)
compList = ClientCompetitors.objects.prefetch_related().filter(client = client.client)
else:
peerList = ClientPeers.objects.prefetch_related().filter(client = request.REQUEST["search"])
compList = ClientCompetitors.objects.prefetch_related().filter(client = request.REQUEST["search"])
for peer in peerList:
peerlst.append({"pid" : peer.parentorg.parentorg, "pname" : peer.parentorg.parentorgname})
for comp in compList:
complst.append({"cid" : comp.parentorg.parentorg, "cname" : comp.parentorg.parentorgname})
lst.append(simplejson.dumps(peerlst))
lst.append(simplejson.dumps(complst))
return HttpResponse(simplejson.dumps(lst), mimetype = "text/json")
This allows me to send a 2D array of data to the browser in the format
[ { //JSON }, { //JSON } ]
In my jQuery.ajax success function, I have
function handler(results) {
var data = JSON.parse(results);
for (var i = 0; i < data[0].length; i++)
$("#available_peers").append("<li>" + data[0][i].pname + "</li>");
for (var i = 0; i < data[1].length; i++)
$("#available_competitors").append("<li>" + data[1][i].cname + "</li>");
Firebug shows that the GET request works and I can see the data in the response tab. However, the console prints out
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data
var data = JSON.parse(results)
This error disappears if I replace var data = JSON.parse(results) with
var peers = JSON.parse(data[0]);
var comps = JSON.parse(data[1]);
Why does one method work but another doesn't?
The jQuery ajax() call will make an intelligent guess as to the returned data type. In your example, function handler(results), the results variable will already be a decoded JSON object, containing two items in an array. The reason that JSON.parse(data[0]) works, is that you have returned JSON encoded data as a string.
Don't encode the individual list elements to JSON before placing in the output array:
lst.append(peerlst) # <-- Don't encode to JSON string here
lst.append(complst)
return HttpResponse(simplejson.dumps(lst), mimetype = "application/json") # <-- Single JSON encoding