python进行端口监控并将接收到的数据写入excel
#!/usr/bin/python3
#--*--coding:utf-8 --*--
import socket
import xlwt
import xlrd
import json
import sys
import time
import datetime
#recv data
#bind ip and port
ip_port=('192.168.10.193',9009)
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.bind(ip_port)
def main():
i=0
data_save = xlwt.Workbook() #make a data save
sheet1 = data_save.add_sheet('sheet1') #make a sheet1
sheet1.write(0,0,'messageType')
sheet1.write(0,1,'region')
sheet1.write(0,2,'type')
sheet1.write(0,3,'id')
sheet1.write(0,4,'source')
sheet1.write(0,5,'lat')
sheet1.write(0,6,'lon')
while 1:
data, addr = sock.recvfrom(10240)
innovusion_data = json.loads(data.decode())
print(innovusion_data)
participants = innovusion_data['participants']
print(len(participants))
for x in range(len(participants)):
print(participants[x]['messageType'])
print(participants[x]['region'])
print(participants[x]['type'])
print(participants[x]['id'])
print(participants[x]['source'])
print(participants[x]['lat'])
print(participants[x]['lon'])
messageType = participants[x]['messageType']
region = participants[x]['region']
type = participants[x]['type']
id = participants[x]['id']
source = participants[x]['source']
lat = participants[x]['lat']
lon = participants[x]['lon']
print('messageType:%d' % messageType)
sheet1.write(x+1,0,'messageType')
sheet1.write(x+1,1,participants[x]['messageType'])
sheet1.write(x+1,2,participants[x]['messageType'])
sheet1.write(x+1,3,participants[x]['messageType'])
sheet1.write(x+1,4,participants[x]['messageType'])
sheet1.write(x+1,5,participants[x]['messageType'])
sheet1.write(x+1,6,participants[x]['messageType'])
data_save.save("v2r_data.xls")
if __name__ == '__main__':
main()