服务器程序:
运行系统:ubuntu
环境:tornado
mongodb
#! /usr/bin/env python #coding=utf-8 from tornado.tcpserver import TCPServer from tornado.ioloop import IOLoop import pymongo import json class Connection(object): clients = set() def __init__(self, stream, address): Connection.clients.add(self) self._stream = stream self._address = address self._stream.set_close_callback(self.on_close) self.read_message() # print "new client ", address def read_message(self): self._stream.read_until(‘\n‘, self.broadcast_messages) def broadcast_messages(self, data): # print "client said:", data[:-1], self._address print ‘client said‘,data[:-1] # print ‘data[-2]‘,data[-2] for conn in Connection.clients: conn.send_message(data) self.read_message() #data base if data[0]==‘[‘: conn = pymongo.Connection("localhost", 27017) db = conn[‘networkSecurity‘] processInfo=db[‘processInfo‘] decodeinfos=json.loads(data[:-1]) # print ‘after:‘,type(decodeinfos) for info in decodeinfos: processInfo.insert({‘name‘:info[u‘name‘],‘memeory‘:info[u‘memeory‘]}) print ‘success to store the data!‘ def send_message(self, data): self._stream.write(data) # self._stream.write(‘yessssssssssssss‘) def on_close(self): print "A user has left the chat room.", self._address Connection.clients.remove(self) class ChatServer(TCPServer): def handle_stream(self, stream, address): print "New connection :", address, stream Connection(stream, address) print "connection num is:", len(Connection.clients) if __name__ == ‘__main__‘: print "Server start ......" server = ChatServer() server.listen(8000) IOLoop.instance().start()
客户端程序:
运行系统:win7 64位
环境: python 2.7
psutil库
#! /usr/bin/env python #coding=utf-8 import socket import time import psutil import json HOST = ‘192.168.111.130‘ # The remote host PORT = 8000 # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((HOST, PORT)) #the info of process plist=[] for pnum in psutil.pids(): p = psutil.Process(pnum) pinfo={‘name‘:p.name(),‘memeory‘:p.memory_percent()} plist.append(pinfo) # print u"进程名 %-20s 内存利用率 %-18s 进程状态 %-10s 创建时间 %-10s "%(p.name(),p.memory_percent(),p.status(),p.create_time()) # print ‘plist : ‘,plist s.sendall(json.dumps(plist)) # time.sleep(5) s.sendall(‘\n‘) data = s.recv(1024) print ‘Received‘, repr(data) time.sleep(6000) s.close()
时间: 2024-10-25 20:14:02