圆月山庄资源网 Design By www.vgjia.com
用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.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年11月08日
2024年11月08日
- 雨林唱片《赏》新曲+精选集SACD版[ISO][2.3G]
- 罗大佑与OK男女合唱团.1995-再会吧!素兰【音乐工厂】【WAV+CUE】
- 草蜢.1993-宝贝对不起(国)【宝丽金】【WAV+CUE】
- 杨培安.2009-抒·情(EP)【擎天娱乐】【WAV+CUE】
- 周慧敏《EndlessDream》[WAV+CUE]
- 彭芳《纯色角3》2007[WAV+CUE]
- 江志丰2008-今生为你[豪记][WAV+CUE]
- 罗大佑1994《恋曲2000》音乐工厂[WAV+CUE][1G]
- 群星《一首歌一个故事》赵英俊某些作品重唱企划[FLAC分轨][1G]
- 群星《网易云英文歌曲播放量TOP100》[MP3][1G]
- 方大同.2024-梦想家TheDreamer【赋音乐】【FLAC分轨】
- 李慧珍.2007-爱死了【华谊兄弟】【WAV+CUE】
- 王大文.2019-国际太空站【环球】【FLAC分轨】
- 群星《2022超好听的十倍音质网络歌曲(163)》U盘音乐[WAV分轨][1.1G]
- 童丽《啼笑姻缘》头版限量编号24K金碟[低速原抓WAV+CUE][1.1G]