Python實(shí)現(xiàn)簡單的文件傳輸與MySQL備份的腳本分享
用python實(shí)現(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實(shí)現(xiàn)基于HTTP文件傳輸實(shí)例
- Python實(shí)現(xiàn)的簡單文件傳輸服務(wù)器和客戶端
- python實(shí)現(xiàn)的一個p2p文件傳輸實(shí)例
- python使用tcp實(shí)現(xiàn)局域網(wǎng)內(nèi)文件傳輸
- python基于xmlrpc實(shí)現(xiàn)二進(jìn)制文件傳輸?shù)姆椒?/a>
- python cs架構(gòu)實(shí)現(xiàn)簡單文件傳輸
- python 使用poster模塊進(jìn)行http方式的文件傳輸?shù)椒?wù)器的方法
- python3.5基于TCP實(shí)現(xiàn)文件傳輸
- 詳解Python3的TFTP文件傳輸
- python實(shí)現(xiàn)UDP協(xié)議下的文件傳輸
相關(guān)文章
Python利用Flask-Mail實(shí)現(xiàn)發(fā)送郵件詳解
Flask?的擴(kuò)展包?Flask?-?Mail?通過包裝了?Python?內(nèi)置的smtplib包,可以用在?Flask?程序中發(fā)送郵件。本文將利用這特性實(shí)現(xiàn)郵件發(fā)送功能,感興趣的可以了解一下2022-08-08
Python 中將秒轉(zhuǎn)換為小時、分鐘和秒的示例代碼
這篇文章主要介紹了在 Python 中將秒轉(zhuǎn)換為小時、分鐘和秒,本篇文章將討論使用 Python 中的四種不同方法來使用、管理秒并將其轉(zhuǎn)換為天、小時、分鐘和秒,需要的朋友可以參考下2023-05-05
從0到1使用python開發(fā)一個半自動答題小程序的實(shí)現(xiàn)
這篇文章主要介紹了從0到1使用python開發(fā)一個半自動答題小程序的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-05-05
Python socket連接中的粘包、精確傳輸問題實(shí)例分析
這篇文章主要介紹了Python socket連接中的粘包、精確傳輸問題,結(jié)合實(shí)例形式分析了Python socket連接中的粘包、精確傳輸相關(guān)問題原因、解決方案與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
Python Pickling 和 Unpickling 的區(qū)別
Python中的Pickling和Unpickling是與數(shù)據(jù)序列化和反序列化相關(guān)的重要概念,本文主要介紹了Python Pickling和Unpickling的區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2023-11-11
ubuntu20.04運(yùn)用startup application開機(jī)自啟動python程序的腳本寫法
這篇文章主要介紹了ubuntu20.04運(yùn)用startup application開機(jī)自啟動python程序的腳本寫法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-10-10
Python數(shù)據(jù)結(jié)構(gòu)之棧詳解
棧和隊(duì)列是在程序設(shè)計(jì)中常見的數(shù)據(jù)類型,從數(shù)據(jù)結(jié)構(gòu)的角度來講,棧和隊(duì)列也是線性表,是操作受限的線性表。本文將詳細(xì)介紹一下Python中的棧,感興趣的可以了解一下2022-03-03
用python實(shí)現(xiàn)的可以拷貝或剪切一個文件列表中的所有文件
python 實(shí)現(xiàn)剪切或是拷貝一個文件列表中的所有文件2009-04-04

