Python實現(xiàn)簡單的文件傳輸與MySQL備份的腳本分享
更新時間:2016年01月03日 16:29:53 作者:leonisliu
這篇文章主要介紹了Python實現(xiàn)簡單的文件傳輸與MySQL備份的腳本分享,用到了socket與tarfile模塊,需要的朋友可以參考下
用python實現(xiàn)簡單Server/Client文件傳輸:
服務(wù)器端:
#!/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()
由于擔(dān)心服務(wù)器數(shù)據(jù)安全,所以寫了這個腳本,結(jié)合上面分享的Server/Client 文件互傳,可以備份網(wǎng)站數(shù)據(jù)到本地,安全又可靠
#!/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 壓縮 以當(dāng)日日期命名
filename = bakdir + time.strftime('%Y%m%d')+'.tar.gz'
tar = tarfile.open(filename,"w:gz")
tar.add(source)
tar.close()
您可能感興趣的文章:
- Python實現(xiàn)基于HTTP文件傳輸實例
- Python實現(xiàn)的簡單文件傳輸服務(wù)器和客戶端
- python實現(xiàn)的一個p2p文件傳輸實例
- python使用tcp實現(xiàn)局域網(wǎng)內(nèi)文件傳輸
- python基于xmlrpc實現(xiàn)二進制文件傳輸?shù)姆椒?/a>
- python cs架構(gòu)實現(xiàn)簡單文件傳輸
- python 使用poster模塊進行http方式的文件傳輸?shù)椒?wù)器的方法
- python3.5基于TCP實現(xiàn)文件傳輸
- 詳解Python3的TFTP文件傳輸
- python實現(xiàn)UDP協(xié)議下的文件傳輸
相關(guān)文章
selenium+headless chrome爬蟲的實現(xiàn)示例
這篇文章主要介紹了selenium+headless chrome爬蟲的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Python實現(xiàn)學(xué)生管理系統(tǒng)的代碼(JSON模塊)
這篇文章主要介紹了Python實現(xiàn)學(xué)生管理系統(tǒng)的代碼(JSON模塊),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04

