代码拉取完成,页面将自动刷新
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#基础的函数
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import os
import time
import socket
import datetime
#注意,这两个函数都有RECV 函数开始,为的是将控制权完全交给客户端。
#不加文件检查的发送 协议的完整性没有考虑
def naive_send_file(conn,path,debugFlag=False):
# 信号,因为缓冲区中数据都挨在一起
msg_r=conn.recv(1024).decode('utf-8')
if not msg_r=="reday to receive": return
print "###############发送文件#################"
print "发送:\"%s\"......" %(path) + str(datetime.datetime.utcnow())
# 传输文件大小
filesize = str(os.path.getsize(path))
if debugFlag: print "file size:%s"%filesize
conn.send(filesize.encode('utf-8'))
# 信号,因为缓冲区中数据都挨在一起
msg_r=conn.recv(1024).decode('utf-8')
if not msg_r=="file size received": return
# 传输文件
f = open(path, "rb")
for line in f:
conn.send(line) # 传输的是二进制文件字节流,不需要解码
time.sleep(0.001)
s="sending:\t" + line[:20]
if len(line)>20: s=s+"等%d个字符"%len(line)
if debugFlag:print s
print "发送完成"
msg_r=conn.recv(1024).decode('utf-8')
if not msg_r=="file received": return
print "############## END ##################"
return
#不加文件检查的接受 协议的完整性没有考虑
def naive_receive_file(conn,path,debugFlag=False):
# 接收文件大小
print "################接收文件################"
print "接收:\"%s\"\t......"%(path) +str(datetime.datetime.utcnow())
filesize = conn.recv(1024)
filesize = int(filesize)
if debugFlag: print "file size:%d" % filesize
# 信号,因为缓冲区中数据都挨在一起
conn.send("file size received".encode('utf-8'))
# 传输文件
received_size = 0
f = open(path, "wb")
while received_size < filesize:
data = conn.recv(1024)
received_size += len(data)
if debugFlag: print "received size:%d"%received_size
f.write(data)
f.close()
print "接收完成"
conn.send("file received".encode('utf-8'))
print "############### END #################"
return
#封装好的发送文件用函数,依赖于naive_receive_file
def client_receive_file(remote_source_path,local_dest_path,ip,port,codeing='utf-8'):
sk = socket.socket() # 创建套接字
sk.connect((ip, port)) # 绑定IP
# 确认链接正常
msg_r = sk.recv(1024).decode('utf-8')
if msg_r == "connection start":
print "连接成功"
# 声明发送文件文件名以及存储位置
msg_s = "read_file:%s"%remote_source_path
sk.send(msg_s.encode('utf-8'))
msg_r = sk.recv(1024).decode('utf-8')
if not msg_r == "reday to send": print "ERROR"
sk.send("reday to receive".encode('utf-8'))
# 发送文件
filepath = local_dest_path
naive_receive_file(sk, filepath, True)
sk.close()
time.sleep(0.001)
return
#封装好的接收文件用函数,依赖于naive_send_file
def client_send_file(local_source_path,remote_dest_path,ip,port,codeing='utf-8'):
sk = socket.socket() # 创建套接字
sk.connect((ip, port)) # 绑定IP
# 确认链接正常
msg_r = sk.recv(1024).decode('utf-8')
if msg_r == "connection start":
print "连接成功"
# 声明发送文件文件名以及存储位置
msg_s = "save_file:%s"%remote_dest_path
sk.send(msg_s.encode('utf-8'))
# 发送文件
filepath = local_source_path
naive_send_file(sk, filepath, True)
sk.close()
return
"""
协议如下:
naive_send_file
naive_receive_file
#########客户端发送文件##########
Server Client
----- file
<--- command
发送"ready to receive"启动naive_receive_file进程 ---> 启动naive_send_file进程
文件数据量
"file size received"
文件内容
""file received""
#########客户端接收文件##########
Server Client
file -----
<--- command
发送"ready to send"启动naive_send_file进程 ---> "reday to receive"启动naive_receive_file进程
文件数据量
"file size received"
文件内容
"file received"
"""
#执行命令并等待完成
def client_run_command(command,ip,port):
sk = socket.socket() # 创建套接字
sk.connect((ip, port)) # 绑定IP
print "connection to:\t%s:%s" % (ip, port)
msg_r = sk.recv(1024).decode('utf-8')
# 首次链接
if msg_r == "connection start":
print "连接成功"
s="run command:"+command
print "发送命令"+ command
sk.send(s.encode('utf-8'))
msg_r = sk.recv(1024).decode('utf-8')
if not msg_r[:13]=="command over:":
print "非法返回值%s"%msg_r
pass
print "返回值:%s"%msg_r[13:]
print "命令执行完成:%s"%command
sk.close()
return
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。