Python implements simple File Transfer and shares with MySQL backup scripts,

Source: Internet
Author: User

Python implements simple File Transfer and shares with MySQL backup scripts,

Use python to implement simple Server/Client file transmission:

Server:

#!/usr/bin/pythonimport SocketServer, timeclass MyServer(SocketServer.BaseRequestHandler):userInfo = {'leonis' : 'leonis','hudeyong' : 'hudeyong','mudan' : 'mudan' }def handle(self):print 'Connected from', self.client_addresswhile True:receivedData = self.request.recv(8192)if not receivedData:continueelif 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:breakwhile 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':breakself.request.close()print 'Disconnected from', self.client_addressprintif __name__ == '__main__':print 'Server is started\nwaiting for connection…\n'srv = SocketServer.ThreadingTCPServer(('ip', 50000), MyServer)srv.serve_forever()

Client:

import socket, timeclass 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.responseself.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':breakelse: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':breakf.write(data)f.flush()f.close()print 'download finished'breakelse:print 'Server: Invalid password'sock.sendall('bye')sock.close()print 'Disconnected'if __name__ == '__main__':client = MyClient()client.connect()

Due to concerns about Server data security, This script can be used together with the Server/Client files shared above to back up website data locally, which is secure and reliable.

#! /Usr/bin/python # Filename: webbak. pyimport osimport timeimport tarfile OS. chdir ('/home/web /') # Switch the directory source = 'leonis 'bakdir = '/home/web/leonis/' # mysql dumpdump = 'mysqldump 'dbuser = 'xxxxxxxx' dbpwd = 'xxxxxxxxxxxx' dbname = 'xxxxxxxx 'sqlfile = '/home/web/leonis. SQL 'SQL = "% s-u % s-p % s> % s" % (dump, dbuser, dbpwd, dbname, sqlfile) if OS. path. exists (sqlfile): OS. remove (sqlfile) else: print 'then w Ill dump SQL file 'result = OS. popen (SQL) if result: # print ("SQL backup completed! ") Else: print (" SQL backup failed! ") # Gzip compression name: filename = bakdir + time.strftime('policy?m=d'{}'.tar.gz 'tar = tarfile. open (filename," w: gz ") tar. add (source) tar. close ()

Articles you may be interested in:
  • Python implements HTTP-based file transfer instances
  • A p2p File Transfer instance implemented by python
  • How to Use Python to send email attachments to regularly back up MySQL
  • Python backup Mysql script

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.