python实现简单的文件传输与mysql备份的脚本分享

用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?(y/n):”)
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()

Posted in 未分类

发表评论