萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> python實現的文件同步服務器實例

python實現的文件同步服務器實例

   本文實例講述了python實現的文件同步服務器。分享給大家供大家參考。具體實現方法如下:

  服務端使用asyncore, 收到文件後保存到本地。

  客戶端使用pyinotify監視目錄的變化 ,把變動的文件發送到服務端。

  重點:

  1. 使用structs打包發送文件的信息,服務端收到後,根據文件信息來接收客戶端傳送過來的文件。

  2. 客戶端使用多線程,pyinotify監視到文件變化,放到隊列中,由另外一個線程發送。

  上代碼:

  服務端:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 # receive file from client and store them into file use asyncore.# #/usr/bin/python #coding: utf-8 import asyncore import socket from socket import errno import logging import time import sys import struct import os import fcntl import threading from rrd_graph import MakeGraph try: import rrdtool except (ImportError, ImportWarnning): print "Hope this information can help you:" print "Can not find pyinotify module in sys path, just run [apt-get install python-rrdtool] in ubuntu." sys.exit(1) class RequestHandler(asyncore.dispatcher): def __init__(self, sock, map=None, chunk_size=1024): self.logger = logging.getLogger('%s-%s' % (self.__class__.__name__, str(sock.getsockname()))) self.chunk_size = chunk_size asyncore.dispatcher.__init__(self,sock,map) self.data_to_write = list() def readable(self): #self.logger.debug("readable() called.") return True def writable(self): response = (not self.connected) or len(self.data_to_write) #self.logger.debug('writable() -> %s data length -> %s' % (response, len(self.data_to_write))) return response def handle_write(self): data = self.data_to_write.pop() #self.logger.debug("handle_write()->%s size: %s",data.rstrip('rn'),len(data)) sent = self.send(data[:self.chunk_size]) if sent < len(data): remaining = data[sent:] self.data_to_write.append(remaining) def handle_read(self): self.writen_size = 0 nagios_perfdata = '../perfdata' head_packet_format = "!LL128s128sL" head_packet_size = struct.calcsize(head_packet_format) data = self.recv(head_packet_size) if not data: return filepath_len, filename_len, filepath,filename, filesize = struct.unpack(head_packet_format,data) filepath = os.path.join(nagios_perfdata, filepath[:filepath_len]) filename = filename[:filename_len] self.logger.debug("update file: %s" % filepath + '/' + filename) try: if not os.path.exists(filepath): os.makedirs(filepath) except OSError: pass self.fd = open(os.path.join(filepath,filename), 'w') #self.fd = open(filename,'w') if filesize > self.chunk_size: times = filesize / self.chunk_size first_part_size = times * self.chunk_size second_part_size = filesize % self.chunk_size while 1: try: data = self.recv(self.chunk_size) #self.logger.debug("handle_read()->%s size.",len(data)) except socket.error,e: if e.args[0] == errno.EWOULDBLOCK: print "EWOULDBLOCK" time.sleep(1) else: #self.logger.debug("Error happend while receive data: %s" % e) break else: self.fd.write(data) self.fd.flush() self.writen_size += len(data) if self.writen_size == first_part_size: break #receive the packet at last while 1: try: data = self.recv(second_part_size) #self.logger.debug("handle_read()->%s size.",len(data)) except socket.error,e: if e.args[0] == errno.EWOULDBLOCK: print "EWOULDBLOCK" time.sleep(1) else: #self.logger.debug("Error happend while receive data: %s" % e) break else: self.fd.write(data) self.fd.flush() self.writen_size += len(data) if len(data) == second_part_size: break elif filesize <= self.chunk_size: while 1: try: data = self.recv(filesize) #self.logger.debug("handle_read()->%s size.",len(data)) except socket.error,e: if e.args[0] == errno.EWOULDBLOCK: print "EWOULDBLOCK" time.sleep(1) else: #self.logger.debug("Error happend while receive data: %s" % e) break else: self.fd.write(data) self.fd.flush() self.writen_size += len(data) if len(data) == filesize:
copyright © 萬盛學電腦網 all rights reserved