Server Monitoring Client Process

Source: Internet
Author: User

Server program:

Operating system: Ubuntu

Environment: Tornado

Mongodb

#!/usr/bin/env python#Coding=utf-8   fromTornado.tcpserverImportTCPServer fromTornado.ioloopImportIoloopImportPymongoImportJSONclassConnection (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          defread_message (self): Self._stream.read_until ('\ n', Self.broadcast_messages)defbroadcast_messages (self, data):#print "Client said:", Data[:-1], self._address        Print 'Client said', data[:-1]        #print ' data[-2] ', data[-2]         forConninchConnection.clients:conn.send_message (data) self.read_message ()#Data Base        ifdata[0]=='[': Conn= Pymongo. Connection ("localhost", 27017) DB= conn['networksecurity'] ProcessInfo=db['ProcessInfo'] Decodeinfos=json.loads (data[:-1])            #print ' After: ', type (Decodeinfos)             forInfoinchDecodeinfos:processInfo.insert ({'name': Info[u'name'],'memeory': Info[u'memeory']})            Print 'success to store the data!'          defsend_message (self, data): Self._stream.write (data)#self._stream.write (' yessssssssssssss ')              defOn_close (self):Print "A User has left the chat.", self._address Connection.clients.remove (self)classChatserver (tcpserver):defHandle_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 ()

Client program:

Operating system: Win7 64-bit

Environment: Python 2.7

Psutil Library

#!/usr/bin/env python#Coding=utf-8ImportSocketImport TimeImportPsutilImportJsonhost='192.168.111.130'    #The remote hostPORT = 8000#The same port as used by the servers =Socket.socket (socket.af_inet, socket. Sock_stream) S.connect ((HOST, PORT))#The info of processplist=[] forPnuminchpsutil.pids (): P=Psutil. Process (pnum) pinfo={'name':p. Name (),'memeory':p. Memory_percent ()} plist.append (pinfo)#print U "process name%-20s memory utilization%-18s process status%-10s creation time%-10s"% (P.name (), P.memory_percent (), P.status (), P.create_time ())#print ' plist: ', plistS.sendall (Json.dumps (plist))#Time.sleep (5)S.sendall ('\ n') Data= S.RECV (1024)Print 'Received', repr (data) time.sleep (6000) S.close ()

Server Monitoring Client Process

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.