python - 将文件从服务器发送到客户端(python)

标签 python client-server file-transfer

我目前正在编写一个需要传输一些文件才能工作的服务器-客户端应用程序。 我正在使用这种方法:

客户:

file_to_send = raw_input(">") 

try:
    f = open("./sent_files/" + file_to_send, "rb")
except IOError, e:
    print ">error: ", e
    break

data = xmlrpclib.Binary(f.read())

if s.receive_file(file_to_send, data):
    print ">file correctly sent"

服务器:

def receive_file(self, name, arg):                                        
    with open("./sampletest/"+name, "wb") as handle: 
        handle.write(arg.data)

但是我如何做相反的事情(我的意思是从服务器向客户端发送文件)?

最佳答案

像这样在服务器上写一个函数:

def send_file(self, name):
  with open('./sampletest/' + name, 'rb') as handle:
    return handle.read()

并在客户端上调用它:

data = send_file(fileName)
with open('./received_files/' + fileName, 'wb') as handle:
  handle.write(data)

关于python - 将文件从服务器发送到客户端(python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16790725/

相关文章:

python - 从字符串中提取列表

java - 带有 applet 的 Java 客户端服务器游戏

c++ - 在 Windows 服务器上使用 C++ 后端的网站

c++ - 用C++设计文件传输协议(protocol)

audio - 使用 Java 流式传输音频

android - phonegap 从 url 下载图像并将其保存到应用程序安装文件夹

python - 训练期间的 Tensorflow Slim 调试

python - 试图查看变量是否包含在 django 的列表中

Python xarray : Extract first and last time value within each month of a timeseries

c# - 通过 C# 构建基于 Windows 的客户端/服务器应用程序