python 多进程方法

#!/usr/bin/env python3


import multiprocessing as mp  # define mp
import logging
import socket
import time
import pymongo
from pymongo.database import Database
import math


logger = mp.log_to_stderr(logging.DEBUG)


def getTagContent(line, tag):
return line[line.find("<"+tag+">")+len("<"+tag+">"):line.rfind("</"+tag+">")]


def record_clientname(collection,winMsg):
pcuid=getTagContent(winMsg,"PCUID")
clientname=getTagContent(winMsg,"CLIENTNAME")
sendDate=getTagContent(winMsg,"SENDDATE")
print(collection)
print(pcuid)
print(sendDate)
print(clientname)
collection.insert({"PCUID":pcuid,"SENDDATE":sendDate,"CLIENTNAME":clientname })
return


def record_versioninfo(collection, winMsg):
ver=getTagContent(winMsg,"VERSION")
print(ver)
print(collection)
lastVer=b'1.0'
return lastVer


def record_pcinfo(collection, winMsg, clientIP):
pcuid=getTagContent(winMsg,"PCUID")
hwinfo=getTagContent(winMsg,"HWINFO")
sendDate=getTagContent(winMsg,"SENDDATE")
clientIP=clientIP[clientIP.find("('")+2: clientIP.find("',")]
print(collection)
print(sendDate)
print(pcuid)
print(hwinfo)
#pcuid
collection.insert({"PCUID":pcuid,"SENDDATE":sendDate,"HWINFO":hwinfo,"CLIENTIP":clientIP })
return




def record_traffic(collection, winMsg):
beginDate=getTagContent(winMsg,"BEGINDATE")
endDate=getTagContent(winMsg,"ENDDATE")
maxVal=getTagContent(winMsg,"MAX")
minVal=getTagContent(winMsg,"MIN")
sigma=getTagContent(winMsg,"MEANS")
variance=getTagContent(winMsg,"VARIANCE")
pcuid=getTagContent(winMsg,"PCUID")
iotype=getTagContent(winMsg,"IOTYPE")
print(collection)
print(beginDate)
print(endDate)
print(maxVal)
print(minVal)
print(sigma)
print(variance)
print(pcuid)
print(iotype)


collection.insert({"BEGINDATE":beginDate,"ENDDATE":endDate, "MAX":maxVal, "MIN": minVal, "SIGMA":sigma,"VARIANCE":variance, "PCUID":pcuid, "IOTYPE":iotype})
print('INSERT OVER!\n')
#for rows in collection.find():
# print(rows)
return


def worker(socket):
db = pymongo.MongoClient('localhost',27017).monidb
db.authenticate("monitor", "monitor123")
#print(collection)
while True:
client, address = socket.accept()
clientIP="{u}".format(u=address)
logger.debug("{u} connected".format(u=address))
#print("connected!")
winMsg=client.recv(1000).decode('GB18030')
print(winMsg)
#print("recv over!")
if(len(winMsg)<9):
continue
if "02"==getTagContent(winMsg,"MSGTYPE"):
collection = db.traffic
record_traffic(collection, winMsg)
elif "01"==getTagContent(winMsg,"MSGTYPE"):
collection = db.pchwi
record_pcinfo(collection, winMsg, clientIP)
elif "03"==getTagContent(winMsg,"MSGTYPE"):
collection = db.versioncontrol
lastVer=record_versioninfo(collection, winMsg)
print(lastVer)
client.send(lastVer)
elif "04"==getTagContent(winMsg,"MSGTYPE"):
collection = db.clientname
record_clientname(collection,winMsg)
else:
print("ERROR MSGTYPE!")
print(winMsg)
#client.send("OK")
client.close()






if __name__ == '__main__':
num_workers = 5


serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('172.18.193.170',6444))
serversocket.listen(5)


workers = [mp.Process(target=worker, args=(serversocket,)) for i in
range(num_workers)]


for p in workers:
p.daemon = True
p.start()


while True:
try:
time.sleep(10)
except:
break
#continue
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值