要求:
1、客戶端收集系統cpu及記憶體佔用率,正在執行的程序名稱,週期上傳至伺服器
2、伺服器接受資訊,儲存至資料庫
客戶端程式:
環境:win7,python,python的psutil庫
程式:
#! /usr/bin/env python
#coding=utf-8
'''version:2.0
監控windows資訊:cpu占有率,記憶體占有率,當前程序名稱
資料格式:,{},...]
'''import
socket
import
time
import
psutil
import
json
#function of run server
defrunserver(s,host,port):
s.connect((host, port))
'connect success!'#
function of close server
defcloseserver(s):
time.sleep(5)
s.close()
#function of get cpu state
def getcpustate(interval=1):
return (str(psutil.cpu_percent(interval)) + "%"
)#function of get memory
defgetmemorystate():
mem_rate =0
for pnum in
psutil.pids():
p =psutil.process(pnum)
mem_rate = mem_rate +p.memory_percent()
return
"%.2f%%
"%mem_rate
#function of get process's name
defgetprocessname():
processnamelist =
for pnum in
psutil.pids():
p =psutil.process(pnum)
return
processnamelist
#function of package massage
defpackmassage():
massage =
massage
return
massage
#function of send massage
defsendmassage(s):
s.send(json.dumps(packmassage()))
time.sleep(5)
s.sendall('\n
')#function of receive massage
defrecvmassage(s):
data = s.recv(1024)
'received
', repr(data)
if__name__ == "
__main__":
host = '
172.29.153.22'#
the server host
port = 8000 #
the same port as used by the server
cycle = 60 #
the cycle of sending massage
while
true:
s =socket.socket(socket.af_inet, socket.sock_stream)
runserver(s,host,port)
sendmassage(s)
closeserver(s)
time.sleep(cycle)
伺服器程式:
環境:ubuntu14.04,python,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()
defread_message(self):
self._stream.read_until('\n
', self.broadcast_messages) #
read massage until '\n'
defbroadcast_messages(self, data, massagedict):
massagedict=eval(data[:-1]) #
str to dictif'
cpustate'in
massagedict.keys() :
print'\n
','cpustate:
',massagedict['
cpustate
'],'\n'
if'memorystate'in
massagedict.keys() :
'memorystate:
',massagedict['
memorystate
'],'\n'
if'processname'in
massagedict.keys() :
'processname:
',massagedict['
processname
'],'\n'
self.storemassage()
for conn in
connection.clients:
conn.send_message(data)
self.read_message()
defstoremassage(self,massagedict ):
conn = pymongo.connection("
localhost
", 27017)
db = conn['
networksecurity']
processinfo=db['
processinfo']
processinfo.insert(massagedict)
'success to store the massage!
'def
send_message(self, data):
self._stream.write(data)
defon_close(self):
"a user has left the chat room.
", self._address
connection.clients.remove(self)
class
chatserver(tcpserver):
defhandle_stream(self, stream, address):
"new connection :
", address
connection(stream, address)
#print "connection num is:", len(connection.clients)
if__name__ == '
__main__
':
"server start ......
"server =chatserver()
server.listen(8000)
ioloop.instance().start()
伺服器與客戶端
建立socket操作,建立流式套接字,返回套接字型大小socksrv socket socket int af,int type,int protocol 第乙個引數,指定位址簇 tcp ip只能是af inet,也可寫成pf inet socket socksrv socket af inet,s...
UDP 客戶端伺服器
udp 客戶端 include include include include include define size 100 define ip 127.0.0.1 define port 10086 int main struct sockaddr in addr 建立socket udp so...
客戶端與伺服器
cs與bs 軟體使用方式上的兩種劃分 c s client server pc客戶端與伺服器架構 特點 在伺服器當中就主要是乙個資料庫,把所有業務邏輯都交給客戶端來完成 優點 較為安全,使用者介面豐富,客戶體驗好 缺點 每次公升級都要重新安裝,針對不同的作業系統開發,可移植性差 b sbrowser...