[TOC] ## server.py <details> <summary> server.py</summary> ``` #!/usr/bin/env python #encoding=utf-8 import os,sys import socket class DataServer: port=10000 def __init__(self): pass def run(self): dsoc=socket.socket(socket.AF_INET,socket.SOCK_STREAM) dsoc.bind(('localhost',self.port)) print "listening at",self.port while True: dsoc.listen(1) conn,addr=dsoc.accept() print "connected",addr databuf=conn.recv(13) print "received",databuf if databuf.startswith('WRITFILE'): print "OPS == write file" dlist=databuf.split(',') fnamelen=int(dlist[1]) conn.send('OK') print "filenamelen is",fnamelen filename=conn.recv(fnamelen) filename=filename[filename.rindex('\\')+1:] print "file is",filename fp=open(filename,'wb') while True: data=conn.recv(1024) if not data:break fp.write(data) fp.flush() fp.close() print "finished!",filename if databuf.startswith('FETCFILE'): print "OPS == fetch file" dlist=databuf.split(',') fnamelen=int(dlist[1]) conn.send('OK') print "filenamelen is",fnamelen filename=conn.recv(fnamelen) filename=filename[filename.rindex('\\')+1:] print "file is",filename fp=open(filename,'rb') while True: data=fp.read(4096) if not data: break while len(data)>0: sent=conn.send(data) data=data[sent:] print "Fished to send ",filename conn.close() def setPort(self,port): self.port=int(port) if __name__=="__main__": ds=DataServer() ds.setPort(sys.argv[1]) ds.run() ``` </details> <br/> ## client.py <details> <summary>client.py</summary> ``` # !/usr/bin/env python # encoding=utf-8 import os import socket import hashlib class Configuration: count = 0 clist = [] def __init__(self): self.readPath("machineconf.txt") def readPath(self, pStr): self.pathStr = pStr fp = open(self.pathStr, 'r') while True: line = fp.readline() if not line: break line = line.rstrip() self.clist.append(line) self.count = self.count + 1 def getCount(self): return self.count def getList(self): return self.clist class Client: maList = [] macNum = 0 def __init__(self): conf = Configuration() self.maList = conf.getList() self.macNum = conf.getCount() def write(self, src): srcHash = self.hash(src) print "File is", src locatedNum = srcHash % self.macNum print "Location machine number is", locatedNum IPort = self.maList[locatedNum].split(',') toIp = IPort[0] toPort = IPort[1] print "Send to", toIp, toPort self.send(src, toIp, toPort) def hash(self, src): md5 = hashlib.md5() md5.update(src) return int(md5.hexdigest()[-4:], 16) def send(self, src, ip, port): clsoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) clsoc.connect((ip, int(port))) fp = open(src, 'rb') formStr = "WRITFILE,%04d" % len(src) print "formStr", formStr, len(formStr) clsoc.send(formStr) resdata = clsoc.recv(1024) if resdata.startswith('OK'): print "OK" print "sending....", src clsoc.send(src) print "sending data...." while True: data = fp.read(4096) if not data: break while len(data) > 0: sent = clsoc.send(data) data = data[sent:] print "Fished to send ", src fp.close() def fetch(self, src): srcHash = self.hash(src) locatedNum = srcHash % self.macNum IPort = self.maList[locatedNum].split(',') toIp = IPort[0] toPort = IPort[1] print "fetch from", toIp, toPort self.get(src, toIp, toPort) def get(self, src, ip, port): clsoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM) clsoc.connect((ip, int(port))) formStr = "FETCFILE,%04d" % len(src) print "formStr", formStr clsoc.send(formStr) resdata = clsoc.recv(1024) if resdata.startswith('OK'): print "OK" print "sending....", src clsoc.send(src) print "fetching data...." ffile = src[src.rindex('\\') + 1:] fp = open(ffile, 'wb') while True: data = clsoc.recv(1024) if not data: break fp.write(data) print "fetching", data[-8:] fp.flush() fp.close() print "finished!", src if __name__ == "__main__": mac = Client() src = "C:\Program Files\JetBrains\GoLand 2018.3.62\GoLand 2018.3.6\product-info.json" mac.write(src) mac.fetch(src) ``` </details> <br/> ## machineconf.txt 客户端的配置文件 ``` 127.0.0.1,10001 127.0.0.1,10002 127.0.0.1,10003 ``` ## 运行 ``` python2 server.py 10001 python2 server.py 10002 python2 server.py 10003 python2 client.py ```