用python实现简单Server/Client文件传输:
服务器端:
#!/usr/bin/python import SocketServer, time class MyServer(SocketServer.BaseRequestHandler): userInfo = { 'leonis' : 'leonis', 'hudeyong' : 'hudeyong', 'mudan' : 'mudan' } def handle(self): print 'Connected from', self.client_address while True: receivedData = self.request.recv(8192) if not receivedData: continue elif receivedData == 'Hi, server': self.request.sendall('hi, client') elif receivedData.startswith('name'): self.clientName = receivedData.split(':')[-1] if MyServer.userInfo.has_key(self.clientName): self.request.sendall('valid') else: self.request.sendall('invalid') elif receivedData.startswith('pwd'): self.clientPwd = receivedData.split(':')[-1] if self.clientPwd == MyServer.userInfo[self.clientName]: self.request.sendall('valid') time.sleep(5) sfile = open('down.sh', 'rb') while True: data = sfile.read(1024) if not data: break while len(data) > 0: intSent = self.request.send(data) data = data[intSent:] time.sleep(3) self.request.sendall('EOF') else: self.request.sendall('invalid') elif receivedData == 'bye': break self.request.close() print 'Disconnected from', self.client_address print if __name__ == '__main__': print 'Server is started\nwaiting for connection…\n' srv = SocketServer.ThreadingTCPServer(('ip', 50000), MyServer) srv.serve_forever()
客户端:
import socket, time class MyClient: def __init__(self): print 'Prepare for connecting…' def connect(self): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.connect(('ip', 50000)) sock.sendall('Hi, server') self.response = sock.recv(8192) print 'Server:', self.response self.s = raw_input("Server: Do you want get the 'thinking in python' file") if self.s == 'y': while True: self.name = raw_input('Server: input our name:') sock.sendall('name:' + self.name.strip()) self.response = sock.recv(8192) if self.response == 'valid': break else: print 'Server: Invalid username' while True: self.pwd = raw_input('Server: input our password:') sock.sendall('pwd:' + self.pwd.strip()) self.response = sock.recv(8192) if self.response == 'valid': print 'please wait…' f = open('down.sh', 'wb') while True: data = sock.recv(1024) if data == 'EOF': break f.write(data) f.flush() f.close() print 'download finished' break else: print 'Server: Invalid password' sock.sendall('bye') sock.close() print 'Disconnected' if __name__ == '__main__': client = MyClient() client.connect()
由于担心服务器数据安全,所以写了这个脚本,结合上面分享的Server/Client 文件互传,可以备份网站数据到本地,安全又可靠
#!/usr/bin/python # Filename: webbak.py import os import time import tarfile os.chdir('/home/web/') #切换目录 source = 'leonis' bakdir = '/home/web/leonis/' # mysql dump dump = 'mysqldump' dbuser = 'XXXXXXX' dbpwd = 'XXXXXXXXXXX' dbname = 'XXXXXXXX' sqlfile = '/home/web/leonis/leonis.sql' sql = "%s -u%s -p%s %s > %s" % (dump,dbuser,dbpwd,dbname,sqlfile) if os.path.exists(sqlfile): os.remove(sqlfile) else: print 'then will dump sql file' result = os.popen(sql) if result: # print ("SQL backup completed!") else: print ("SQL backup failed!") # gzip 压缩 以当日日期命名 filename = bakdir + time.strftime('%Y%m%d')+'.tar.gz' tar = tarfile.open(filename,"w:gz") tar.add(source) tar.close()
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月15日
2024年11月15日
- 黄乙玲1988-无稳定的爱心肝乱糟糟[日本东芝1M版][WAV+CUE]
- 群星《我们的歌第六季 第3期》[320K/MP3][70.68MB]
- 群星《我们的歌第六季 第3期》[FLAC/分轨][369.48MB]
- 群星《燃!沙排少女 影视原声带》[320K/MP3][175.61MB]
- 乱斗海盗瞎6胜卡组推荐一览 深暗领域乱斗海盗瞎卡组分享
- 炉石传说乱斗6胜卡组分享一览 深暗领域乱斗6胜卡组代码推荐
- 炉石传说乱斗本周卡组合集 乱斗模式卡组最新推荐
- 佟妍.2015-七窍玲珑心【万马旦】【WAV+CUE】
- 叶振棠陈晓慧.1986-龙的心·俘虏你(2006复黑限量版)【永恒】【WAV+CUE】
- 陈慧琳.1998-爱我不爱(国)【福茂】【WAV+CUE】
- 咪咕快游豪礼放送,百元京东卡、海量欢乐豆就在咪咕咪粉节!
- 双11百吋大屏焕新“热”,海信AI画质电视成最大赢家
- 海信电视E8N Ultra:真正的百吋,不止是大!
- 曾庆瑜1990-曾庆瑜历年精选[派森][WAV+CUE]
- 叶玉卿1999-深情之选[飞图][WAV+CUE]