├── README.md ├── client ├── answer.py ├── cpuserver.py ├── join.py ├── join_client.py ├── serverfile.py ├── start.sh ├── tcpserver.py └── tcpsign.py └── server ├── add.py ├── checkstatus.py ├── clientfile.py ├── conn.py ├── countip.py ├── findSleep.py ├── getanswer.py ├── itselfip.py ├── join_server.py ├── makefile.py ├── mysql.ini ├── ping.py ├── task.py └── tcpclient.py /README.md: -------------------------------------------------------------------------------- 1 | 雾计算分布式平台 2 | ================ 3 | 基于python实现 4 | -------------- 5 | 6 | 组成 7 | ---- 8 | 9 | + mysql 10 | + python 11 | + raspberry pi3 12 | + docker 13 | -------------------------------------------------------------------------------- /client/answer.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | import socket 4 | import sys 5 | import commands 6 | import re 7 | import time 8 | 9 | def send(host,port,msg): 10 | s=socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 11 | #judge the network status by ping 12 | cmd='ping -w 1 -c 1 '+host 13 | (status,output)=commands.getstatusoutput(cmd) 14 | regex=re.compile("time=\d*",re.IGNORECASE | re.MULTILINE) 15 | if len(regex.findall(output))>0: 16 | s.connect((host,port)) 17 | s.send(msg) 18 | data=s.recv(1024) 19 | s.close() 20 | return data 21 | -------------------------------------------------------------------------------- /client/cpuserver.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #coding=utf-8 3 | import socket 4 | import sys,os 5 | import commands 6 | s=socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 7 | 8 | if len(sys.argv)==1: 9 | print "need argv" 10 | else: 11 | host='' 12 | port=int(sys.argv[1]) 13 | s.bind((host,port)) 14 | s.listen(3) 15 | while True: 16 | client,ipaddr=s.accept() 17 | print "Got a connect from %s" %str(ipaddr) 18 | data=client.recv(1024) 19 | if data=='Cpu': 20 | sy=os.popen('top -bi -n 2 -d 0.02').read().split('\n\n')[2].split('\n')[2].split(':')[1].split(',')[1] 21 | client.send(sy) 22 | else: 23 | client.send("echo:Failed!") 24 | client.close() 25 | -------------------------------------------------------------------------------- /client/join.py: -------------------------------------------------------------------------------- 1 | import sys 2 | import time 3 | import join_client 4 | import commands 5 | import answer 6 | i=0 7 | port2=10413 8 | port=10412 9 | host=sys.argv[1] 10 | #listening the port 10412 11 | #if get 'done,it means no task working or all done,so just continue to next loop 12 | #if get msg (run$id),execute the command (run),then send the answer and its id to master 13 | while True: 14 | i=i+1 15 | data=join_client.join(host,port) 16 | if data=='done': 17 | time.sleep(5) 18 | print 'done' 19 | continue 20 | msg=data.split('&') 21 | run=msg[0] 22 | id=msg[1] 23 | (status,output)=commands.getstatusoutput(run) 24 | get=answer.send(host,port2,output+'&'+id) 25 | if get=='success': 26 | print get 27 | else: 28 | print 'error' 29 | -------------------------------------------------------------------------------- /client/join_client.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | import socket 4 | import sys 5 | import commands 6 | import re 7 | import time 8 | 9 | def join(host,port): 10 | s=socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 11 | 12 | cmd='ping -w 1 -c 1 '+host 13 | (status,output)=commands.getstatusoutput(cmd) 14 | regex=re.compile("time=\d*",re.IGNORECASE | re.MULTILINE) 15 | if len(regex.findall(output))>0: 16 | s.connect((host,port)) 17 | s.send('join') 18 | data=s.recv(1024) 19 | s.close() 20 | return data 21 | -------------------------------------------------------------------------------- /client/serverfile.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding:utf-8 3 | import SocketServer 4 | import subprocess 5 | import string 6 | import time 7 | import sys 8 | class MyTcpServer(SocketServer.BaseRequestHandler): 9 | def recvfile(self, filename): 10 | print "starting reve file!" 11 | f = open(filename, 'wb') 12 | self.request.send('ready') 13 | while True: 14 | data = self.request.recv(4096) 15 | if data == 'EOF': 16 | print "recv file success!" 17 | break 18 | f.write(data) 19 | f.close() 20 | 21 | def sendfile(self, filename): 22 | print "starting send file!" 23 | self.request.send('ready') 24 | time.sleep(1) 25 | f = open(filename, 'rb') 26 | while True: 27 | data = f.read(4096) 28 | if not data: 29 | break 30 | self.request.send(data) 31 | f.close() 32 | time.sleep(1) 33 | self.request.send('EOF') 34 | print "send file success!" 35 | 36 | def handle(self): 37 | print "get connection from :",self.client_address 38 | while True: 39 | try: 40 | data = self.request.recv(4096) 41 | print "get data:", data 42 | if not data: 43 | print "break the connection!" 44 | break 45 | else: 46 | action, filename = data.split() 47 | if action == "put": 48 | self.recvfile(filename) 49 | elif action == 'get': 50 | self.sendfile(filename) 51 | else: 52 | print "get error!" 53 | continue 54 | except Exception,e: 55 | print "get error at:",e 56 | 57 | 58 | if __name__ == "__main__": 59 | host = '' 60 | port = int(sys.argv[1]) 61 | s = SocketServer.ThreadingTCPServer((host,port), MyTcpServer) 62 | s.serve_forever() 63 | -------------------------------------------------------------------------------- /client/start.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | cd /tmp/cut_pic/ 3 | python tcpsign.py 10.0.9.55 & 4 | python serverfile.py 60000 & 5 | python tcpserver.py 33332 & 6 | python cpuserver.py 12222 & 7 | python join.py '''[ip]''' & -------------------------------------------------------------------------------- /client/tcpserver.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | #coding=utf-8 3 | import socket 4 | import sys 5 | import commands 6 | s=socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 7 | 8 | if len(sys.argv)==1: 9 | print "need argv" 10 | else: 11 | host='' 12 | port=int(sys.argv[1]) 13 | s.bind((host,port)) 14 | s.listen(3) 15 | while True: 16 | client,ipaddr=s.accept() 17 | print "Got a connect from %s" %str(ipaddr) 18 | data=client.recv(1024) 19 | (status,output)=commands.getstatusoutput(data) 20 | client.send(output) 21 | client.close() 22 | 23 | -------------------------------------------------------------------------------- /client/tcpsign.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding=utf-8 3 | import socket 4 | import sys 5 | import commands 6 | import re 7 | import time 8 | s=socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 9 | host=sys.argv[1] 10 | port=33334 11 | while True: 12 | cmd='ping -w 1 -c 1 '+host 13 | (status,output)=commands.getstatusoutput(cmd) 14 | regex=re.compile("time=\d*",re.IGNORECASE | re.MULTILINE) 15 | if len(regex.findall(output))>0: 16 | s.connect((host,port)) 17 | s.send('sign') 18 | data=s.recv(1024) 19 | print "Reply from server ------%s" %data 20 | break 21 | else: 22 | continue 23 | 24 | -------------------------------------------------------------------------------- /server/add.py: -------------------------------------------------------------------------------- 1 | import MySQLdb 2 | 3 | def add(ip): 4 | try: 5 | conn=MySQLdb.connect(host='123.206.77.218',user='root',passwd='123456',port=12306) 6 | cur=conn.cursor() 7 | conn.select_db('network') 8 | sql='select id from ips where ip="'+ip+'"' 9 | cur.execute(sql) 10 | result=cur.fetchone() 11 | if result==None: 12 | sql='insert into ips(ip,work,status)values("'+ip+'",0,1)' 13 | cur.execute(sql) 14 | conn.commit() 15 | else: 16 | sql='update ips set status=1 where ip="'+ip+'"' 17 | cur.execute(sql) 18 | return True 19 | except MySQLdb.Error,e: 20 | print "Mysql Error%d:%s"% (e.args[0],e.args[1]) 21 | return False 22 | -------------------------------------------------------------------------------- /server/checkstatus.py: -------------------------------------------------------------------------------- 1 | import MySQLdb 2 | 3 | def check(): 4 | try: 5 | conn=MySQLdb.connect(host='123.206.77.218',user='root',passwd='123456',port=12306) 6 | 7 | cur=conn.cursor() 8 | conn.select_db('network') 9 | sql='select status from work' 10 | cur.execute(sql) 11 | result=cur.fetchall() 12 | if result[0][0]==1: 13 | print 'step 1' 14 | return 1 15 | elif result[0][0]==2: 16 | print 'step 2' 17 | return 2 18 | else: 19 | print 'step 3' 20 | return 3 21 | cur.close() 22 | conn.close() 23 | except MySQLdb.Error,e: 24 | 25 | print "Mysql Error%d:%s"% (e.args[0],e.args[1]) 26 | def checkWork(ip): 27 | conn=MySQLdb.connect(host='123.206.77.218',user='root',passwd='123456',port=12306) 28 | cur=conn.cursor() 29 | conn.select_db('network') 30 | sql='select work from ips where ip="'+ip+'"' 31 | cur.execute(sql) 32 | result=cur.fetchone() 33 | if result[0]==1: 34 | return True 35 | else: 36 | return False 37 | 38 | -------------------------------------------------------------------------------- /server/clientfile.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding:utf-8 3 | import socket 4 | import sys 5 | import time 6 | port=60000 7 | s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) 8 | def recvfile(filename,name): 9 | print "server ready,now client rece file~~" 10 | (file,type)=filename.split('.') 11 | f=open(name+'.'+type,'wb') 12 | while True: 13 | data=s.recv(4096) 14 | if data=='EOF': 15 | print "recv file success" 16 | break 17 | f.write(data) 18 | f.close() 19 | def sendfile(filename): 20 | print "server ready,now client sending file~~" 21 | f=open(filename,'rb') 22 | while True: 23 | data=f.read(4096) 24 | if not data: 25 | break 26 | s.sendall(data) 27 | f.close() 28 | time.sleep(1) 29 | s.sendall('EOF') 30 | print "send file success!" 31 | 32 | def confirm(s,client_command): 33 | s.send(client_command) 34 | data=s.recv(4096) 35 | if data=='ready': 36 | return True 37 | 38 | try: 39 | if len(sys.argv)==1: 40 | print "need argv" 41 | else: 42 | ip=sys.argv[1] 43 | action=sys.argv[2] 44 | filename=sys.argv[3] 45 | name=sys.argv[4] 46 | s.connect((ip,port)) 47 | client_command=action+' '+filename 48 | if action=='get': 49 | if confirm(s,client_command): 50 | recvfile(filename,name) 51 | else: 52 | print "server get error!" 53 | elif action=='put': 54 | if confirm(s,client_command): 55 | sendfile(filename) 56 | else: 57 | print "server get error!" 58 | else: 59 | print "command error!" 60 | except socket.error,e: 61 | print "get error as",e 62 | finally: 63 | s.close(); 64 | -------------------------------------------------------------------------------- /server/conn.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | 3 | import MySQLdb 4 | import configparser 5 | 6 | def mysql_exec(sql): 7 | """ 8 | 执行sql命令 9 | """ 10 | #:获取配置信息 11 | path = 'mysql.ini' 12 | config = configparser.ConfigParser() 13 | config.read(path) 14 | host = config.get("Settings","host") 15 | user = config.get("Settings","user") 16 | passwd = config.get("Settings","passwd") 17 | port = config.get("Settings","port") 18 | 19 | #:连接数据库 20 | conn = MySQLdb.connect(host=host, user=user,passwd=passwd,port=int(port)) 21 | cur = conn.cursor() 22 | conn.select_db('network') 23 | 24 | #执行sql命令 25 | cur.execute(sql) 26 | conn.commit() 27 | result = cur.fetchall() 28 | 29 | #:关闭数据库连接 30 | cur.close() 31 | conn.close() 32 | 33 | return result 34 | 35 | if __name__ == '__main__': 36 | sql = 'select * from ips' 37 | for i in range(5): 38 | print(mysql_exec(sql)) 39 | -------------------------------------------------------------------------------- /server/countip.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | #coding:utf-8 3 | import MySQLdb 4 | def count(): 5 | try: 6 | conn=MySQLdb.connect(host='123.206.77.218',user='root',passwd='123456',port=12306) 7 | cur=conn.cursor() 8 | conn.select_db('network') 9 | cur.execute('select count(*) from ips where status=1') 10 | result=cur.fetchone() 11 | cur.close() 12 | conn.close() 13 | return result[0] 14 | except MySQLdb.Error,e: 15 | print "Mysql Error%d:%s"% (e.args[0],e.args[1]) 16 | return 0 17 | -------------------------------------------------------------------------------- /server/findSleep.py: -------------------------------------------------------------------------------- 1 | import MySQLdb 2 | 3 | def getSleep(): 4 | conn=MySQLdb.connect(host='123.206.77.218',user='root',passwd='123456',port=12306) 5 | cur=conn.cursor() 6 | conn.select_db('network') 7 | sql='select count(*) from task' 8 | cur.execute(sql) 9 | result=cur.fetchone() 10 | if result[0]==0: 11 | return None 12 | else 13 | sql='select ip from ips where work=0 and status=1' 14 | cur.execute(sql) 15 | result=cur.fetchall() 16 | if result==(): 17 | return None 18 | -------------------------------------------------------------------------------- /server/getanswer.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import sys 3 | import checkstatus 4 | import commands 5 | import MySQLdb 6 | import makefile 7 | s=socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 8 | #open the port 10413 for getting the answer from slave 9 | if len(sys.argv)==1: 10 | print "need argv" 11 | else: 12 | host='' 13 | port=int(sys.argv[1]) 14 | s.bind((host,port)) 15 | s.listen(3) 16 | while True: 17 | client,ipaddr=s.accept() 18 | ip=str(ipaddr[0]) 19 | data=client.recv(1024) 20 | msg=data.split('&') 21 | id=msg[1] 22 | answer=msg[0] 23 | conn=MySQLdb.connect(host='123.206.77.218',user='root',passwd='123456',port=12306) 24 | cur=conn.cursor() 25 | conn.select_db('network') 26 | #update the status of the task item by the id 27 | sql='update task set ends=1 where id='+id 28 | cur.execute(sql) 29 | conn.commit() 30 | cur.close() 31 | conn.close() 32 | #save the answer to a file named by its command's id 33 | makefile.make(id,answer) 34 | client.send('success') 35 | client.close() 36 | 37 | -------------------------------------------------------------------------------- /server/itselfip.py: -------------------------------------------------------------------------------- 1 | def get_local_ip(ifname): 2 | import socket 3 | import fcntl 4 | import struct 5 | s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) 6 | inet = fcntl.ioctl(s.fileno(), 0x8915, struct.pack('256s', ifname[:15])) 7 | ret = socket.inet_ntoa(inet[20:24]) 8 | return ret 9 | -------------------------------------------------------------------------------- /server/join_server.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python 2 | import socket 3 | import sys 4 | import checkstatus 5 | import commands 6 | import MySQLdb 7 | s=socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 8 | 9 | if len(sys.argv)==1: 10 | print "need argv" 11 | else: 12 | host='' 13 | port=int(sys.argv[1]) 14 | s.bind((host,port)) 15 | s.listen(3) 16 | while True: 17 | client,ipaddr=s.accept() 18 | ip=str(ipaddr[0]) 19 | data=client.recv(1024) 20 | if data=='join': 21 | (status,output)=commands.getstatusoutput('python task.py '+ip) 22 | out=output 23 | #send the output to slave through the tcp socket 24 | print 'working' 25 | else: 26 | out='error' 27 | print 'error' 28 | client.send(out) 29 | client.close() 30 | 31 | -------------------------------------------------------------------------------- /server/makefile.py: -------------------------------------------------------------------------------- 1 | def make(name,text): 2 | try: 3 | f=open(name,'a') 4 | f.write(text) 5 | f.close() 6 | except IOError: 7 | pass 8 | 9 | -------------------------------------------------------------------------------- /server/mysql.ini: -------------------------------------------------------------------------------- 1 | [Settings] 2 | host = 123.206.77.218 3 | user = root 4 | passwd = 123456 5 | port = 12306 6 | -------------------------------------------------------------------------------- /server/ping.py: -------------------------------------------------------------------------------- 1 | import MySQLdb 2 | import os,sys,re 3 | import subprocess 4 | import time 5 | while(True): 6 | time.sleep(5) 7 | try: 8 | conn=MySQLdb.connect(host='123.206.77.218',user='root',passwd='123456',port=12306) 9 | cur=conn.cursor() 10 | conn.select_db('network') 11 | cur.execute('select ip,status from ips') 12 | result=cur.fetchall() 13 | if result==None: 14 | print 'None' 15 | else: 16 | for ip in result: 17 | p=subprocess.Popen(["ping -w 1 -c 1 "+ip[0]], 18 | stdin=subprocess.PIPE, 19 | stdout=subprocess.PIPE, 20 | stderr=subprocess.PIPE, 21 | shell=True) 22 | out=p.stdout.read() 23 | regex=re.compile("time=\d*",re.IGNORECASE | re.MULTILINE) 24 | if len(regex.findall(out))>0: 25 | print ip[0]+':Host Up!' 26 | if ip[1]!=1: 27 | cur.execute('update ips set status=1 where ip="'+ip[0]+'"') 28 | conn.commit() 29 | else: 30 | print ip[0]+':Host Down!' 31 | if ip[1]!=0: 32 | cur.execute('update ips set status=0 where ip="'+ip[0]+'"') 33 | conn.commit() 34 | cur.close() 35 | conn.close() 36 | except MySQLdb.Error,e: 37 | print "Mysql Error%d:%s"% (e.args[0],e.args[1]) 38 | 39 | -------------------------------------------------------------------------------- /server/task.py: -------------------------------------------------------------------------------- 1 | #coding:utf-8 2 | import socket 3 | import commands 4 | import sys 5 | import MySQLdb 6 | import itselfip 7 | import makefile 8 | ''' 9 | build4.0 10 | ''' 11 | ip=sys.argv[1] 12 | flag=True 13 | try: 14 | conn=MySQLdb.connect(host='123.206.77.218',user='root',passwd='123456',port=12306) 15 | cur=conn.cursor() 16 | conn.select_db('network') 17 | 18 | #get a command and its id(run$id) from task list,and update the status of tasklist 19 | sql='update task set gets=1,ip="'+ip+'",status=0 where gets=0 limit 1' 20 | cur.execute(sql) 21 | conn.commit() 22 | sql='select id,runs from task where gets=1 and ends=0 and ip="'+ip+'"' 23 | cur.execute(sql) 24 | result=cur.fetchone() 25 | if result==None: 26 | sql='update task set gets=1,ip="'+ip+'",status=0 where gets=1 and ends=0 and status=1 limit 1' 27 | cur.execute(sql) 28 | conn.commit() 29 | sql='select id,runs from task where gets=1 and ends=0 and ip="'+ip+'" limit 1' 30 | cur.execute(sql) 31 | result=cur.fetchall() 32 | if result==(): 33 | flag=False 34 | #if the tasklist is empty,print done 35 | print 'done' 36 | else: 37 | #start to do the task whick has been taken but not done. 38 | id=result[0][0] 39 | run=result[0][1] 40 | #here print the command and id 41 | print run+'$'+str(id) 42 | else: 43 | id=result[0] 44 | run=result[1] 45 | print run+'&'+str(id) 46 | 47 | cur.close() 48 | conn.close() 49 | 50 | except MySQLdb.Error,e: 51 | print "Mysql Error%d:%s"% (e.args[0],e.args[1]) 52 | 53 | -------------------------------------------------------------------------------- /server/tcpclient.py: -------------------------------------------------------------------------------- 1 | import socket 2 | import sys 3 | import commands 4 | import re 5 | import time 6 | import sys 7 | import multiprocessing 8 | import MySQLdb 9 | s=socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 10 | 11 | def ping(ip): 12 | i=0 13 | while True: 14 | cmd='ping -w 1 -c 1 '+ip 15 | (status,output)=commands.getstatusoutput(cmd) 16 | regex=re.compile("time=\d*",re.IGNORECASE | re.MULTILINE) 17 | if len(regex.findall(output))>0: 18 | continue 19 | else: 20 | i=i+1 21 | if i>2: 22 | try: 23 | conn=MySQLdb.connect(host='123.206.77.218',user='root',passwd='123456',port=12306) 24 | cur=conn.cursor() 25 | conn.select_db('network') 26 | cur.execute('select status from ips where ip="'+ip+'"') 27 | result=cur.fetchall() 28 | if result[0][0]==0: 29 | cur.execute('update task set status=1 where gets=1 and ends=0 and ip="'+ip+'"') 30 | conn.commit() 31 | cur.execute('update ips set work=0 where ip="'+ip+'"') 32 | conn.commit() 33 | cur.close() 34 | conn.close() 35 | print 'failed network!' 36 | break 37 | else: 38 | continue 39 | except MySQLdb.Error,e: 40 | print "Mysql Error%d:%s"% (e.args[0],e.args[1]) 41 | print 'failed' 42 | sys.exit(0) 43 | 44 | def send(host,port,msg): 45 | try: 46 | s.connect((host,port)) 47 | s.send(msg) 48 | data=s.recv(1024) 49 | print 'success&'+data 50 | s.close() 51 | except: 52 | print 'failed' 53 | conn=MySQLdb.connect(host='123.206.77.218',user='root',passwd='123456',port=12306) 54 | cur=conn.cursor() 55 | conn.select_db('network') 56 | cur.execute('update task set status=1 where gets=1 and ends=0 and ip="'+ip+'"') 57 | conn.commit() 58 | cur.execute('update ips set status=0,work=0 where ip="'+host+'"') 59 | conn.commit() 60 | cur.close() 61 | conn.close() 62 | 63 | if __name__=="__main__": 64 | host=sys.argv[1] 65 | port=int(sys.argv[2]) 66 | msg=sys.argv[3] 67 | p1=multiprocessing.Process(target = ping, args = (host,)) 68 | p2=multiprocessing.Process(target = send, args = (host,port,msg)) 69 | 70 | p2.start() 71 | p1.start() 72 | 73 | while True: 74 | p=multiprocessing.active_children() 75 | if len(p)==1: 76 | cmd='kill -9 '+str(p[0].pid) 77 | (status,output)=commands.getstatusoutput(cmd) 78 | break 79 | elif len(p)==2: 80 | continue 81 | else: 82 | 83 | break 84 | 85 | --------------------------------------------------------------------------------